@vitejs/plugin-react-swc 3.3.2 → 3.4.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 +16 -0
- package/index.cjs +28 -11
- package/index.d.ts +7 -0
- package/index.mjs +28 -11
- package/package.json +2 -2
- package/refresh-runtime.js +4 -3
package/README.md
CHANGED
|
@@ -40,6 +40,8 @@ This plugin has limited options to enable good performances and be transpiler ag
|
|
|
40
40
|
|
|
41
41
|
Control where the JSX factory is imported from.
|
|
42
42
|
|
|
43
|
+
`@default` "react"
|
|
44
|
+
|
|
43
45
|
```ts
|
|
44
46
|
react({ jsxImportSource: "@emotion/react" });
|
|
45
47
|
```
|
|
@@ -48,6 +50,8 @@ react({ jsxImportSource: "@emotion/react" });
|
|
|
48
50
|
|
|
49
51
|
Enable TypeScript decorators. Requires `experimentalDecorators` in tsconfig.
|
|
50
52
|
|
|
53
|
+
`@default` false
|
|
54
|
+
|
|
51
55
|
```ts
|
|
52
56
|
react({ tsDecorators: true });
|
|
53
57
|
```
|
|
@@ -60,6 +64,18 @@ Use SWC plugins. Enable SWC at build time.
|
|
|
60
64
|
react({ plugins: [["@swc/plugin-styled-components", {}]] });
|
|
61
65
|
```
|
|
62
66
|
|
|
67
|
+
## devTarget
|
|
68
|
+
|
|
69
|
+
Set the target for SWC in dev. This can avoid to down-transpile private class method for example.
|
|
70
|
+
|
|
71
|
+
For production target, see https://vitejs.dev/config/build-options.html#build-target.
|
|
72
|
+
|
|
73
|
+
`@default` "es2020"
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
react({ devTarget: "es2022" });
|
|
77
|
+
```
|
|
78
|
+
|
|
63
79
|
## Consistent components exports
|
|
64
80
|
|
|
65
81
|
For React refresh to work correctly, your file should only export React components. The best explanation I've read is the one from the [Gatsby docs](https://www.gatsbyjs.com/docs/reference/local-development/fast-refresh/#how-it-works).
|
package/index.cjs
CHANGED
|
@@ -15,11 +15,15 @@ var resolve = (0, import_module.createRequire)(
|
|
|
15
15
|
typeof __filename !== "undefined" ? __filename : import_meta.url
|
|
16
16
|
).resolve;
|
|
17
17
|
var refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
|
|
18
|
+
var _a, _b;
|
|
19
|
+
var isWebContainer = (_b = (_a = globalThis.process) == null ? void 0 : _a.versions) == null ? void 0 : _b.webcontainer;
|
|
18
20
|
var react = (_options) => {
|
|
21
|
+
let hmrDisabled = false;
|
|
19
22
|
const options = {
|
|
20
23
|
jsxImportSource: (_options == null ? void 0 : _options.jsxImportSource) ?? "react",
|
|
21
24
|
tsDecorators: _options == null ? void 0 : _options.tsDecorators,
|
|
22
|
-
plugins: (_options == null ? void 0 : _options.plugins) ? _options == null ? void 0 : _options.plugins.map((el) => [resolve(el[0]), el[1]]) : void 0
|
|
25
|
+
plugins: (_options == null ? void 0 : _options.plugins) ? _options == null ? void 0 : _options.plugins.map((el) => [resolve(el[0]), el[1]]) : void 0,
|
|
26
|
+
devTarget: (_options == null ? void 0 : _options.devTarget) ?? "es2020"
|
|
23
27
|
};
|
|
24
28
|
return [
|
|
25
29
|
{
|
|
@@ -40,6 +44,8 @@ var react = (_options) => {
|
|
|
40
44
|
}
|
|
41
45
|
}),
|
|
42
46
|
configResolved(config) {
|
|
47
|
+
if (config.server.hmr === false)
|
|
48
|
+
hmrDisabled = true;
|
|
43
49
|
const mdxIndex = config.plugins.findIndex(
|
|
44
50
|
(p) => p.name === "@mdx-js/rollup"
|
|
45
51
|
);
|
|
@@ -48,6 +54,11 @@ var react = (_options) => {
|
|
|
48
54
|
"[vite:react-swc] The MDX plugin should be placed before this plugin"
|
|
49
55
|
);
|
|
50
56
|
}
|
|
57
|
+
if (isWebContainer) {
|
|
58
|
+
config.logger.warn(
|
|
59
|
+
"[vite:react-swc] SWC is currently not supported in WebContainers. You can use the default React plugin instead."
|
|
60
|
+
);
|
|
61
|
+
}
|
|
51
62
|
},
|
|
52
63
|
transformIndexHtml: (_, config) => [
|
|
53
64
|
{
|
|
@@ -61,13 +72,19 @@ var react = (_options) => {
|
|
|
61
72
|
],
|
|
62
73
|
async transform(code, _id, transformOptions) {
|
|
63
74
|
const id = _id.split("?")[0];
|
|
64
|
-
const refresh = !(transformOptions == null ? void 0 : transformOptions.ssr) && !
|
|
65
|
-
const result = await transformWithOptions(
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
75
|
+
const refresh = !(transformOptions == null ? void 0 : transformOptions.ssr) && !hmrDisabled;
|
|
76
|
+
const result = await transformWithOptions(
|
|
77
|
+
id,
|
|
78
|
+
code,
|
|
79
|
+
options.devTarget,
|
|
80
|
+
options,
|
|
81
|
+
{
|
|
82
|
+
refresh,
|
|
83
|
+
development: true,
|
|
84
|
+
runtime: "automatic",
|
|
85
|
+
importSource: options.jsxImportSource
|
|
86
|
+
}
|
|
87
|
+
);
|
|
71
88
|
if (!result)
|
|
72
89
|
return;
|
|
73
90
|
if (!refresh || !refreshContentRE.test(result.code)) {
|
|
@@ -129,7 +146,7 @@ RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
|
|
|
129
146
|
};
|
|
130
147
|
var transformWithOptions = async (id, code, target, options, reactConfig) => {
|
|
131
148
|
const decorators = (options == null ? void 0 : options.tsDecorators) ?? false;
|
|
132
|
-
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") ? (
|
|
149
|
+
const parser = id.endsWith(".tsx") ? { syntax: "typescript", tsx: true, decorators } : id.endsWith(".ts") || id.endsWith(".mts") ? { syntax: "typescript", tsx: false, decorators } : id.endsWith(".jsx") ? { syntax: "ecmascript", jsx: true } : id.endsWith(".mdx") ? (
|
|
133
150
|
// JSX is required to trigger fast refresh transformations, even if MDX already transforms it
|
|
134
151
|
{ syntax: "ecmascript", jsx: true }
|
|
135
152
|
) : void 0;
|
|
@@ -169,11 +186,11 @@ var transformWithOptions = async (id, code, target, options, reactConfig) => {
|
|
|
169
186
|
var silenceUseClientWarning = (userConfig) => ({
|
|
170
187
|
rollupOptions: {
|
|
171
188
|
onwarn(warning, defaultHandler) {
|
|
172
|
-
var
|
|
189
|
+
var _a2, _b2;
|
|
173
190
|
if (warning.code === "MODULE_LEVEL_DIRECTIVE" && warning.message.includes("use client")) {
|
|
174
191
|
return;
|
|
175
192
|
}
|
|
176
|
-
if ((
|
|
193
|
+
if ((_b2 = (_a2 = userConfig.build) == null ? void 0 : _a2.rollupOptions) == null ? void 0 : _b2.onwarn) {
|
|
177
194
|
userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
|
|
178
195
|
} else {
|
|
179
196
|
defaultHandler(warning);
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { JscTarget } from "@swc/core";
|
|
1
2
|
import { PluginOption } from "vite";
|
|
2
3
|
type Options = {
|
|
3
4
|
/**
|
|
@@ -15,6 +16,12 @@ type Options = {
|
|
|
15
16
|
* @default undefined
|
|
16
17
|
*/
|
|
17
18
|
plugins?: [string, Record<string, any>][];
|
|
19
|
+
/**
|
|
20
|
+
* Set the target for SWC in dev. This can avoid to down-transpile private class method for example.
|
|
21
|
+
* For production target, see https://vitejs.dev/config/build-options.html#build-target
|
|
22
|
+
* @default "es2020"
|
|
23
|
+
*/
|
|
24
|
+
devTarget?: JscTarget;
|
|
18
25
|
};
|
|
19
26
|
declare const react: (_options?: Options) => PluginOption[];
|
|
20
27
|
export default react;
|
package/index.mjs
CHANGED
|
@@ -16,11 +16,15 @@ var resolve = createRequire(
|
|
|
16
16
|
typeof __filename !== "undefined" ? __filename : import.meta.url
|
|
17
17
|
).resolve;
|
|
18
18
|
var refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
|
|
19
|
+
var _a, _b;
|
|
20
|
+
var isWebContainer = (_b = (_a = globalThis.process) == null ? void 0 : _a.versions) == null ? void 0 : _b.webcontainer;
|
|
19
21
|
var react = (_options) => {
|
|
22
|
+
let hmrDisabled = false;
|
|
20
23
|
const options = {
|
|
21
24
|
jsxImportSource: (_options == null ? void 0 : _options.jsxImportSource) ?? "react",
|
|
22
25
|
tsDecorators: _options == null ? void 0 : _options.tsDecorators,
|
|
23
|
-
plugins: (_options == null ? void 0 : _options.plugins) ? _options == null ? void 0 : _options.plugins.map((el) => [resolve(el[0]), el[1]]) : void 0
|
|
26
|
+
plugins: (_options == null ? void 0 : _options.plugins) ? _options == null ? void 0 : _options.plugins.map((el) => [resolve(el[0]), el[1]]) : void 0,
|
|
27
|
+
devTarget: (_options == null ? void 0 : _options.devTarget) ?? "es2020"
|
|
24
28
|
};
|
|
25
29
|
return [
|
|
26
30
|
{
|
|
@@ -41,6 +45,8 @@ var react = (_options) => {
|
|
|
41
45
|
}
|
|
42
46
|
}),
|
|
43
47
|
configResolved(config) {
|
|
48
|
+
if (config.server.hmr === false)
|
|
49
|
+
hmrDisabled = true;
|
|
44
50
|
const mdxIndex = config.plugins.findIndex(
|
|
45
51
|
(p) => p.name === "@mdx-js/rollup"
|
|
46
52
|
);
|
|
@@ -49,6 +55,11 @@ var react = (_options) => {
|
|
|
49
55
|
"[vite:react-swc] The MDX plugin should be placed before this plugin"
|
|
50
56
|
);
|
|
51
57
|
}
|
|
58
|
+
if (isWebContainer) {
|
|
59
|
+
config.logger.warn(
|
|
60
|
+
"[vite:react-swc] SWC is currently not supported in WebContainers. You can use the default React plugin instead."
|
|
61
|
+
);
|
|
62
|
+
}
|
|
52
63
|
},
|
|
53
64
|
transformIndexHtml: (_, config) => [
|
|
54
65
|
{
|
|
@@ -62,13 +73,19 @@ var react = (_options) => {
|
|
|
62
73
|
],
|
|
63
74
|
async transform(code, _id, transformOptions) {
|
|
64
75
|
const id = _id.split("?")[0];
|
|
65
|
-
const refresh = !(transformOptions == null ? void 0 : transformOptions.ssr) && !
|
|
66
|
-
const result = await transformWithOptions(
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
76
|
+
const refresh = !(transformOptions == null ? void 0 : transformOptions.ssr) && !hmrDisabled;
|
|
77
|
+
const result = await transformWithOptions(
|
|
78
|
+
id,
|
|
79
|
+
code,
|
|
80
|
+
options.devTarget,
|
|
81
|
+
options,
|
|
82
|
+
{
|
|
83
|
+
refresh,
|
|
84
|
+
development: true,
|
|
85
|
+
runtime: "automatic",
|
|
86
|
+
importSource: options.jsxImportSource
|
|
87
|
+
}
|
|
88
|
+
);
|
|
72
89
|
if (!result)
|
|
73
90
|
return;
|
|
74
91
|
if (!refresh || !refreshContentRE.test(result.code)) {
|
|
@@ -130,7 +147,7 @@ RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
|
|
|
130
147
|
};
|
|
131
148
|
var transformWithOptions = async (id, code, target, options, reactConfig) => {
|
|
132
149
|
const decorators = (options == null ? void 0 : options.tsDecorators) ?? false;
|
|
133
|
-
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") ? (
|
|
150
|
+
const parser = id.endsWith(".tsx") ? { syntax: "typescript", tsx: true, decorators } : id.endsWith(".ts") || id.endsWith(".mts") ? { syntax: "typescript", tsx: false, decorators } : id.endsWith(".jsx") ? { syntax: "ecmascript", jsx: true } : id.endsWith(".mdx") ? (
|
|
134
151
|
// JSX is required to trigger fast refresh transformations, even if MDX already transforms it
|
|
135
152
|
{ syntax: "ecmascript", jsx: true }
|
|
136
153
|
) : void 0;
|
|
@@ -170,11 +187,11 @@ var transformWithOptions = async (id, code, target, options, reactConfig) => {
|
|
|
170
187
|
var silenceUseClientWarning = (userConfig) => ({
|
|
171
188
|
rollupOptions: {
|
|
172
189
|
onwarn(warning, defaultHandler) {
|
|
173
|
-
var
|
|
190
|
+
var _a2, _b2;
|
|
174
191
|
if (warning.code === "MODULE_LEVEL_DIRECTIVE" && warning.message.includes("use client")) {
|
|
175
192
|
return;
|
|
176
193
|
}
|
|
177
|
-
if ((
|
|
194
|
+
if ((_b2 = (_a2 = userConfig.build) == null ? void 0 : _a2.rollupOptions) == null ? void 0 : _b2.onwarn) {
|
|
178
195
|
userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
|
|
179
196
|
} else {
|
|
180
197
|
defaultHandler(warning);
|
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.4.1",
|
|
5
5
|
"author": "Arnaud Barré (https://github.com/ArnaudBarre)",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "github:vitejs/vite-plugin-react-swc",
|
|
@@ -28,6 +28,6 @@
|
|
|
28
28
|
"vite": "^4"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@swc/core": "^1.3.
|
|
31
|
+
"@swc/core": "^1.3.95"
|
|
32
32
|
}
|
|
33
33
|
}
|
package/refresh-runtime.js
CHANGED
|
@@ -391,9 +391,12 @@ function debounce(fn, delay) {
|
|
|
391
391
|
}
|
|
392
392
|
const enqueueUpdate = debounce(performReactRefresh, 16);
|
|
393
393
|
function validateRefreshBoundaryAndEnqueueUpdate(prevExports, nextExports) {
|
|
394
|
-
if (!predicateOnExport(prevExports, (key) =>
|
|
394
|
+
if (!predicateOnExport(prevExports, (key) => key in nextExports)) {
|
|
395
395
|
return "Could not Fast Refresh (export removed)";
|
|
396
396
|
}
|
|
397
|
+
if (!predicateOnExport(nextExports, (key) => key in prevExports)) {
|
|
398
|
+
return "Could not Fast Refresh (new export)";
|
|
399
|
+
}
|
|
397
400
|
let hasExports = false;
|
|
398
401
|
const allExportsAreComponentsOrUnchanged = predicateOnExport(
|
|
399
402
|
nextExports,
|
|
@@ -401,8 +404,6 @@ function validateRefreshBoundaryAndEnqueueUpdate(prevExports, nextExports) {
|
|
|
401
404
|
hasExports = true;
|
|
402
405
|
if (isLikelyComponentType(value))
|
|
403
406
|
return true;
|
|
404
|
-
if (!prevExports[key])
|
|
405
|
-
return false;
|
|
406
407
|
return prevExports[key] === nextExports[key];
|
|
407
408
|
}
|
|
408
409
|
);
|