@vitejs/plugin-react 2.0.0-alpha.3 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +46 -16
- package/dist/index.mjs +43 -14
- package/package.json +11 -11
- package/src/fast-refresh.ts +3 -3
- package/src/index.ts +45 -11
package/dist/index.cjs
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
const path = require('path');
|
3
|
+
const path = require('node:path');
|
4
4
|
const babel = require('@babel/core');
|
5
|
-
const pluginutils = require('@rollup/pluginutils');
|
6
5
|
const vite = require('vite');
|
7
|
-
const
|
8
|
-
const
|
6
|
+
const MagicString = require('magic-string');
|
7
|
+
const fs = require('node:fs');
|
8
|
+
const node_module = require('node:module');
|
9
9
|
|
10
10
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
|
11
11
|
|
@@ -23,10 +23,11 @@ function _interopNamespace(e) {
|
|
23
23
|
|
24
24
|
const path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
25
25
|
const babel__namespace = /*#__PURE__*/_interopNamespace(babel);
|
26
|
+
const MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
|
26
27
|
const fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
27
28
|
|
28
29
|
const runtimePublicPath = "/@react-refresh";
|
29
|
-
const _require =
|
30
|
+
const _require = node_module.createRequire((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)));
|
30
31
|
const reactRefreshDir = path__default.dirname(_require.resolve("react-refresh/package.json"));
|
31
32
|
const runtimeFilePath = path__default.join(reactRefreshDir, "cjs/react-refresh-runtime.development.js");
|
32
33
|
const runtimeCode = `
|
@@ -184,10 +185,12 @@ function parseReactAlias(code) {
|
|
184
185
|
return [void 0, false];
|
185
186
|
}
|
186
187
|
|
188
|
+
const prependReactImportCode = "import React from 'react'; ";
|
187
189
|
function viteReact(opts = {}) {
|
188
|
-
let
|
190
|
+
let devBase = "/";
|
189
191
|
let resolvedCacheDir;
|
190
|
-
let filter =
|
192
|
+
let filter = vite.createFilter(opts.include, opts.exclude);
|
193
|
+
let needHiresSourcemap = false;
|
191
194
|
let isProduction = true;
|
192
195
|
let projectRoot = process.cwd();
|
193
196
|
let skipFastRefresh = opts.fastRefresh === false;
|
@@ -200,13 +203,25 @@ function viteReact(opts = {}) {
|
|
200
203
|
const viteBabel = {
|
201
204
|
name: "vite:react-babel",
|
202
205
|
enforce: "pre",
|
206
|
+
config() {
|
207
|
+
if (opts.jsxRuntime === "classic") {
|
208
|
+
return {
|
209
|
+
esbuild: {
|
210
|
+
logOverride: {
|
211
|
+
"this-is-undefined-in-esm": "silent"
|
212
|
+
}
|
213
|
+
}
|
214
|
+
};
|
215
|
+
}
|
216
|
+
},
|
203
217
|
configResolved(config) {
|
204
|
-
|
218
|
+
devBase = config.base;
|
205
219
|
projectRoot = config.root;
|
206
220
|
resolvedCacheDir = vite.normalizePath(path__default.resolve(config.cacheDir));
|
207
|
-
filter =
|
221
|
+
filter = vite.createFilter(opts.include, opts.exclude, {
|
208
222
|
resolve: projectRoot
|
209
223
|
});
|
224
|
+
needHiresSourcemap = config.command === "build" && !!config.build.sourcemap;
|
210
225
|
isProduction = config.isProduction;
|
211
226
|
skipFastRefresh || (skipFastRefresh = isProduction || config.command === "build");
|
212
227
|
const jsxInject = config.esbuild && config.esbuild.jsxInject;
|
@@ -222,10 +237,10 @@ function viteReact(opts = {}) {
|
|
222
237
|
runPluginOverrides = (babelOptions, context) => {
|
223
238
|
const hooks = config.plugins.map((plugin) => plugin.api?.reactBabel).filter(Boolean);
|
224
239
|
if (hooks.length > 0) {
|
225
|
-
return (runPluginOverrides = (babelOptions2) => {
|
226
|
-
hooks.forEach((hook) => hook(babelOptions2,
|
240
|
+
return (runPluginOverrides = (babelOptions2, context2) => {
|
241
|
+
hooks.forEach((hook) => hook(babelOptions2, context2, config));
|
227
242
|
return true;
|
228
|
-
})(babelOptions);
|
243
|
+
})(babelOptions, context);
|
229
244
|
}
|
230
245
|
runPluginOverrides = () => false;
|
231
246
|
return false;
|
@@ -263,6 +278,7 @@ function viteReact(opts = {}) {
|
|
263
278
|
}
|
264
279
|
}
|
265
280
|
let ast;
|
281
|
+
let prependReactImport = false;
|
266
282
|
if (!isProjectFile || isJSX) {
|
267
283
|
if (useAutomaticRuntime) {
|
268
284
|
const isOptimizedReactDom = id.startsWith(resolvedCacheDir) && id.includes("/react-dom.js");
|
@@ -285,13 +301,27 @@ function viteReact(opts = {}) {
|
|
285
301
|
plugins.push(await loadPlugin("@babel/plugin-transform-react-jsx-self"), await loadPlugin("@babel/plugin-transform-react-jsx-source"));
|
286
302
|
}
|
287
303
|
if (!skipReactImport && !importReactRE.test(code)) {
|
288
|
-
|
304
|
+
prependReactImport = true;
|
289
305
|
}
|
290
306
|
}
|
291
307
|
}
|
308
|
+
let inputMap;
|
309
|
+
if (prependReactImport) {
|
310
|
+
if (needHiresSourcemap) {
|
311
|
+
const s = new MagicString__default(code);
|
312
|
+
s.prepend(prependReactImportCode);
|
313
|
+
code = s.toString();
|
314
|
+
inputMap = s.generateMap({ hires: true, source: id });
|
315
|
+
} else {
|
316
|
+
code = prependReactImportCode + code;
|
317
|
+
}
|
318
|
+
}
|
292
319
|
const shouldSkip = !plugins.length && !babelOptions.configFile && !(isProjectFile && babelOptions.babelrc);
|
293
320
|
if (shouldSkip) {
|
294
|
-
return
|
321
|
+
return {
|
322
|
+
code,
|
323
|
+
map: inputMap ?? null
|
324
|
+
};
|
295
325
|
}
|
296
326
|
const parserPlugins = [
|
297
327
|
...babelOptions.parserOpts.plugins,
|
@@ -327,7 +357,7 @@ function viteReact(opts = {}) {
|
|
327
357
|
},
|
328
358
|
plugins,
|
329
359
|
sourceMaps: true,
|
330
|
-
inputSourceMap: false
|
360
|
+
inputSourceMap: inputMap ?? false
|
331
361
|
});
|
332
362
|
if (result) {
|
333
363
|
let code2 = result.code;
|
@@ -367,7 +397,7 @@ function viteReact(opts = {}) {
|
|
367
397
|
{
|
368
398
|
tag: "script",
|
369
399
|
attrs: { type: "module" },
|
370
|
-
children: preambleCode.replace(`__BASE__`,
|
400
|
+
children: preambleCode.replace(`__BASE__`, devBase)
|
371
401
|
}
|
372
402
|
];
|
373
403
|
}
|
package/dist/index.mjs
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
import path from 'path';
|
1
|
+
import path from 'node:path';
|
2
2
|
import * as babel from '@babel/core';
|
3
|
-
import { createFilter } from '
|
4
|
-
import
|
5
|
-
import fs from 'fs';
|
6
|
-
import { createRequire } from 'module';
|
3
|
+
import { createFilter, normalizePath } from 'vite';
|
4
|
+
import MagicString from 'magic-string';
|
5
|
+
import fs from 'node:fs';
|
6
|
+
import { createRequire } from 'node:module';
|
7
7
|
|
8
8
|
const runtimePublicPath = "/@react-refresh";
|
9
9
|
const _require = createRequire(import.meta.url);
|
@@ -164,10 +164,12 @@ function parseReactAlias(code) {
|
|
164
164
|
return [void 0, false];
|
165
165
|
}
|
166
166
|
|
167
|
+
const prependReactImportCode = "import React from 'react'; ";
|
167
168
|
function viteReact(opts = {}) {
|
168
|
-
let
|
169
|
+
let devBase = "/";
|
169
170
|
let resolvedCacheDir;
|
170
171
|
let filter = createFilter(opts.include, opts.exclude);
|
172
|
+
let needHiresSourcemap = false;
|
171
173
|
let isProduction = true;
|
172
174
|
let projectRoot = process.cwd();
|
173
175
|
let skipFastRefresh = opts.fastRefresh === false;
|
@@ -180,13 +182,25 @@ function viteReact(opts = {}) {
|
|
180
182
|
const viteBabel = {
|
181
183
|
name: "vite:react-babel",
|
182
184
|
enforce: "pre",
|
185
|
+
config() {
|
186
|
+
if (opts.jsxRuntime === "classic") {
|
187
|
+
return {
|
188
|
+
esbuild: {
|
189
|
+
logOverride: {
|
190
|
+
"this-is-undefined-in-esm": "silent"
|
191
|
+
}
|
192
|
+
}
|
193
|
+
};
|
194
|
+
}
|
195
|
+
},
|
183
196
|
configResolved(config) {
|
184
|
-
|
197
|
+
devBase = config.base;
|
185
198
|
projectRoot = config.root;
|
186
199
|
resolvedCacheDir = normalizePath(path.resolve(config.cacheDir));
|
187
200
|
filter = createFilter(opts.include, opts.exclude, {
|
188
201
|
resolve: projectRoot
|
189
202
|
});
|
203
|
+
needHiresSourcemap = config.command === "build" && !!config.build.sourcemap;
|
190
204
|
isProduction = config.isProduction;
|
191
205
|
skipFastRefresh || (skipFastRefresh = isProduction || config.command === "build");
|
192
206
|
const jsxInject = config.esbuild && config.esbuild.jsxInject;
|
@@ -202,10 +216,10 @@ function viteReact(opts = {}) {
|
|
202
216
|
runPluginOverrides = (babelOptions, context) => {
|
203
217
|
const hooks = config.plugins.map((plugin) => plugin.api?.reactBabel).filter(Boolean);
|
204
218
|
if (hooks.length > 0) {
|
205
|
-
return (runPluginOverrides = (babelOptions2) => {
|
206
|
-
hooks.forEach((hook) => hook(babelOptions2,
|
219
|
+
return (runPluginOverrides = (babelOptions2, context2) => {
|
220
|
+
hooks.forEach((hook) => hook(babelOptions2, context2, config));
|
207
221
|
return true;
|
208
|
-
})(babelOptions);
|
222
|
+
})(babelOptions, context);
|
209
223
|
}
|
210
224
|
runPluginOverrides = () => false;
|
211
225
|
return false;
|
@@ -243,6 +257,7 @@ function viteReact(opts = {}) {
|
|
243
257
|
}
|
244
258
|
}
|
245
259
|
let ast;
|
260
|
+
let prependReactImport = false;
|
246
261
|
if (!isProjectFile || isJSX) {
|
247
262
|
if (useAutomaticRuntime) {
|
248
263
|
const isOptimizedReactDom = id.startsWith(resolvedCacheDir) && id.includes("/react-dom.js");
|
@@ -265,13 +280,27 @@ function viteReact(opts = {}) {
|
|
265
280
|
plugins.push(await loadPlugin("@babel/plugin-transform-react-jsx-self"), await loadPlugin("@babel/plugin-transform-react-jsx-source"));
|
266
281
|
}
|
267
282
|
if (!skipReactImport && !importReactRE.test(code)) {
|
268
|
-
|
283
|
+
prependReactImport = true;
|
269
284
|
}
|
270
285
|
}
|
271
286
|
}
|
287
|
+
let inputMap;
|
288
|
+
if (prependReactImport) {
|
289
|
+
if (needHiresSourcemap) {
|
290
|
+
const s = new MagicString(code);
|
291
|
+
s.prepend(prependReactImportCode);
|
292
|
+
code = s.toString();
|
293
|
+
inputMap = s.generateMap({ hires: true, source: id });
|
294
|
+
} else {
|
295
|
+
code = prependReactImportCode + code;
|
296
|
+
}
|
297
|
+
}
|
272
298
|
const shouldSkip = !plugins.length && !babelOptions.configFile && !(isProjectFile && babelOptions.babelrc);
|
273
299
|
if (shouldSkip) {
|
274
|
-
return
|
300
|
+
return {
|
301
|
+
code,
|
302
|
+
map: inputMap ?? null
|
303
|
+
};
|
275
304
|
}
|
276
305
|
const parserPlugins = [
|
277
306
|
...babelOptions.parserOpts.plugins,
|
@@ -307,7 +336,7 @@ function viteReact(opts = {}) {
|
|
307
336
|
},
|
308
337
|
plugins,
|
309
338
|
sourceMaps: true,
|
310
|
-
inputSourceMap: false
|
339
|
+
inputSourceMap: inputMap ?? false
|
311
340
|
});
|
312
341
|
if (result) {
|
313
342
|
let code2 = result.code;
|
@@ -347,7 +376,7 @@ function viteReact(opts = {}) {
|
|
347
376
|
{
|
348
377
|
tag: "script",
|
349
378
|
attrs: { type: "module" },
|
350
|
-
children: preambleCode.replace(`__BASE__`,
|
379
|
+
children: preambleCode.replace(`__BASE__`, devBase)
|
351
380
|
}
|
352
381
|
];
|
353
382
|
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@vitejs/plugin-react",
|
3
|
-
"version": "2.0.0
|
3
|
+
"version": "2.0.0",
|
4
4
|
"license": "MIT",
|
5
5
|
"author": "Evan You",
|
6
6
|
"contributors": [
|
@@ -23,11 +23,11 @@
|
|
23
23
|
"scripts": {
|
24
24
|
"dev": "unbuild --stub",
|
25
25
|
"build": "unbuild && pnpm run patch-cjs",
|
26
|
-
"patch-cjs": "
|
26
|
+
"patch-cjs": "tsx ../../scripts/patchCJS.ts",
|
27
27
|
"prepublishOnly": "npm run build"
|
28
28
|
},
|
29
29
|
"engines": {
|
30
|
-
"node": ">=14.
|
30
|
+
"node": ">=14.18.0"
|
31
31
|
},
|
32
32
|
"repository": {
|
33
33
|
"type": "git",
|
@@ -39,16 +39,16 @@
|
|
39
39
|
},
|
40
40
|
"homepage": "https://github.com/vitejs/vite/tree/main/packages/plugin-react#readme",
|
41
41
|
"dependencies": {
|
42
|
-
"@babel/core": "^7.18.
|
43
|
-
"@babel/plugin-transform-react-jsx": "^7.
|
44
|
-
"@babel/plugin-transform-react-jsx-development": "^7.
|
45
|
-
"@babel/plugin-transform-react-jsx-self": "^7.
|
46
|
-
"@babel/plugin-transform-react-jsx-source": "^7.
|
47
|
-
"
|
48
|
-
"react-refresh": "^0.
|
42
|
+
"@babel/core": "^7.18.6",
|
43
|
+
"@babel/plugin-transform-react-jsx": "^7.18.6",
|
44
|
+
"@babel/plugin-transform-react-jsx-development": "^7.18.6",
|
45
|
+
"@babel/plugin-transform-react-jsx-self": "^7.18.6",
|
46
|
+
"@babel/plugin-transform-react-jsx-source": "^7.18.6",
|
47
|
+
"magic-string": "^0.26.2",
|
48
|
+
"react-refresh": "^0.14.0"
|
49
49
|
},
|
50
50
|
"peerDependencies": {
|
51
|
-
"vite": "^3.0.0
|
51
|
+
"vite": "^3.0.0"
|
52
52
|
},
|
53
53
|
"devDependencies": {
|
54
54
|
"vite": "workspace:*"
|
package/src/fast-refresh.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
import fs from 'fs'
|
2
|
-
import path from 'path'
|
3
|
-
import { createRequire } from 'module'
|
1
|
+
import fs from 'node:fs'
|
2
|
+
import path from 'node:path'
|
3
|
+
import { createRequire } from 'node:module'
|
4
4
|
import type { types as t } from '@babel/core'
|
5
5
|
|
6
6
|
export const runtimePublicPath = '/@react-refresh'
|
package/src/index.ts
CHANGED
@@ -1,9 +1,10 @@
|
|
1
|
-
import path from 'path'
|
1
|
+
import path from 'node:path'
|
2
2
|
import type { ParserOptions, TransformOptions, types as t } from '@babel/core'
|
3
3
|
import * as babel from '@babel/core'
|
4
|
-
import { createFilter } from '
|
5
|
-
import { normalizePath } from 'vite'
|
4
|
+
import { createFilter, normalizePath } from 'vite'
|
6
5
|
import type { Plugin, PluginOption, ResolvedConfig } from 'vite'
|
6
|
+
import MagicString from 'magic-string'
|
7
|
+
import type { SourceMap } from 'magic-string'
|
7
8
|
import {
|
8
9
|
addRefreshWrapper,
|
9
10
|
isRefreshBoundary,
|
@@ -89,11 +90,14 @@ declare module 'vite' {
|
|
89
90
|
}
|
90
91
|
}
|
91
92
|
|
93
|
+
const prependReactImportCode = "import React from 'react'; "
|
94
|
+
|
92
95
|
export default function viteReact(opts: Options = {}): PluginOption[] {
|
93
96
|
// Provide default values for Rollup compat.
|
94
|
-
let
|
97
|
+
let devBase = '/'
|
95
98
|
let resolvedCacheDir: string
|
96
99
|
let filter = createFilter(opts.include, opts.exclude)
|
100
|
+
let needHiresSourcemap = false
|
97
101
|
let isProduction = true
|
98
102
|
let projectRoot = process.cwd()
|
99
103
|
let skipFastRefresh = opts.fastRefresh === false
|
@@ -118,13 +122,26 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
|
|
118
122
|
const viteBabel: Plugin = {
|
119
123
|
name: 'vite:react-babel',
|
120
124
|
enforce: 'pre',
|
125
|
+
config() {
|
126
|
+
if (opts.jsxRuntime === 'classic') {
|
127
|
+
return {
|
128
|
+
esbuild: {
|
129
|
+
logOverride: {
|
130
|
+
'this-is-undefined-in-esm': 'silent'
|
131
|
+
}
|
132
|
+
}
|
133
|
+
}
|
134
|
+
}
|
135
|
+
},
|
121
136
|
configResolved(config) {
|
122
|
-
|
137
|
+
devBase = config.base
|
123
138
|
projectRoot = config.root
|
124
139
|
resolvedCacheDir = normalizePath(path.resolve(config.cacheDir))
|
125
140
|
filter = createFilter(opts.include, opts.exclude, {
|
126
141
|
resolve: projectRoot
|
127
142
|
})
|
143
|
+
needHiresSourcemap =
|
144
|
+
config.command === 'build' && !!config.build.sourcemap
|
128
145
|
isProduction = config.isProduction
|
129
146
|
skipFastRefresh ||= isProduction || config.command === 'build'
|
130
147
|
|
@@ -155,10 +172,10 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
|
|
155
172
|
.filter(Boolean) as ReactBabelHook[]
|
156
173
|
|
157
174
|
if (hooks.length > 0) {
|
158
|
-
return (runPluginOverrides = (babelOptions) => {
|
175
|
+
return (runPluginOverrides = (babelOptions, context) => {
|
159
176
|
hooks.forEach((hook) => hook(babelOptions, context, config))
|
160
177
|
return true
|
161
|
-
})(babelOptions)
|
178
|
+
})(babelOptions, context)
|
162
179
|
}
|
163
180
|
runPluginOverrides = () => false
|
164
181
|
return false
|
@@ -207,6 +224,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
|
|
207
224
|
}
|
208
225
|
|
209
226
|
let ast: t.File | null | undefined
|
227
|
+
let prependReactImport = false
|
210
228
|
if (!isProjectFile || isJSX) {
|
211
229
|
if (useAutomaticRuntime) {
|
212
230
|
// By reverse-compiling "React.createElement" calls into JSX,
|
@@ -251,11 +269,23 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
|
|
251
269
|
// Even if the automatic JSX runtime is not used, we can still
|
252
270
|
// inject the React import for .jsx and .tsx modules.
|
253
271
|
if (!skipReactImport && !importReactRE.test(code)) {
|
254
|
-
|
272
|
+
prependReactImport = true
|
255
273
|
}
|
256
274
|
}
|
257
275
|
}
|
258
276
|
|
277
|
+
let inputMap: SourceMap | undefined
|
278
|
+
if (prependReactImport) {
|
279
|
+
if (needHiresSourcemap) {
|
280
|
+
const s = new MagicString(code)
|
281
|
+
s.prepend(prependReactImportCode)
|
282
|
+
code = s.toString()
|
283
|
+
inputMap = s.generateMap({ hires: true, source: id })
|
284
|
+
} else {
|
285
|
+
code = prependReactImportCode + code
|
286
|
+
}
|
287
|
+
}
|
288
|
+
|
259
289
|
// Plugins defined through this Vite plugin are only applied
|
260
290
|
// to modules within the project root, but "babel.config.js"
|
261
291
|
// files can define plugins that need to be applied to every
|
@@ -265,8 +295,12 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
|
|
265
295
|
!babelOptions.configFile &&
|
266
296
|
!(isProjectFile && babelOptions.babelrc)
|
267
297
|
|
298
|
+
// Avoid parsing if no plugins exist.
|
268
299
|
if (shouldSkip) {
|
269
|
-
return
|
300
|
+
return {
|
301
|
+
code,
|
302
|
+
map: inputMap ?? null
|
303
|
+
}
|
270
304
|
}
|
271
305
|
|
272
306
|
const parserPlugins: typeof babelOptions.parserOpts.plugins = [
|
@@ -313,7 +347,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
|
|
313
347
|
plugins,
|
314
348
|
sourceMaps: true,
|
315
349
|
// Vite handles sourcemap flattening
|
316
|
-
inputSourceMap: false as any
|
350
|
+
inputSourceMap: inputMap ?? (false as any)
|
317
351
|
})
|
318
352
|
|
319
353
|
if (result) {
|
@@ -355,7 +389,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
|
|
355
389
|
{
|
356
390
|
tag: 'script',
|
357
391
|
attrs: { type: 'module' },
|
358
|
-
children: preambleCode.replace(`__BASE__`,
|
392
|
+
children: preambleCode.replace(`__BASE__`, devBase)
|
359
393
|
}
|
360
394
|
]
|
361
395
|
}
|