@vitejs/plugin-react-swc 3.5.0 → 3.6.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
@@ -56,7 +56,7 @@ Enable TypeScript decorators. Requires `experimentalDecorators` in tsconfig.
56
56
  react({ tsDecorators: true });
57
57
  ```
58
58
 
59
- ## plugins
59
+ ### plugins
60
60
 
61
61
  Use SWC plugins. Enable SWC at build time.
62
62
 
@@ -64,7 +64,7 @@ Use SWC plugins. Enable SWC at build time.
64
64
  react({ plugins: [["@swc/plugin-styled-components", {}]] });
65
65
  ```
66
66
 
67
- ## devTarget
67
+ ### devTarget
68
68
 
69
69
  Set the target for SWC in dev. This can avoid to down-transpile private class method for example.
70
70
 
@@ -76,6 +76,23 @@ For production target, see https://vitejs.dev/config/build-options.html#build-ta
76
76
  react({ devTarget: "es2022" });
77
77
  ```
78
78
 
79
+ ### parserConfig
80
+
81
+ Override the default include list (.ts, .tsx, .mts, .jsx, .mdx).
82
+
83
+ This requires to redefine the config for any file you want to be included (ts, mdx, ...).
84
+
85
+ If you want to trigger fast refresh on compiled JS, use `jsx: true`. Exclusion of node_modules should be handled by the function if needed. Using this option to use JSX inside `.js` files is highly discouraged and can be removed in any future version.
86
+
87
+ ```ts
88
+ react({
89
+ parserConfig(id) {
90
+ if (id.endsWith(".res")) return { syntax: "ecmascript", jsx: true };
91
+ if (id.endsWith(".ts")) return { syntax: "typescript", tsx: false };
92
+ },
93
+ });
94
+ ```
95
+
79
96
  ## Consistent components exports
80
97
 
81
98
  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
@@ -23,7 +23,8 @@ var react = (_options) => {
23
23
  jsxImportSource: (_options == null ? void 0 : _options.jsxImportSource) ?? "react",
24
24
  tsDecorators: _options == null ? void 0 : _options.tsDecorators,
25
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"
26
+ devTarget: (_options == null ? void 0 : _options.devTarget) ?? "es2020",
27
+ parserConfig: _options == null ? void 0 : _options.parserConfig
27
28
  };
28
29
  return [
29
30
  {
@@ -107,7 +108,7 @@ RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
107
108
  RefreshRuntime.registerExportsForReactRefresh("${id}", currentExports);
108
109
  import.meta.hot.accept((nextExports) => {
109
110
  if (!nextExports) return;
110
- const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(currentExports, nextExports);
111
+ const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate("${id}", currentExports, nextExports);
111
112
  if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
112
113
  });
113
114
  });
@@ -147,7 +148,7 @@ RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
147
148
  };
148
149
  var transformWithOptions = async (id, code, target, options, reactConfig) => {
149
150
  const decorators = (options == null ? void 0 : options.tsDecorators) ?? false;
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") ? (
151
+ const parser = options.parserConfig ? options.parserConfig(id) : 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") ? (
151
152
  // JSX is required to trigger fast refresh transformations, even if MDX already transforms it
152
153
  { syntax: "ecmascript", jsx: true }
153
154
  ) : void 0;
package/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { JscTarget } from "@swc/core";
1
+ import { ParserConfig, JscTarget } from "@swc/core";
2
2
  import { PluginOption } from "vite";
3
3
  type Options = {
4
4
  /**
@@ -22,6 +22,13 @@ type Options = {
22
22
  * @default "es2020"
23
23
  */
24
24
  devTarget?: JscTarget;
25
+ /**
26
+ * Override the default include list (.ts, .tsx, .mts, .jsx, .mdx).
27
+ * This requires to redefine the config for any file you want to be included.
28
+ * If you want to trigger fast refresh on compiled JS, use `jsx: true`.
29
+ * Exclusion of node_modules should be handled by the function if needed.
30
+ */
31
+ parserConfig?: (id: string) => ParserConfig | undefined;
25
32
  };
26
33
  declare const react: (_options?: Options) => PluginOption[];
27
34
  export default react;
package/index.mjs CHANGED
@@ -24,7 +24,8 @@ var react = (_options) => {
24
24
  jsxImportSource: (_options == null ? void 0 : _options.jsxImportSource) ?? "react",
25
25
  tsDecorators: _options == null ? void 0 : _options.tsDecorators,
26
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"
27
+ devTarget: (_options == null ? void 0 : _options.devTarget) ?? "es2020",
28
+ parserConfig: _options == null ? void 0 : _options.parserConfig
28
29
  };
29
30
  return [
30
31
  {
@@ -108,7 +109,7 @@ RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
108
109
  RefreshRuntime.registerExportsForReactRefresh("${id}", currentExports);
109
110
  import.meta.hot.accept((nextExports) => {
110
111
  if (!nextExports) return;
111
- const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(currentExports, nextExports);
112
+ const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate("${id}", currentExports, nextExports);
112
113
  if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
113
114
  });
114
115
  });
@@ -148,7 +149,7 @@ RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
148
149
  };
149
150
  var transformWithOptions = async (id, code, target, options, reactConfig) => {
150
151
  const decorators = (options == null ? void 0 : options.tsDecorators) ?? false;
151
- 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") ? (
152
+ const parser = options.parserConfig ? options.parserConfig(id) : 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") ? (
152
153
  // JSX is required to trigger fast refresh transformations, even if MDX already transforms it
153
154
  { syntax: "ecmascript", jsx: true }
154
155
  ) : void 0;
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.5.0",
4
+ "version": "3.6.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 || ^5"
29
29
  },
30
30
  "dependencies": {
31
- "@swc/core": "^1.3.96"
31
+ "@swc/core": "^1.3.107"
32
32
  }
33
33
  }
@@ -17,16 +17,16 @@ function computeFullKey(signature) {
17
17
  return signature.fullKey;
18
18
  }
19
19
  let fullKey = signature.ownKey;
20
- let hooks;
20
+ let hooks2;
21
21
  try {
22
- hooks = signature.getCustomHooks();
22
+ hooks2 = signature.getCustomHooks();
23
23
  } catch (err) {
24
24
  signature.forceReset = true;
25
25
  signature.fullKey = fullKey;
26
26
  return fullKey;
27
27
  }
28
- for (let i = 0; i < hooks.length; i++) {
29
- const hook = hooks[i];
28
+ for (let i = 0; i < hooks2.length; i++) {
29
+ const hook = hooks2[i];
30
30
  if (typeof hook !== "function") {
31
31
  signature.forceReset = true;
32
32
  signature.fullKey = fullKey;
@@ -389,16 +389,35 @@ function debounce(fn, delay) {
389
389
  handle = setTimeout(fn, delay);
390
390
  };
391
391
  }
392
- const enqueueUpdate = debounce(performReactRefresh, 16);
393
- function validateRefreshBoundaryAndEnqueueUpdate(prevExports, nextExports) {
394
- if (!predicateOnExport(prevExports, (key) => key in nextExports)) {
392
+ const hooks = [];
393
+ window.__registerBeforePerformReactRefresh = (cb) => {
394
+ hooks.push(cb);
395
+ };
396
+ const enqueueUpdate = debounce(async () => {
397
+ if (hooks.length)
398
+ await Promise.all(hooks.map((cb) => cb()));
399
+ performReactRefresh();
400
+ }, 16);
401
+ function validateRefreshBoundaryAndEnqueueUpdate(id, prevExports, nextExports) {
402
+ var _a, _b;
403
+ const ignoredExports = (_b = (_a = window.__getReactRefreshIgnoredExports) == null ? void 0 : _a.call(window, { id })) != null ? _b : [];
404
+ if (predicateOnExport(
405
+ ignoredExports,
406
+ prevExports,
407
+ (key) => key in nextExports
408
+ ) !== true) {
395
409
  return "Could not Fast Refresh (export removed)";
396
410
  }
397
- if (!predicateOnExport(nextExports, (key) => key in prevExports)) {
411
+ if (predicateOnExport(
412
+ ignoredExports,
413
+ nextExports,
414
+ (key) => key in prevExports
415
+ ) !== true) {
398
416
  return "Could not Fast Refresh (new export)";
399
417
  }
400
418
  let hasExports = false;
401
419
  const allExportsAreComponentsOrUnchanged = predicateOnExport(
420
+ ignoredExports,
402
421
  nextExports,
403
422
  (key, value) => {
404
423
  hasExports = true;
@@ -407,21 +426,23 @@ function validateRefreshBoundaryAndEnqueueUpdate(prevExports, nextExports) {
407
426
  return prevExports[key] === nextExports[key];
408
427
  }
409
428
  );
410
- if (hasExports && allExportsAreComponentsOrUnchanged) {
429
+ if (hasExports && allExportsAreComponentsOrUnchanged === true) {
411
430
  enqueueUpdate();
412
431
  } else {
413
- return "Could not Fast Refresh. Learn more at https://github.com/vitejs/vite-plugin-react-swc#consistent-components-exports";
432
+ return `Could not Fast Refresh ("${allExportsAreComponentsOrUnchanged}" export is incompatible). Learn more at https://github.com/vitejs/vite-plugin-react-swc#consistent-components-exports`;
414
433
  }
415
434
  }
416
- function predicateOnExport(moduleExports, predicate) {
435
+ function predicateOnExport(ignoredExports, moduleExports, predicate) {
417
436
  for (const key in moduleExports) {
418
437
  if (key === "__esModule")
419
438
  continue;
439
+ if (ignoredExports.includes(key))
440
+ continue;
420
441
  const desc = Object.getOwnPropertyDescriptor(moduleExports, key);
421
442
  if (desc && desc.get)
422
- return false;
443
+ return key;
423
444
  if (!predicate(key, moduleExports[key]))
424
- return false;
445
+ return key;
425
446
  }
426
447
  return true;
427
448
  }