@vitejs/plugin-react-swc 4.0.1 → 4.1.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/index.js +31 -50
- package/package.json +3 -3
- package/refresh-runtime.js +1 -8
package/index.js
CHANGED
|
@@ -15,22 +15,15 @@ injectIntoGlobalHook(window);
|
|
|
15
15
|
window.$RefreshReg$ = () => {};
|
|
16
16
|
window.$RefreshSig$ = () => (type) => type;`;
|
|
17
17
|
const getPreambleCode = (base) => preambleCode.replace("__BASE__", base);
|
|
18
|
-
|
|
19
|
-
function addRefreshWrapper(code, map, pluginName, id, reactRefreshHost = "") {
|
|
18
|
+
function addRefreshWrapper(code, pluginName, id, reactRefreshHost = "") {
|
|
20
19
|
const hasRefresh = refreshContentRE.test(code);
|
|
21
20
|
const onlyReactComp = !hasRefresh && reactCompRE.test(code);
|
|
22
|
-
|
|
23
|
-
if (!hasRefresh && !onlyReactComp) return {
|
|
24
|
-
code,
|
|
25
|
-
map: normalizedMap
|
|
26
|
-
};
|
|
27
|
-
const avoidSourceMap = map === avoidSourceMapOption;
|
|
28
|
-
const newMap = typeof normalizedMap === "string" ? JSON.parse(normalizedMap) : normalizedMap;
|
|
21
|
+
if (!hasRefresh && !onlyReactComp) return void 0;
|
|
29
22
|
let newCode = code;
|
|
30
|
-
|
|
31
|
-
const refreshHead = removeLineBreaksIfNeeded(`let prevRefreshReg;
|
|
32
|
-
let prevRefreshSig;
|
|
23
|
+
newCode += `
|
|
33
24
|
|
|
25
|
+
import * as RefreshRuntime from "${reactRefreshHost}${runtimePublicPath}";
|
|
26
|
+
const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
|
|
34
27
|
if (import.meta.hot && !inWebWorker) {
|
|
35
28
|
if (!window.$RefreshReg$) {
|
|
36
29
|
throw new Error(
|
|
@@ -38,29 +31,6 @@ if (import.meta.hot && !inWebWorker) {
|
|
|
38
31
|
);
|
|
39
32
|
}
|
|
40
33
|
|
|
41
|
-
prevRefreshReg = window.$RefreshReg$;
|
|
42
|
-
prevRefreshSig = window.$RefreshSig$;
|
|
43
|
-
window.$RefreshReg$ = RefreshRuntime.getRefreshReg(${JSON.stringify(id)});
|
|
44
|
-
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
`, avoidSourceMap);
|
|
48
|
-
newCode = `${refreshHead}${newCode}
|
|
49
|
-
|
|
50
|
-
if (import.meta.hot && !inWebWorker) {
|
|
51
|
-
window.$RefreshReg$ = prevRefreshReg;
|
|
52
|
-
window.$RefreshSig$ = prevRefreshSig;
|
|
53
|
-
}
|
|
54
|
-
`;
|
|
55
|
-
if (newMap) newMap.mappings = ";".repeat(16) + newMap.mappings;
|
|
56
|
-
}
|
|
57
|
-
const sharedHead = removeLineBreaksIfNeeded(`import * as RefreshRuntime from "${reactRefreshHost}${runtimePublicPath}";
|
|
58
|
-
const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
|
|
59
|
-
|
|
60
|
-
`, avoidSourceMap);
|
|
61
|
-
newCode = `${sharedHead}${newCode}
|
|
62
|
-
|
|
63
|
-
if (import.meta.hot && !inWebWorker) {
|
|
64
34
|
RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
|
|
65
35
|
RefreshRuntime.registerExportsForReactRefresh(${JSON.stringify(id)}, currentExports);
|
|
66
36
|
import.meta.hot.accept((nextExports) => {
|
|
@@ -71,14 +41,10 @@ if (import.meta.hot && !inWebWorker) {
|
|
|
71
41
|
});
|
|
72
42
|
}
|
|
73
43
|
`;
|
|
74
|
-
if (
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
function removeLineBreaksIfNeeded(code, enabled) {
|
|
81
|
-
return enabled ? code.replace(/\n/g, "") : code;
|
|
44
|
+
if (hasRefresh) newCode += `function $RefreshReg$(type, id) { return RefreshRuntime.register(type, ${JSON.stringify(id)} + ' ' + id) }
|
|
45
|
+
function $RefreshSig$() { return RefreshRuntime.createSignatureFunctionForTransform(); }
|
|
46
|
+
`;
|
|
47
|
+
return newCode;
|
|
82
48
|
}
|
|
83
49
|
|
|
84
50
|
//#endregion
|
|
@@ -96,6 +62,7 @@ const _dirname = typeof __dirname !== "undefined" ? __dirname : dirname(fileURLT
|
|
|
96
62
|
const resolve = createRequire(typeof __filename !== "undefined" ? __filename : import.meta.url).resolve;
|
|
97
63
|
const react = (_options) => {
|
|
98
64
|
let hmrDisabled = false;
|
|
65
|
+
let viteCacheRoot;
|
|
99
66
|
const options = {
|
|
100
67
|
jsxImportSource: _options?.jsxImportSource ?? "react",
|
|
101
68
|
tsDecorators: _options?.tsDecorators,
|
|
@@ -132,6 +99,7 @@ const react = (_options) => {
|
|
|
132
99
|
}
|
|
133
100
|
}),
|
|
134
101
|
configResolved(config) {
|
|
102
|
+
viteCacheRoot = config.cacheDir;
|
|
135
103
|
if (config.server.hmr === false) hmrDisabled = true;
|
|
136
104
|
const mdxIndex = config.plugins.findIndex((p) => p.name === "@mdx-js/rollup");
|
|
137
105
|
if (mdxIndex !== -1 && mdxIndex > config.plugins.findIndex((p) => p.name === "vite:react-swc")) throw new Error("[vite:react-swc] The MDX plugin should be placed before this plugin");
|
|
@@ -147,7 +115,7 @@ const react = (_options) => {
|
|
|
147
115
|
async transform(code, _id, transformOptions) {
|
|
148
116
|
const id = _id.split("?")[0];
|
|
149
117
|
const refresh = !transformOptions?.ssr && !hmrDisabled;
|
|
150
|
-
const result = await transformWithOptions(id, code, options.devTarget, options, {
|
|
118
|
+
const result = await transformWithOptions(id, code, options.devTarget, options, viteCacheRoot, {
|
|
151
119
|
refresh,
|
|
152
120
|
development: true,
|
|
153
121
|
runtime: "automatic",
|
|
@@ -155,7 +123,10 @@ const react = (_options) => {
|
|
|
155
123
|
});
|
|
156
124
|
if (!result) return;
|
|
157
125
|
if (!refresh) return result;
|
|
158
|
-
return
|
|
126
|
+
return {
|
|
127
|
+
code: addRefreshWrapper(result.code, "@vitejs/plugin-react-swc", id, options.reactRefreshHost) ?? result.code,
|
|
128
|
+
map: result.map
|
|
129
|
+
};
|
|
159
130
|
}
|
|
160
131
|
},
|
|
161
132
|
options.plugins ? {
|
|
@@ -163,7 +134,10 @@ const react = (_options) => {
|
|
|
163
134
|
apply: "build",
|
|
164
135
|
enforce: "pre",
|
|
165
136
|
config: (userConfig) => ({ build: silenceUseClientWarning(userConfig) }),
|
|
166
|
-
|
|
137
|
+
configResolved(config) {
|
|
138
|
+
viteCacheRoot = config.cacheDir;
|
|
139
|
+
},
|
|
140
|
+
transform: (code, _id) => transformWithOptions(_id.split("?")[0], code, "esnext", options, viteCacheRoot, {
|
|
167
141
|
runtime: "automatic",
|
|
168
142
|
importSource: options.jsxImportSource
|
|
169
143
|
})
|
|
@@ -177,11 +151,14 @@ const react = (_options) => {
|
|
|
177
151
|
jsxImportSource: options.jsxImportSource,
|
|
178
152
|
tsconfigRaw: { compilerOptions: { useDefineForClassFields: true } }
|
|
179
153
|
}
|
|
180
|
-
})
|
|
154
|
+
}),
|
|
155
|
+
configResolved(config) {
|
|
156
|
+
viteCacheRoot = config.cacheDir;
|
|
157
|
+
}
|
|
181
158
|
}
|
|
182
159
|
];
|
|
183
160
|
};
|
|
184
|
-
const transformWithOptions = async (id, code, target, options, reactConfig) => {
|
|
161
|
+
const transformWithOptions = async (id, code, target, options, viteCacheRoot, reactConfig) => {
|
|
185
162
|
const decorators = options?.tsDecorators ?? false;
|
|
186
163
|
const parser = options.parserConfig ? options.parserConfig(id) : id.endsWith(".tsx") ? {
|
|
187
164
|
syntax: "typescript",
|
|
@@ -209,7 +186,10 @@ const transformWithOptions = async (id, code, target, options, reactConfig) => {
|
|
|
209
186
|
jsc: {
|
|
210
187
|
target,
|
|
211
188
|
parser,
|
|
212
|
-
experimental: {
|
|
189
|
+
experimental: {
|
|
190
|
+
plugins: options.plugins,
|
|
191
|
+
cacheRoot: join(viteCacheRoot ?? "node_modules/.vite", ".swc")
|
|
192
|
+
},
|
|
213
193
|
transform: {
|
|
214
194
|
useDefineForClassFields: true,
|
|
215
195
|
react: reactConfig
|
|
@@ -232,10 +212,11 @@ const transformWithOptions = async (id, code, target, options, reactConfig) => {
|
|
|
232
212
|
}
|
|
233
213
|
return result;
|
|
234
214
|
};
|
|
215
|
+
var src_default = react;
|
|
235
216
|
function pluginForCjs(options) {
|
|
236
217
|
return react.call(this, options);
|
|
237
218
|
}
|
|
238
219
|
Object.assign(pluginForCjs, { default: pluginForCjs });
|
|
239
220
|
|
|
240
221
|
//#endregion
|
|
241
|
-
export {
|
|
222
|
+
export { src_default as default, pluginForCjs as "module.exports" };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitejs/plugin-react-swc",
|
|
3
|
-
"version": "4.0
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Arnaud Barré (https://github.com/ArnaudBarre)",
|
|
6
6
|
"description": "Speed up your Vite dev server with SWC",
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react-swc#readme",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@rolldown/pluginutils": "1.0.0-beta.
|
|
30
|
-
"@swc/core": "^1.13.
|
|
29
|
+
"@rolldown/pluginutils": "1.0.0-beta.35",
|
|
30
|
+
"@swc/core": "^1.13.5"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
33
|
"vite": "^4 || ^5 || ^6 || ^7"
|
package/refresh-runtime.js
CHANGED
|
@@ -243,7 +243,7 @@ function performReactRefresh() {
|
|
|
243
243
|
}
|
|
244
244
|
}
|
|
245
245
|
|
|
246
|
-
function register(type, id) {
|
|
246
|
+
export function register(type, id) {
|
|
247
247
|
if (type === null) {
|
|
248
248
|
return
|
|
249
249
|
}
|
|
@@ -564,10 +564,6 @@ function isPlainObject(obj) {
|
|
|
564
564
|
* Plugin utils
|
|
565
565
|
*/
|
|
566
566
|
|
|
567
|
-
export function getRefreshReg(filename) {
|
|
568
|
-
return (type, id) => register(type, filename + ' ' + id)
|
|
569
|
-
}
|
|
570
|
-
|
|
571
567
|
// Taken from https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/lib/runtime/RefreshUtils.js#L141
|
|
572
568
|
// This allows to resister components not detected by SWC like styled component
|
|
573
569
|
export function registerExportsForReactRefresh(filename, moduleExports) {
|
|
@@ -653,10 +649,7 @@ export function validateRefreshBoundaryAndEnqueueUpdate(
|
|
|
653
649
|
|
|
654
650
|
function predicateOnExport(ignoredExports, moduleExports, predicate) {
|
|
655
651
|
for (const key in moduleExports) {
|
|
656
|
-
if (key === '__esModule') continue
|
|
657
652
|
if (ignoredExports.includes(key)) continue
|
|
658
|
-
const desc = Object.getOwnPropertyDescriptor(moduleExports, key)
|
|
659
|
-
if (desc && desc.get) return key
|
|
660
653
|
if (!predicate(key, moduleExports[key])) return key
|
|
661
654
|
}
|
|
662
655
|
return true
|