@vitejs/plugin-react-swc 3.3.1 → 3.4.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/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,15 +72,22 @@ var react = (_options) => {
61
72
  ],
62
73
  async transform(code, _id, transformOptions) {
63
74
  const id = _id.split("?")[0];
64
- const result = await transformWithOptions(id, code, "es2020", options, {
65
- refresh: !(transformOptions == null ? void 0 : transformOptions.ssr),
66
- development: true,
67
- runtime: "automatic",
68
- importSource: options.jsxImportSource
69
- });
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
+ );
70
88
  if (!result)
71
89
  return;
72
- if ((transformOptions == null ? void 0 : transformOptions.ssr) || !refreshContentRE.test(result.code)) {
90
+ if (!refresh || !refreshContentRE.test(result.code)) {
73
91
  return result;
74
92
  }
75
93
  result.code = `import * as RefreshRuntime from "${runtimePublicPath}";
@@ -168,11 +186,11 @@ var transformWithOptions = async (id, code, target, options, reactConfig) => {
168
186
  var silenceUseClientWarning = (userConfig) => ({
169
187
  rollupOptions: {
170
188
  onwarn(warning, defaultHandler) {
171
- var _a, _b;
189
+ var _a2, _b2;
172
190
  if (warning.code === "MODULE_LEVEL_DIRECTIVE" && warning.message.includes("use client")) {
173
191
  return;
174
192
  }
175
- if ((_b = (_a = userConfig.build) == null ? void 0 : _a.rollupOptions) == null ? void 0 : _b.onwarn) {
193
+ if ((_b2 = (_a2 = userConfig.build) == null ? void 0 : _a2.rollupOptions) == null ? void 0 : _b2.onwarn) {
176
194
  userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
177
195
  } else {
178
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,15 +73,22 @@ var react = (_options) => {
62
73
  ],
63
74
  async transform(code, _id, transformOptions) {
64
75
  const id = _id.split("?")[0];
65
- const result = await transformWithOptions(id, code, "es2020", options, {
66
- refresh: !(transformOptions == null ? void 0 : transformOptions.ssr),
67
- development: true,
68
- runtime: "automatic",
69
- importSource: options.jsxImportSource
70
- });
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
+ );
71
89
  if (!result)
72
90
  return;
73
- if ((transformOptions == null ? void 0 : transformOptions.ssr) || !refreshContentRE.test(result.code)) {
91
+ if (!refresh || !refreshContentRE.test(result.code)) {
74
92
  return result;
75
93
  }
76
94
  result.code = `import * as RefreshRuntime from "${runtimePublicPath}";
@@ -169,11 +187,11 @@ var transformWithOptions = async (id, code, target, options, reactConfig) => {
169
187
  var silenceUseClientWarning = (userConfig) => ({
170
188
  rollupOptions: {
171
189
  onwarn(warning, defaultHandler) {
172
- var _a, _b;
190
+ var _a2, _b2;
173
191
  if (warning.code === "MODULE_LEVEL_DIRECTIVE" && warning.message.includes("use client")) {
174
192
  return;
175
193
  }
176
- if ((_b = (_a = userConfig.build) == null ? void 0 : _a.rollupOptions) == null ? void 0 : _b.onwarn) {
194
+ if ((_b2 = (_a2 = userConfig.build) == null ? void 0 : _a2.rollupOptions) == null ? void 0 : _b2.onwarn) {
177
195
  userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
178
196
  } else {
179
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.3.1",
4
+ "version": "3.4.0",
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.56"
31
+ "@swc/core": "^1.3.85"
32
32
  }
33
33
  }
@@ -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) => !!nextExports[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
  );