@vitejs/plugin-react-swc 3.1.0 → 3.3.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.cjs +64 -36
- package/index.mjs +67 -37
- package/package.json +2 -2
- package/refresh-runtime.js +11 -1
package/index.cjs
CHANGED
|
@@ -17,20 +17,28 @@ var resolve = (0, import_module.createRequire)(
|
|
|
17
17
|
var refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
|
|
18
18
|
var react = (_options) => {
|
|
19
19
|
const options = {
|
|
20
|
-
jsxImportSource: _options == null ? void 0 : _options.jsxImportSource,
|
|
20
|
+
jsxImportSource: (_options == null ? void 0 : _options.jsxImportSource) ?? "react",
|
|
21
21
|
tsDecorators: _options == null ? void 0 : _options.tsDecorators,
|
|
22
22
|
plugins: (_options == null ? void 0 : _options.plugins) ? _options == null ? void 0 : _options.plugins.map((el) => [resolve(el[0]), el[1]]) : void 0
|
|
23
23
|
};
|
|
24
24
|
return [
|
|
25
|
+
{
|
|
26
|
+
name: "vite:react-swc:resolve-runtime",
|
|
27
|
+
apply: "serve",
|
|
28
|
+
enforce: "pre",
|
|
29
|
+
// Run before Vite default resolve to avoid syscalls
|
|
30
|
+
resolveId: (id) => id === runtimePublicPath ? id : void 0,
|
|
31
|
+
load: (id) => id === runtimePublicPath ? (0, import_fs.readFileSync)((0, import_path.join)(_dirname, "refresh-runtime.js"), "utf-8") : void 0
|
|
32
|
+
},
|
|
25
33
|
{
|
|
26
34
|
name: "vite:react-swc",
|
|
27
35
|
apply: "serve",
|
|
28
36
|
config: () => ({
|
|
29
37
|
esbuild: false,
|
|
30
|
-
optimizeDeps: {
|
|
38
|
+
optimizeDeps: {
|
|
39
|
+
include: [`${options.jsxImportSource}/jsx-dev-runtime`]
|
|
40
|
+
}
|
|
31
41
|
}),
|
|
32
|
-
resolveId: (id) => id === runtimePublicPath ? id : void 0,
|
|
33
|
-
load: (id) => id === runtimePublicPath ? (0, import_fs.readFileSync)((0, import_path.join)(_dirname, "refresh-runtime.js"), "utf-8") : void 0,
|
|
34
42
|
transformIndexHtml: (_, config) => [
|
|
35
43
|
{
|
|
36
44
|
tag: "script",
|
|
@@ -43,12 +51,11 @@ var react = (_options) => {
|
|
|
43
51
|
],
|
|
44
52
|
async transform(code, _id, transformOptions) {
|
|
45
53
|
const id = _id.split("?")[0];
|
|
46
|
-
const result = await transformWithOptions(id, code, options, {
|
|
54
|
+
const result = await transformWithOptions(id, code, "es2020", options, {
|
|
47
55
|
refresh: !(transformOptions == null ? void 0 : transformOptions.ssr),
|
|
48
56
|
development: true,
|
|
49
|
-
useBuiltins: true,
|
|
50
57
|
runtime: "automatic",
|
|
51
|
-
importSource: options
|
|
58
|
+
importSource: options.jsxImportSource
|
|
52
59
|
});
|
|
53
60
|
if (!result)
|
|
54
61
|
return;
|
|
@@ -56,26 +63,26 @@ var react = (_options) => {
|
|
|
56
63
|
return result;
|
|
57
64
|
}
|
|
58
65
|
result.code = `import * as RefreshRuntime from "${runtimePublicPath}";
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
});
|
|
66
|
+
|
|
67
|
+
if (!window.$RefreshReg$) throw new Error("React refresh preamble was not loaded. Something is wrong.");
|
|
68
|
+
const prevRefreshReg = window.$RefreshReg$;
|
|
69
|
+
const prevRefreshSig = window.$RefreshSig$;
|
|
70
|
+
window.$RefreshReg$ = RefreshRuntime.getRefreshReg("${id}");
|
|
71
|
+
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
|
|
72
|
+
|
|
73
|
+
${result.code}
|
|
74
|
+
|
|
75
|
+
window.$RefreshReg$ = prevRefreshReg;
|
|
76
|
+
window.$RefreshSig$ = prevRefreshSig;
|
|
77
|
+
RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
|
|
78
|
+
RefreshRuntime.registerExportsForReactRefresh("${id}", currentExports);
|
|
79
|
+
import.meta.hot.accept((nextExports) => {
|
|
80
|
+
if (!nextExports) return;
|
|
81
|
+
const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(currentExports, nextExports);
|
|
82
|
+
if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
|
|
77
83
|
});
|
|
78
|
-
|
|
84
|
+
});
|
|
85
|
+
`;
|
|
79
86
|
const sourceMap = JSON.parse(result.map);
|
|
80
87
|
sourceMap.mappings = ";;;;;;;;" + sourceMap.mappings;
|
|
81
88
|
return { code: result.code, map: sourceMap };
|
|
@@ -84,18 +91,23 @@ var react = (_options) => {
|
|
|
84
91
|
options.plugins ? {
|
|
85
92
|
name: "vite:react-swc",
|
|
86
93
|
apply: "build",
|
|
87
|
-
|
|
88
|
-
|
|
94
|
+
enforce: "pre",
|
|
95
|
+
// Run before esbuild
|
|
96
|
+
config: (userConfig) => ({
|
|
97
|
+
build: silenceUseClientWarning(userConfig)
|
|
98
|
+
}),
|
|
99
|
+
transform: (code, _id) => transformWithOptions(_id.split("?")[0], code, "esnext", options, {
|
|
89
100
|
runtime: "automatic",
|
|
90
|
-
importSource: options
|
|
101
|
+
importSource: options.jsxImportSource
|
|
91
102
|
})
|
|
92
103
|
} : {
|
|
93
104
|
name: "vite:react-swc",
|
|
94
105
|
apply: "build",
|
|
95
|
-
config: () => ({
|
|
106
|
+
config: (userConfig) => ({
|
|
107
|
+
build: silenceUseClientWarning(userConfig),
|
|
96
108
|
esbuild: {
|
|
97
109
|
jsx: "automatic",
|
|
98
|
-
jsxImportSource: options
|
|
110
|
+
jsxImportSource: options.jsxImportSource,
|
|
99
111
|
tsconfigRaw: {
|
|
100
112
|
compilerOptions: { useDefineForClassFields: true }
|
|
101
113
|
}
|
|
@@ -104,11 +116,12 @@ var react = (_options) => {
|
|
|
104
116
|
}
|
|
105
117
|
];
|
|
106
118
|
};
|
|
107
|
-
var transformWithOptions = async (id, code, options, reactConfig) => {
|
|
108
|
-
if (id.includes("node_modules"))
|
|
109
|
-
return;
|
|
119
|
+
var transformWithOptions = async (id, code, target, options, reactConfig) => {
|
|
110
120
|
const decorators = (options == null ? void 0 : options.tsDecorators) ?? false;
|
|
111
|
-
const parser = id.endsWith(".tsx") ? { syntax: "typescript", tsx: true, decorators } : id.endsWith(".ts") ? { syntax: "typescript", tsx: false, decorators } : id.endsWith(".jsx") ? { syntax: "ecmascript", jsx: true } :
|
|
121
|
+
const parser = id.endsWith(".tsx") ? { syntax: "typescript", tsx: true, decorators } : id.endsWith(".ts") ? { syntax: "typescript", tsx: false, decorators } : id.endsWith(".jsx") ? { syntax: "ecmascript", jsx: true } : id.endsWith(".mdx") ? (
|
|
122
|
+
// JSX is required to trigger fast refresh transformations, even if MDX already transforms it
|
|
123
|
+
{ syntax: "ecmascript", jsx: true }
|
|
124
|
+
) : void 0;
|
|
112
125
|
if (!parser)
|
|
113
126
|
return;
|
|
114
127
|
let result;
|
|
@@ -119,7 +132,7 @@ var transformWithOptions = async (id, code, options, reactConfig) => {
|
|
|
119
132
|
configFile: false,
|
|
120
133
|
sourceMaps: true,
|
|
121
134
|
jsc: {
|
|
122
|
-
target
|
|
135
|
+
target,
|
|
123
136
|
parser,
|
|
124
137
|
experimental: { plugins: options.plugins },
|
|
125
138
|
transform: {
|
|
@@ -142,6 +155,21 @@ var transformWithOptions = async (id, code, options, reactConfig) => {
|
|
|
142
155
|
}
|
|
143
156
|
return result;
|
|
144
157
|
};
|
|
158
|
+
var silenceUseClientWarning = (userConfig) => ({
|
|
159
|
+
rollupOptions: {
|
|
160
|
+
onwarn(warning, defaultHandler) {
|
|
161
|
+
var _a, _b;
|
|
162
|
+
if (warning.code === "MODULE_LEVEL_DIRECTIVE" && warning.message.includes("use client")) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if ((_b = (_a = userConfig.build) == null ? void 0 : _a.rollupOptions) == null ? void 0 : _b.onwarn) {
|
|
166
|
+
userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
|
|
167
|
+
} else {
|
|
168
|
+
defaultHandler(warning);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
});
|
|
145
173
|
var src_default = react;
|
|
146
174
|
|
|
147
175
|
// <stdin>
|
package/index.mjs
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
import { readFileSync } from "fs";
|
|
3
3
|
import { dirname, join } from "path";
|
|
4
4
|
import { fileURLToPath } from "url";
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
transform
|
|
7
|
+
} from "@swc/core";
|
|
6
8
|
import { createRequire } from "module";
|
|
7
9
|
var runtimePublicPath = "/@react-refresh";
|
|
8
10
|
var preambleCode = `import { injectIntoGlobalHook } from "__PATH__";
|
|
@@ -16,20 +18,28 @@ var resolve = createRequire(
|
|
|
16
18
|
var refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
|
|
17
19
|
var react = (_options) => {
|
|
18
20
|
const options = {
|
|
19
|
-
jsxImportSource: _options == null ? void 0 : _options.jsxImportSource,
|
|
21
|
+
jsxImportSource: (_options == null ? void 0 : _options.jsxImportSource) ?? "react",
|
|
20
22
|
tsDecorators: _options == null ? void 0 : _options.tsDecorators,
|
|
21
23
|
plugins: (_options == null ? void 0 : _options.plugins) ? _options == null ? void 0 : _options.plugins.map((el) => [resolve(el[0]), el[1]]) : void 0
|
|
22
24
|
};
|
|
23
25
|
return [
|
|
26
|
+
{
|
|
27
|
+
name: "vite:react-swc:resolve-runtime",
|
|
28
|
+
apply: "serve",
|
|
29
|
+
enforce: "pre",
|
|
30
|
+
// Run before Vite default resolve to avoid syscalls
|
|
31
|
+
resolveId: (id) => id === runtimePublicPath ? id : void 0,
|
|
32
|
+
load: (id) => id === runtimePublicPath ? readFileSync(join(_dirname, "refresh-runtime.js"), "utf-8") : void 0
|
|
33
|
+
},
|
|
24
34
|
{
|
|
25
35
|
name: "vite:react-swc",
|
|
26
36
|
apply: "serve",
|
|
27
37
|
config: () => ({
|
|
28
38
|
esbuild: false,
|
|
29
|
-
optimizeDeps: {
|
|
39
|
+
optimizeDeps: {
|
|
40
|
+
include: [`${options.jsxImportSource}/jsx-dev-runtime`]
|
|
41
|
+
}
|
|
30
42
|
}),
|
|
31
|
-
resolveId: (id) => id === runtimePublicPath ? id : void 0,
|
|
32
|
-
load: (id) => id === runtimePublicPath ? readFileSync(join(_dirname, "refresh-runtime.js"), "utf-8") : void 0,
|
|
33
43
|
transformIndexHtml: (_, config) => [
|
|
34
44
|
{
|
|
35
45
|
tag: "script",
|
|
@@ -42,12 +52,11 @@ var react = (_options) => {
|
|
|
42
52
|
],
|
|
43
53
|
async transform(code, _id, transformOptions) {
|
|
44
54
|
const id = _id.split("?")[0];
|
|
45
|
-
const result = await transformWithOptions(id, code, options, {
|
|
55
|
+
const result = await transformWithOptions(id, code, "es2020", options, {
|
|
46
56
|
refresh: !(transformOptions == null ? void 0 : transformOptions.ssr),
|
|
47
57
|
development: true,
|
|
48
|
-
useBuiltins: true,
|
|
49
58
|
runtime: "automatic",
|
|
50
|
-
importSource: options
|
|
59
|
+
importSource: options.jsxImportSource
|
|
51
60
|
});
|
|
52
61
|
if (!result)
|
|
53
62
|
return;
|
|
@@ -55,26 +64,26 @@ var react = (_options) => {
|
|
|
55
64
|
return result;
|
|
56
65
|
}
|
|
57
66
|
result.code = `import * as RefreshRuntime from "${runtimePublicPath}";
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
});
|
|
67
|
+
|
|
68
|
+
if (!window.$RefreshReg$) throw new Error("React refresh preamble was not loaded. Something is wrong.");
|
|
69
|
+
const prevRefreshReg = window.$RefreshReg$;
|
|
70
|
+
const prevRefreshSig = window.$RefreshSig$;
|
|
71
|
+
window.$RefreshReg$ = RefreshRuntime.getRefreshReg("${id}");
|
|
72
|
+
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
|
|
73
|
+
|
|
74
|
+
${result.code}
|
|
75
|
+
|
|
76
|
+
window.$RefreshReg$ = prevRefreshReg;
|
|
77
|
+
window.$RefreshSig$ = prevRefreshSig;
|
|
78
|
+
RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
|
|
79
|
+
RefreshRuntime.registerExportsForReactRefresh("${id}", currentExports);
|
|
80
|
+
import.meta.hot.accept((nextExports) => {
|
|
81
|
+
if (!nextExports) return;
|
|
82
|
+
const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(currentExports, nextExports);
|
|
83
|
+
if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
|
|
76
84
|
});
|
|
77
|
-
|
|
85
|
+
});
|
|
86
|
+
`;
|
|
78
87
|
const sourceMap = JSON.parse(result.map);
|
|
79
88
|
sourceMap.mappings = ";;;;;;;;" + sourceMap.mappings;
|
|
80
89
|
return { code: result.code, map: sourceMap };
|
|
@@ -83,18 +92,23 @@ var react = (_options) => {
|
|
|
83
92
|
options.plugins ? {
|
|
84
93
|
name: "vite:react-swc",
|
|
85
94
|
apply: "build",
|
|
86
|
-
|
|
87
|
-
|
|
95
|
+
enforce: "pre",
|
|
96
|
+
// Run before esbuild
|
|
97
|
+
config: (userConfig) => ({
|
|
98
|
+
build: silenceUseClientWarning(userConfig)
|
|
99
|
+
}),
|
|
100
|
+
transform: (code, _id) => transformWithOptions(_id.split("?")[0], code, "esnext", options, {
|
|
88
101
|
runtime: "automatic",
|
|
89
|
-
importSource: options
|
|
102
|
+
importSource: options.jsxImportSource
|
|
90
103
|
})
|
|
91
104
|
} : {
|
|
92
105
|
name: "vite:react-swc",
|
|
93
106
|
apply: "build",
|
|
94
|
-
config: () => ({
|
|
107
|
+
config: (userConfig) => ({
|
|
108
|
+
build: silenceUseClientWarning(userConfig),
|
|
95
109
|
esbuild: {
|
|
96
110
|
jsx: "automatic",
|
|
97
|
-
jsxImportSource: options
|
|
111
|
+
jsxImportSource: options.jsxImportSource,
|
|
98
112
|
tsconfigRaw: {
|
|
99
113
|
compilerOptions: { useDefineForClassFields: true }
|
|
100
114
|
}
|
|
@@ -103,11 +117,12 @@ var react = (_options) => {
|
|
|
103
117
|
}
|
|
104
118
|
];
|
|
105
119
|
};
|
|
106
|
-
var transformWithOptions = async (id, code, options, reactConfig) => {
|
|
107
|
-
if (id.includes("node_modules"))
|
|
108
|
-
return;
|
|
120
|
+
var transformWithOptions = async (id, code, target, options, reactConfig) => {
|
|
109
121
|
const decorators = (options == null ? void 0 : options.tsDecorators) ?? false;
|
|
110
|
-
const parser = id.endsWith(".tsx") ? { syntax: "typescript", tsx: true, decorators } : id.endsWith(".ts") ? { syntax: "typescript", tsx: false, decorators } : id.endsWith(".jsx") ? { syntax: "ecmascript", jsx: true } :
|
|
122
|
+
const parser = id.endsWith(".tsx") ? { syntax: "typescript", tsx: true, decorators } : id.endsWith(".ts") ? { syntax: "typescript", tsx: false, decorators } : id.endsWith(".jsx") ? { syntax: "ecmascript", jsx: true } : id.endsWith(".mdx") ? (
|
|
123
|
+
// JSX is required to trigger fast refresh transformations, even if MDX already transforms it
|
|
124
|
+
{ syntax: "ecmascript", jsx: true }
|
|
125
|
+
) : void 0;
|
|
111
126
|
if (!parser)
|
|
112
127
|
return;
|
|
113
128
|
let result;
|
|
@@ -118,7 +133,7 @@ var transformWithOptions = async (id, code, options, reactConfig) => {
|
|
|
118
133
|
configFile: false,
|
|
119
134
|
sourceMaps: true,
|
|
120
135
|
jsc: {
|
|
121
|
-
target
|
|
136
|
+
target,
|
|
122
137
|
parser,
|
|
123
138
|
experimental: { plugins: options.plugins },
|
|
124
139
|
transform: {
|
|
@@ -141,6 +156,21 @@ var transformWithOptions = async (id, code, options, reactConfig) => {
|
|
|
141
156
|
}
|
|
142
157
|
return result;
|
|
143
158
|
};
|
|
159
|
+
var silenceUseClientWarning = (userConfig) => ({
|
|
160
|
+
rollupOptions: {
|
|
161
|
+
onwarn(warning, defaultHandler) {
|
|
162
|
+
var _a, _b;
|
|
163
|
+
if (warning.code === "MODULE_LEVEL_DIRECTIVE" && warning.message.includes("use client")) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if ((_b = (_a = userConfig.build) == null ? void 0 : _a.rollupOptions) == null ? void 0 : _b.onwarn) {
|
|
167
|
+
userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
|
|
168
|
+
} else {
|
|
169
|
+
defaultHandler(warning);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
});
|
|
144
174
|
var src_default = react;
|
|
145
175
|
export {
|
|
146
176
|
src_default as default
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitejs/plugin-react-swc",
|
|
3
3
|
"description": "Speed up your Vite dev server with SWC",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.3.0",
|
|
5
5
|
"author": "Arnaud Barré (https://github.com/ArnaudBarre)",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "github:vitejs/vite-plugin-react-swc",
|
|
@@ -27,6 +27,6 @@
|
|
|
27
27
|
"vite": "^4"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@swc/core": "^1.3.
|
|
30
|
+
"@swc/core": "^1.3.42"
|
|
31
31
|
}
|
|
32
32
|
}
|
package/refresh-runtime.js
CHANGED
|
@@ -364,6 +364,11 @@ function isLikelyComponentType(type) {
|
|
|
364
364
|
}
|
|
365
365
|
}
|
|
366
366
|
}
|
|
367
|
+
if (window.$RefreshReg$) {
|
|
368
|
+
throw new Error(
|
|
369
|
+
"React refresh runtime was loaded twice. Maybe you forgot the base path?"
|
|
370
|
+
);
|
|
371
|
+
}
|
|
367
372
|
function getRefreshReg(filename) {
|
|
368
373
|
return (type, id) => register(type, filename + " " + id);
|
|
369
374
|
}
|
|
@@ -373,7 +378,7 @@ function registerExportsForReactRefresh(filename, moduleExports) {
|
|
|
373
378
|
continue;
|
|
374
379
|
const exportValue = moduleExports[key];
|
|
375
380
|
if (isLikelyComponentType(exportValue)) {
|
|
376
|
-
register(exportValue, filename + " " + key);
|
|
381
|
+
register(exportValue, filename + " export " + key);
|
|
377
382
|
}
|
|
378
383
|
}
|
|
379
384
|
}
|
|
@@ -419,8 +424,13 @@ function predicateOnExport(moduleExports, predicate) {
|
|
|
419
424
|
}
|
|
420
425
|
return true;
|
|
421
426
|
}
|
|
427
|
+
const __hmr_import = (module) => import(
|
|
428
|
+
/* @vite-ignore */
|
|
429
|
+
module
|
|
430
|
+
);
|
|
422
431
|
var refresh_runtime_default = { injectIntoGlobalHook };
|
|
423
432
|
export {
|
|
433
|
+
__hmr_import,
|
|
424
434
|
createSignatureFunctionForTransform,
|
|
425
435
|
refresh_runtime_default as default,
|
|
426
436
|
getRefreshReg,
|