@vitejs/plugin-react 4.3.4 → 4.4.0-beta.1
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/README.md +1 -1
- package/dist/index.cjs +105 -102
- package/dist/index.d.cts +2 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.mjs +105 -97
- package/dist/refresh-runtime.js +647 -0
- package/package.json +14 -3
- package/dist/refreshUtils.js +0 -93
package/README.md
CHANGED
@@ -96,7 +96,7 @@ Here's the [complete list of Babel parser plugins](https://babeljs.io/docs/en/ba
|
|
96
96
|
|
97
97
|
## Middleware mode
|
98
98
|
|
99
|
-
In [middleware mode](https://
|
99
|
+
In [middleware mode](https://vite.dev/config/server-options.html#server-middlewaremode), you should make sure your entry `index.html` file is transformed by Vite. Here's an example for an Express server:
|
100
100
|
|
101
101
|
```js
|
102
102
|
app.get('/', async (req, res, next) => {
|
package/dist/index.cjs
CHANGED
@@ -1,89 +1,115 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
+
const node_path = require('node:path');
|
4
|
+
const node_url = require('node:url');
|
5
|
+
const node_fs = require('node:fs');
|
3
6
|
const vite = require('vite');
|
4
|
-
const fs = require('node:fs');
|
5
|
-
const path = require('node:path');
|
6
|
-
const node_module = require('node:module');
|
7
7
|
|
8
8
|
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
9
|
-
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
10
|
-
|
11
|
-
const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
|
12
|
-
const path__default = /*#__PURE__*/_interopDefaultCompat(path);
|
13
|
-
|
14
9
|
const runtimePublicPath = "/@react-refresh";
|
15
|
-
const
|
16
|
-
const
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
)
|
23
|
-
const
|
24
|
-
const
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
const sharedHeader = `
|
37
|
-
import RefreshRuntime from "${runtimePublicPath}";
|
38
|
-
|
39
|
-
const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
|
40
|
-
`.replace(/\n+/g, "");
|
41
|
-
const functionHeader = `
|
42
|
-
let prevRefreshReg;
|
10
|
+
const reactCompRE = /extends\s+(?:React\.)?(?:Pure)?Component/;
|
11
|
+
const refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
|
12
|
+
const preambleCode = `import { injectIntoGlobalHook } from "__BASE__${runtimePublicPath.slice(
|
13
|
+
1
|
14
|
+
)}"
|
15
|
+
injectIntoGlobalHook(window);
|
16
|
+
window.$RefreshReg$ = () => {};
|
17
|
+
window.$RefreshSig$ = () => (type) => type;`;
|
18
|
+
const getPreambleCode = (base) => preambleCode.replace("__BASE__", base);
|
19
|
+
const avoidSourceMapOption = Symbol();
|
20
|
+
function addRefreshWrapper(code, map, pluginName, id) {
|
21
|
+
const hasRefresh = refreshContentRE.test(code);
|
22
|
+
const onlyReactComp = !hasRefresh && reactCompRE.test(code);
|
23
|
+
const normalizedMap = map === avoidSourceMapOption ? null : map;
|
24
|
+
if (!hasRefresh && !onlyReactComp) return { code, map: normalizedMap };
|
25
|
+
const avoidSourceMap = map === avoidSourceMapOption;
|
26
|
+
const newMap = typeof normalizedMap === "string" ? JSON.parse(normalizedMap) : normalizedMap;
|
27
|
+
let newCode = code;
|
28
|
+
if (hasRefresh) {
|
29
|
+
const refreshHead = removeLineBreaksIfNeeded(
|
30
|
+
`let prevRefreshReg;
|
43
31
|
let prevRefreshSig;
|
44
32
|
|
45
33
|
if (import.meta.hot && !inWebWorker) {
|
46
|
-
if (!window
|
34
|
+
if (!window.$RefreshReg$) {
|
47
35
|
throw new Error(
|
48
|
-
"
|
49
|
-
"See https://github.com/vitejs/vite-plugin-react/pull/11#discussion_r430879201"
|
36
|
+
"${pluginName} can't detect preamble. Something is wrong."
|
50
37
|
);
|
51
38
|
}
|
52
39
|
|
53
40
|
prevRefreshReg = window.$RefreshReg$;
|
54
41
|
prevRefreshSig = window.$RefreshSig$;
|
55
|
-
window.$RefreshReg$ = (
|
56
|
-
RefreshRuntime.register(type, __SOURCE__ + " " + id)
|
57
|
-
};
|
42
|
+
window.$RefreshReg$ = RefreshRuntime.getRefreshReg(${JSON.stringify(id)});
|
58
43
|
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
|
59
|
-
}
|
60
|
-
|
44
|
+
}
|
45
|
+
|
46
|
+
`,
|
47
|
+
avoidSourceMap
|
48
|
+
);
|
49
|
+
newCode = `${refreshHead}${newCode}
|
50
|
+
|
61
51
|
if (import.meta.hot && !inWebWorker) {
|
62
52
|
window.$RefreshReg$ = prevRefreshReg;
|
63
53
|
window.$RefreshSig$ = prevRefreshSig;
|
64
|
-
}
|
65
|
-
|
54
|
+
}
|
55
|
+
`;
|
56
|
+
if (newMap) {
|
57
|
+
newMap.mappings = ";".repeat(16) + newMap.mappings;
|
58
|
+
}
|
59
|
+
}
|
60
|
+
const sharedHead = removeLineBreaksIfNeeded(
|
61
|
+
`import * as RefreshRuntime from "${runtimePublicPath}";
|
62
|
+
const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
|
63
|
+
|
64
|
+
`,
|
65
|
+
avoidSourceMap
|
66
|
+
);
|
67
|
+
newCode = `${sharedHead}${newCode}
|
68
|
+
|
66
69
|
if (import.meta.hot && !inWebWorker) {
|
67
70
|
RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
|
68
71
|
RefreshRuntime.registerExportsForReactRefresh(${JSON.stringify(
|
69
|
-
|
70
|
-
)}, currentExports);
|
72
|
+
id
|
73
|
+
)}, currentExports);
|
71
74
|
import.meta.hot.accept((nextExports) => {
|
72
75
|
if (!nextExports) return;
|
73
76
|
const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(${JSON.stringify(
|
74
|
-
|
75
|
-
)}, currentExports, nextExports);
|
77
|
+
id
|
78
|
+
)}, currentExports, nextExports);
|
76
79
|
if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
|
77
80
|
});
|
78
81
|
});
|
79
|
-
}`;
|
80
|
-
function addRefreshWrapper(code, id) {
|
81
|
-
return sharedHeader + functionHeader.replace("__SOURCE__", JSON.stringify(id)) + code + functionFooter + sharedFooter(id);
|
82
82
|
}
|
83
|
-
|
84
|
-
|
83
|
+
`;
|
84
|
+
if (newMap) {
|
85
|
+
newMap.mappings = ";;;" + newMap.mappings;
|
86
|
+
}
|
87
|
+
return { code: newCode, map: newMap };
|
85
88
|
}
|
89
|
+
function removeLineBreaksIfNeeded(code, enabled) {
|
90
|
+
return enabled ? code.replace(/\n/g, "") : code;
|
91
|
+
}
|
92
|
+
|
93
|
+
const silenceUseClientWarning = (userConfig) => ({
|
94
|
+
rollupOptions: {
|
95
|
+
onwarn(warning, defaultHandler) {
|
96
|
+
if (warning.code === "MODULE_LEVEL_DIRECTIVE" && warning.message.includes("use client")) {
|
97
|
+
return;
|
98
|
+
}
|
99
|
+
if (warning.code === "SOURCEMAP_ERROR" && warning.message.includes("resolve original location") && warning.pos === 0) {
|
100
|
+
return;
|
101
|
+
}
|
102
|
+
if (userConfig.build?.rollupOptions?.onwarn) {
|
103
|
+
userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
|
104
|
+
} else {
|
105
|
+
defaultHandler(warning);
|
106
|
+
}
|
107
|
+
}
|
108
|
+
}
|
109
|
+
});
|
86
110
|
|
111
|
+
const _dirname = node_path.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))));
|
112
|
+
const refreshRuntimePath = node_path.join(_dirname, "refresh-runtime.js") ;
|
87
113
|
let babel;
|
88
114
|
async function loadBabel() {
|
89
115
|
if (!babel) {
|
@@ -91,12 +117,9 @@ async function loadBabel() {
|
|
91
117
|
}
|
92
118
|
return babel;
|
93
119
|
}
|
94
|
-
const reactCompRE = /extends\s+(?:React\.)?(?:Pure)?Component/;
|
95
|
-
const refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
|
96
120
|
const defaultIncludeRE = /\.[tj]sx?$/;
|
97
121
|
const tsRE = /\.tsx?$/;
|
98
122
|
function viteReact(opts = {}) {
|
99
|
-
let devBase = "/";
|
100
123
|
const filter = vite.createFilter(opts.include ?? defaultIncludeRE, opts.exclude);
|
101
124
|
const jsxImportSource = opts.jsxImportSource ?? "react";
|
102
125
|
const jsxImportRuntime = `${jsxImportSource}/jsx-runtime`;
|
@@ -128,7 +151,6 @@ function viteReact(opts = {}) {
|
|
128
151
|
}
|
129
152
|
},
|
130
153
|
configResolved(config) {
|
131
|
-
devBase = config.base;
|
132
154
|
projectRoot = config.root;
|
133
155
|
isProduction = config.isProduction;
|
134
156
|
skipFastRefresh = isProduction || config.command === "build" || config.server.hmr === false;
|
@@ -147,15 +169,12 @@ function viteReact(opts = {}) {
|
|
147
169
|
}
|
148
170
|
},
|
149
171
|
async transform(code, id, options) {
|
150
|
-
if (id.includes("/node_modules/"))
|
151
|
-
return;
|
172
|
+
if (id.includes("/node_modules/")) return;
|
152
173
|
const [filepath] = id.split("?");
|
153
|
-
if (!filter(filepath))
|
154
|
-
return;
|
174
|
+
if (!filter(filepath)) return;
|
155
175
|
const ssr = options?.ssr === true;
|
156
176
|
const babelOptions = (() => {
|
157
|
-
if (staticBabelOptions)
|
158
|
-
return staticBabelOptions;
|
177
|
+
if (staticBabelOptions) return staticBabelOptions;
|
159
178
|
const newBabelOptions = createBabelOptions(
|
160
179
|
typeof opts.babel === "function" ? opts.babel(id, { ssr }) : opts.babel
|
161
180
|
);
|
@@ -196,7 +215,7 @@ function viteReact(opts = {}) {
|
|
196
215
|
filename: id,
|
197
216
|
sourceFileName: filepath,
|
198
217
|
// Required for esbuild.jsxDev to provide correct line numbers
|
199
|
-
// This
|
218
|
+
// This creates issues the react compiler because the re-order is too important
|
200
219
|
// People should use @babel/plugin-transform-react-jsx-development to get back good line numbers
|
201
220
|
retainLines: getReactCompilerPlugin(plugins) != null ? false : !isProduction && isJSX && opts.jsxRuntime !== "classic",
|
202
221
|
parserOpts: {
|
@@ -215,15 +234,15 @@ function viteReact(opts = {}) {
|
|
215
234
|
sourceMaps: true
|
216
235
|
});
|
217
236
|
if (result) {
|
218
|
-
|
219
|
-
|
220
|
-
if (refreshContentRE.test(code2)) {
|
221
|
-
code2 = addRefreshWrapper(code2, id);
|
222
|
-
} else if (reactCompRE.test(code2)) {
|
223
|
-
code2 = addClassComponentRefreshWrapper(code2, id);
|
224
|
-
}
|
237
|
+
if (!useFastRefresh) {
|
238
|
+
return { code: result.code, map: result.map };
|
225
239
|
}
|
226
|
-
return
|
240
|
+
return addRefreshWrapper(
|
241
|
+
result.code,
|
242
|
+
result.map,
|
243
|
+
"@vitejs/plugin-react",
|
244
|
+
id
|
245
|
+
);
|
227
246
|
}
|
228
247
|
}
|
229
248
|
};
|
@@ -258,16 +277,19 @@ function viteReact(opts = {}) {
|
|
258
277
|
},
|
259
278
|
load(id) {
|
260
279
|
if (id === runtimePublicPath) {
|
261
|
-
return
|
280
|
+
return node_fs.readFileSync(refreshRuntimePath, "utf-8").replace(
|
281
|
+
/__README_URL__/g,
|
282
|
+
"https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react"
|
283
|
+
);
|
262
284
|
}
|
263
285
|
},
|
264
|
-
transformIndexHtml() {
|
286
|
+
transformIndexHtml(_, config) {
|
265
287
|
if (!skipFastRefresh)
|
266
288
|
return [
|
267
289
|
{
|
268
290
|
tag: "script",
|
269
291
|
attrs: { type: "module" },
|
270
|
-
children:
|
292
|
+
children: getPreambleCode(config.server.config.base)
|
271
293
|
}
|
272
294
|
];
|
273
295
|
}
|
@@ -275,28 +297,10 @@ function viteReact(opts = {}) {
|
|
275
297
|
return [viteBabel, viteReactRefresh];
|
276
298
|
}
|
277
299
|
viteReact.preambleCode = preambleCode;
|
278
|
-
const silenceUseClientWarning = (userConfig) => ({
|
279
|
-
rollupOptions: {
|
280
|
-
onwarn(warning, defaultHandler) {
|
281
|
-
if (warning.code === "MODULE_LEVEL_DIRECTIVE" && warning.message.includes("use client")) {
|
282
|
-
return;
|
283
|
-
}
|
284
|
-
if (warning.code === "SOURCEMAP_ERROR" && warning.message.includes("resolve original location") && warning.pos === 0) {
|
285
|
-
return;
|
286
|
-
}
|
287
|
-
if (userConfig.build?.rollupOptions?.onwarn) {
|
288
|
-
userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
|
289
|
-
} else {
|
290
|
-
defaultHandler(warning);
|
291
|
-
}
|
292
|
-
}
|
293
|
-
}
|
294
|
-
});
|
295
300
|
const loadedPlugin = /* @__PURE__ */ new Map();
|
296
301
|
function loadPlugin(path) {
|
297
302
|
const cached = loadedPlugin.get(path);
|
298
|
-
if (cached)
|
299
|
-
return cached;
|
303
|
+
if (cached) return cached;
|
300
304
|
const promise = import(path).then((module) => {
|
301
305
|
const value = module.default || module;
|
302
306
|
loadedPlugin.set(path, value);
|
@@ -306,17 +310,16 @@ function loadPlugin(path) {
|
|
306
310
|
return promise;
|
307
311
|
}
|
308
312
|
function createBabelOptions(rawOptions) {
|
309
|
-
var _a;
|
310
313
|
const babelOptions = {
|
311
314
|
babelrc: false,
|
312
315
|
configFile: false,
|
313
316
|
...rawOptions
|
314
317
|
};
|
315
|
-
babelOptions.plugins
|
316
|
-
babelOptions.presets
|
317
|
-
babelOptions.overrides
|
318
|
-
babelOptions.parserOpts
|
319
|
-
|
318
|
+
babelOptions.plugins ||= [];
|
319
|
+
babelOptions.presets ||= [];
|
320
|
+
babelOptions.overrides ||= [];
|
321
|
+
babelOptions.parserOpts ||= {};
|
322
|
+
babelOptions.parserOpts.plugins ||= [];
|
320
323
|
return babelOptions;
|
321
324
|
}
|
322
325
|
function defined(value) {
|
package/dist/index.d.cts
CHANGED
@@ -51,4 +51,5 @@ declare namespace viteReact {
|
|
51
51
|
var preambleCode: string;
|
52
52
|
}
|
53
53
|
|
54
|
-
export
|
54
|
+
export = viteReact;
|
55
|
+
export type { BabelOptions, Options, ReactBabelOptions, ViteReactPluginApi };
|
package/dist/index.d.mts
CHANGED
@@ -51,4 +51,5 @@ declare namespace viteReact {
|
|
51
51
|
var preambleCode: string;
|
52
52
|
}
|
53
53
|
|
54
|
-
export {
|
54
|
+
export { viteReact as default };
|
55
|
+
export type { BabelOptions, Options, ReactBabelOptions, ViteReactPluginApi };
|
package/dist/index.d.ts
CHANGED
@@ -51,4 +51,5 @@ declare namespace viteReact {
|
|
51
51
|
var preambleCode: string;
|
52
52
|
}
|
53
53
|
|
54
|
-
export
|
54
|
+
export = viteReact;
|
55
|
+
export type { BabelOptions, Options, ReactBabelOptions, ViteReactPluginApi };
|
package/dist/index.mjs
CHANGED
@@ -1,81 +1,112 @@
|
|
1
|
+
import { dirname, join } from 'node:path';
|
2
|
+
import { fileURLToPath } from 'node:url';
|
3
|
+
import { readFileSync } from 'node:fs';
|
1
4
|
import { createFilter } from 'vite';
|
2
|
-
import fs from 'node:fs';
|
3
|
-
import path from 'node:path';
|
4
|
-
import { createRequire } from 'node:module';
|
5
5
|
|
6
6
|
const runtimePublicPath = "/@react-refresh";
|
7
|
-
const
|
8
|
-
const
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
)
|
15
|
-
const
|
16
|
-
const
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
const sharedHeader = `
|
29
|
-
import RefreshRuntime from "${runtimePublicPath}";
|
30
|
-
|
31
|
-
const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
|
32
|
-
`.replace(/\n+/g, "");
|
33
|
-
const functionHeader = `
|
34
|
-
let prevRefreshReg;
|
7
|
+
const reactCompRE = /extends\s+(?:React\.)?(?:Pure)?Component/;
|
8
|
+
const refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
|
9
|
+
const preambleCode = `import { injectIntoGlobalHook } from "__BASE__${runtimePublicPath.slice(
|
10
|
+
1
|
11
|
+
)}"
|
12
|
+
injectIntoGlobalHook(window);
|
13
|
+
window.$RefreshReg$ = () => {};
|
14
|
+
window.$RefreshSig$ = () => (type) => type;`;
|
15
|
+
const getPreambleCode = (base) => preambleCode.replace("__BASE__", base);
|
16
|
+
const avoidSourceMapOption = Symbol();
|
17
|
+
function addRefreshWrapper(code, map, pluginName, id) {
|
18
|
+
const hasRefresh = refreshContentRE.test(code);
|
19
|
+
const onlyReactComp = !hasRefresh && reactCompRE.test(code);
|
20
|
+
const normalizedMap = map === avoidSourceMapOption ? null : map;
|
21
|
+
if (!hasRefresh && !onlyReactComp) return { code, map: normalizedMap };
|
22
|
+
const avoidSourceMap = map === avoidSourceMapOption;
|
23
|
+
const newMap = typeof normalizedMap === "string" ? JSON.parse(normalizedMap) : normalizedMap;
|
24
|
+
let newCode = code;
|
25
|
+
if (hasRefresh) {
|
26
|
+
const refreshHead = removeLineBreaksIfNeeded(
|
27
|
+
`let prevRefreshReg;
|
35
28
|
let prevRefreshSig;
|
36
29
|
|
37
30
|
if (import.meta.hot && !inWebWorker) {
|
38
|
-
if (!window
|
31
|
+
if (!window.$RefreshReg$) {
|
39
32
|
throw new Error(
|
40
|
-
"
|
41
|
-
"See https://github.com/vitejs/vite-plugin-react/pull/11#discussion_r430879201"
|
33
|
+
"${pluginName} can't detect preamble. Something is wrong."
|
42
34
|
);
|
43
35
|
}
|
44
36
|
|
45
37
|
prevRefreshReg = window.$RefreshReg$;
|
46
38
|
prevRefreshSig = window.$RefreshSig$;
|
47
|
-
window.$RefreshReg$ = (
|
48
|
-
RefreshRuntime.register(type, __SOURCE__ + " " + id)
|
49
|
-
};
|
39
|
+
window.$RefreshReg$ = RefreshRuntime.getRefreshReg(${JSON.stringify(id)});
|
50
40
|
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
|
51
|
-
}
|
52
|
-
|
41
|
+
}
|
42
|
+
|
43
|
+
`,
|
44
|
+
avoidSourceMap
|
45
|
+
);
|
46
|
+
newCode = `${refreshHead}${newCode}
|
47
|
+
|
53
48
|
if (import.meta.hot && !inWebWorker) {
|
54
49
|
window.$RefreshReg$ = prevRefreshReg;
|
55
50
|
window.$RefreshSig$ = prevRefreshSig;
|
56
|
-
}
|
57
|
-
|
51
|
+
}
|
52
|
+
`;
|
53
|
+
if (newMap) {
|
54
|
+
newMap.mappings = ";".repeat(16) + newMap.mappings;
|
55
|
+
}
|
56
|
+
}
|
57
|
+
const sharedHead = removeLineBreaksIfNeeded(
|
58
|
+
`import * as RefreshRuntime from "${runtimePublicPath}";
|
59
|
+
const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
|
60
|
+
|
61
|
+
`,
|
62
|
+
avoidSourceMap
|
63
|
+
);
|
64
|
+
newCode = `${sharedHead}${newCode}
|
65
|
+
|
58
66
|
if (import.meta.hot && !inWebWorker) {
|
59
67
|
RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
|
60
68
|
RefreshRuntime.registerExportsForReactRefresh(${JSON.stringify(
|
61
|
-
|
62
|
-
)}, currentExports);
|
69
|
+
id
|
70
|
+
)}, currentExports);
|
63
71
|
import.meta.hot.accept((nextExports) => {
|
64
72
|
if (!nextExports) return;
|
65
73
|
const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(${JSON.stringify(
|
66
|
-
|
67
|
-
)}, currentExports, nextExports);
|
74
|
+
id
|
75
|
+
)}, currentExports, nextExports);
|
68
76
|
if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
|
69
77
|
});
|
70
78
|
});
|
71
|
-
}`;
|
72
|
-
function addRefreshWrapper(code, id) {
|
73
|
-
return sharedHeader + functionHeader.replace("__SOURCE__", JSON.stringify(id)) + code + functionFooter + sharedFooter(id);
|
74
79
|
}
|
75
|
-
|
76
|
-
|
80
|
+
`;
|
81
|
+
if (newMap) {
|
82
|
+
newMap.mappings = ";;;" + newMap.mappings;
|
83
|
+
}
|
84
|
+
return { code: newCode, map: newMap };
|
85
|
+
}
|
86
|
+
function removeLineBreaksIfNeeded(code, enabled) {
|
87
|
+
return enabled ? code.replace(/\n/g, "") : code;
|
77
88
|
}
|
78
89
|
|
90
|
+
const silenceUseClientWarning = (userConfig) => ({
|
91
|
+
rollupOptions: {
|
92
|
+
onwarn(warning, defaultHandler) {
|
93
|
+
if (warning.code === "MODULE_LEVEL_DIRECTIVE" && warning.message.includes("use client")) {
|
94
|
+
return;
|
95
|
+
}
|
96
|
+
if (warning.code === "SOURCEMAP_ERROR" && warning.message.includes("resolve original location") && warning.pos === 0) {
|
97
|
+
return;
|
98
|
+
}
|
99
|
+
if (userConfig.build?.rollupOptions?.onwarn) {
|
100
|
+
userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
|
101
|
+
} else {
|
102
|
+
defaultHandler(warning);
|
103
|
+
}
|
104
|
+
}
|
105
|
+
}
|
106
|
+
});
|
107
|
+
|
108
|
+
const _dirname = dirname(fileURLToPath(import.meta.url));
|
109
|
+
const refreshRuntimePath = join(_dirname, "refresh-runtime.js") ;
|
79
110
|
let babel;
|
80
111
|
async function loadBabel() {
|
81
112
|
if (!babel) {
|
@@ -83,12 +114,9 @@ async function loadBabel() {
|
|
83
114
|
}
|
84
115
|
return babel;
|
85
116
|
}
|
86
|
-
const reactCompRE = /extends\s+(?:React\.)?(?:Pure)?Component/;
|
87
|
-
const refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
|
88
117
|
const defaultIncludeRE = /\.[tj]sx?$/;
|
89
118
|
const tsRE = /\.tsx?$/;
|
90
119
|
function viteReact(opts = {}) {
|
91
|
-
let devBase = "/";
|
92
120
|
const filter = createFilter(opts.include ?? defaultIncludeRE, opts.exclude);
|
93
121
|
const jsxImportSource = opts.jsxImportSource ?? "react";
|
94
122
|
const jsxImportRuntime = `${jsxImportSource}/jsx-runtime`;
|
@@ -120,7 +148,6 @@ function viteReact(opts = {}) {
|
|
120
148
|
}
|
121
149
|
},
|
122
150
|
configResolved(config) {
|
123
|
-
devBase = config.base;
|
124
151
|
projectRoot = config.root;
|
125
152
|
isProduction = config.isProduction;
|
126
153
|
skipFastRefresh = isProduction || config.command === "build" || config.server.hmr === false;
|
@@ -139,15 +166,12 @@ function viteReact(opts = {}) {
|
|
139
166
|
}
|
140
167
|
},
|
141
168
|
async transform(code, id, options) {
|
142
|
-
if (id.includes("/node_modules/"))
|
143
|
-
return;
|
169
|
+
if (id.includes("/node_modules/")) return;
|
144
170
|
const [filepath] = id.split("?");
|
145
|
-
if (!filter(filepath))
|
146
|
-
return;
|
171
|
+
if (!filter(filepath)) return;
|
147
172
|
const ssr = options?.ssr === true;
|
148
173
|
const babelOptions = (() => {
|
149
|
-
if (staticBabelOptions)
|
150
|
-
return staticBabelOptions;
|
174
|
+
if (staticBabelOptions) return staticBabelOptions;
|
151
175
|
const newBabelOptions = createBabelOptions(
|
152
176
|
typeof opts.babel === "function" ? opts.babel(id, { ssr }) : opts.babel
|
153
177
|
);
|
@@ -188,7 +212,7 @@ function viteReact(opts = {}) {
|
|
188
212
|
filename: id,
|
189
213
|
sourceFileName: filepath,
|
190
214
|
// Required for esbuild.jsxDev to provide correct line numbers
|
191
|
-
// This
|
215
|
+
// This creates issues the react compiler because the re-order is too important
|
192
216
|
// People should use @babel/plugin-transform-react-jsx-development to get back good line numbers
|
193
217
|
retainLines: getReactCompilerPlugin(plugins) != null ? false : !isProduction && isJSX && opts.jsxRuntime !== "classic",
|
194
218
|
parserOpts: {
|
@@ -207,15 +231,15 @@ function viteReact(opts = {}) {
|
|
207
231
|
sourceMaps: true
|
208
232
|
});
|
209
233
|
if (result) {
|
210
|
-
|
211
|
-
|
212
|
-
if (refreshContentRE.test(code2)) {
|
213
|
-
code2 = addRefreshWrapper(code2, id);
|
214
|
-
} else if (reactCompRE.test(code2)) {
|
215
|
-
code2 = addClassComponentRefreshWrapper(code2, id);
|
216
|
-
}
|
234
|
+
if (!useFastRefresh) {
|
235
|
+
return { code: result.code, map: result.map };
|
217
236
|
}
|
218
|
-
return
|
237
|
+
return addRefreshWrapper(
|
238
|
+
result.code,
|
239
|
+
result.map,
|
240
|
+
"@vitejs/plugin-react",
|
241
|
+
id
|
242
|
+
);
|
219
243
|
}
|
220
244
|
}
|
221
245
|
};
|
@@ -250,16 +274,19 @@ function viteReact(opts = {}) {
|
|
250
274
|
},
|
251
275
|
load(id) {
|
252
276
|
if (id === runtimePublicPath) {
|
253
|
-
return
|
277
|
+
return readFileSync(refreshRuntimePath, "utf-8").replace(
|
278
|
+
/__README_URL__/g,
|
279
|
+
"https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react"
|
280
|
+
);
|
254
281
|
}
|
255
282
|
},
|
256
|
-
transformIndexHtml() {
|
283
|
+
transformIndexHtml(_, config) {
|
257
284
|
if (!skipFastRefresh)
|
258
285
|
return [
|
259
286
|
{
|
260
287
|
tag: "script",
|
261
288
|
attrs: { type: "module" },
|
262
|
-
children:
|
289
|
+
children: getPreambleCode(config.server.config.base)
|
263
290
|
}
|
264
291
|
];
|
265
292
|
}
|
@@ -267,28 +294,10 @@ function viteReact(opts = {}) {
|
|
267
294
|
return [viteBabel, viteReactRefresh];
|
268
295
|
}
|
269
296
|
viteReact.preambleCode = preambleCode;
|
270
|
-
const silenceUseClientWarning = (userConfig) => ({
|
271
|
-
rollupOptions: {
|
272
|
-
onwarn(warning, defaultHandler) {
|
273
|
-
if (warning.code === "MODULE_LEVEL_DIRECTIVE" && warning.message.includes("use client")) {
|
274
|
-
return;
|
275
|
-
}
|
276
|
-
if (warning.code === "SOURCEMAP_ERROR" && warning.message.includes("resolve original location") && warning.pos === 0) {
|
277
|
-
return;
|
278
|
-
}
|
279
|
-
if (userConfig.build?.rollupOptions?.onwarn) {
|
280
|
-
userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
|
281
|
-
} else {
|
282
|
-
defaultHandler(warning);
|
283
|
-
}
|
284
|
-
}
|
285
|
-
}
|
286
|
-
});
|
287
297
|
const loadedPlugin = /* @__PURE__ */ new Map();
|
288
298
|
function loadPlugin(path) {
|
289
299
|
const cached = loadedPlugin.get(path);
|
290
|
-
if (cached)
|
291
|
-
return cached;
|
300
|
+
if (cached) return cached;
|
292
301
|
const promise = import(path).then((module) => {
|
293
302
|
const value = module.default || module;
|
294
303
|
loadedPlugin.set(path, value);
|
@@ -298,17 +307,16 @@ function loadPlugin(path) {
|
|
298
307
|
return promise;
|
299
308
|
}
|
300
309
|
function createBabelOptions(rawOptions) {
|
301
|
-
var _a;
|
302
310
|
const babelOptions = {
|
303
311
|
babelrc: false,
|
304
312
|
configFile: false,
|
305
313
|
...rawOptions
|
306
314
|
};
|
307
|
-
babelOptions.plugins
|
308
|
-
babelOptions.presets
|
309
|
-
babelOptions.overrides
|
310
|
-
babelOptions.parserOpts
|
311
|
-
|
315
|
+
babelOptions.plugins ||= [];
|
316
|
+
babelOptions.presets ||= [];
|
317
|
+
babelOptions.overrides ||= [];
|
318
|
+
babelOptions.parserOpts ||= {};
|
319
|
+
babelOptions.parserOpts.plugins ||= [];
|
312
320
|
return babelOptions;
|
313
321
|
}
|
314
322
|
function defined(value) {
|