Skip to content

Commit 9baa70b

Browse files
authored
fix: rewrite CJS specific funcs/vars in plugins (#8227)
1 parent 771312b commit 9baa70b

File tree

9 files changed

+28
-10
lines changed

9 files changed

+28
-10
lines changed

.eslintrc.cjs

+6
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ module.exports = defineConfig({
115115
'node/no-extraneous-import': 'off'
116116
}
117117
},
118+
{
119+
files: ['packages/plugin-*/**/*'],
120+
rules: {
121+
'no-restricted-globals': ['error', 'require', '__dirname', '__filename']
122+
}
123+
},
118124
{
119125
files: ['playground/**'],
120126
rules: {

packages/plugin-legacy/src/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* eslint-disable node/no-extraneous-import */
22
import path from 'path'
33
import { createHash } from 'crypto'
4+
import { createRequire } from 'module'
5+
import { fileURLToPath } from 'url'
46
import { build } from 'vite'
57
import MagicString from 'magic-string'
68
import type {
@@ -171,6 +173,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
171173
}
172174
}
173175

176+
const _require = createRequire(import.meta.url)
174177
const legacyPostPlugin: Plugin = {
175178
name: 'vite:legacy-post-process',
176179
enforce: 'post',
@@ -331,7 +334,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
331334
useBuiltIns: needPolyfills ? 'usage' : false,
332335
corejs: needPolyfills
333336
? {
334-
version: require('core-js/package.json').version,
337+
version: _require('core-js/package.json').version,
335338
proposals: false
336339
}
337340
: undefined,
@@ -557,7 +560,7 @@ async function buildPolyfillChunk(
557560
minify = minify ? 'terser' : false
558561
const res = await build({
559562
// so that everything is resolved from here
560-
root: __dirname,
563+
root: path.dirname(fileURLToPath(import.meta.url)),
561564
configFile: false,
562565
logLevel: 'error',
563566
plugins: [polyfillsPlugin(imports, externalSystemJS)],

packages/plugin-legacy/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"compilerOptions": {
55
"outDir": "dist",
66
"target": "ES2020",
7-
"module": "CommonJS",
7+
"module": "ES2020",
88
"moduleResolution": "Node",
99
"strict": true,
1010
"declaration": true,

packages/plugin-react/src/fast-refresh.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import fs from 'fs'
22
import path from 'path'
3+
import { createRequire } from 'module'
34
import type { types as t } from '@babel/core'
45

56
export const runtimePublicPath = '/@react-refresh'
67

8+
const _require = createRequire(import.meta.url)
79
const reactRefreshDir = path.dirname(
8-
require.resolve('react-refresh/package.json')
10+
_require.resolve('react-refresh/package.json')
911
)
1012
const runtimeFilePath = path.join(
1113
reactRefreshDir,

packages/plugin-react/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"compilerOptions": {
55
"outDir": "dist",
66
"target": "ES2020",
7-
"module": "CommonJS",
7+
"module": "ES2020",
88
"moduleResolution": "Node",
99
"strict": true,
1010
"declaration": true,

packages/plugin-vue-jsx/src/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function vueJsxPlugin(options: Options = {}): Plugin {
7373
}
7474
},
7575

76-
transform(code, id, opt) {
76+
async transform(code, id, opt) {
7777
const ssr = typeof opt === 'boolean' ? opt : (opt && opt.ssr) === true
7878
const {
7979
include,
@@ -91,7 +91,10 @@ function vueJsxPlugin(options: Options = {}): Plugin {
9191
const plugins = [importMeta, [jsx, babelPluginOptions], ...babelPlugins]
9292
if (id.endsWith('.tsx') || filepath.endsWith('.tsx')) {
9393
plugins.push([
94-
require('@babel/plugin-transform-typescript'),
94+
// @ts-ignore missing type
95+
await import('@babel/plugin-transform-typescript').then(
96+
(r) => r.default
97+
),
9598
// @ts-ignore
9699
{ isTSX: true, allowExtensions: true }
97100
])

packages/plugin-vue-jsx/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"compilerOptions": {
55
"outDir": "dist",
66
"target": "ES2020",
7-
"module": "CommonJS",
7+
"module": "ES2020",
88
"moduleResolution": "Node",
99
"strict": true,
1010
"declaration": true,

packages/plugin-vue/src/compiler.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ declare module 'vue/compiler-sfc' {
55
}
66
}
77

8+
import { createRequire } from 'module'
89
import type * as _compiler from 'vue/compiler-sfc'
910

1011
export function resolveCompiler(root: string): typeof _compiler {
@@ -23,8 +24,11 @@ export function resolveCompiler(root: string): typeof _compiler {
2324
return compiler
2425
}
2526

27+
const _require = createRequire(import.meta.url)
2628
function tryRequire(id: string, from?: string) {
2729
try {
28-
return from ? require(require.resolve(id, { paths: [from] })) : require(id)
30+
return from
31+
? _require(_require.resolve(id, { paths: [from] }))
32+
: _require(id)
2933
} catch (e) {}
3034
}

packages/plugin-vue/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"compilerOptions": {
55
"outDir": "dist",
66
"target": "ES2020",
7-
"module": "commonjs",
7+
"module": "ES2020",
88
"moduleResolution": "node",
99
"strict": true,
1010
"declaration": true,

0 commit comments

Comments
 (0)