@vitejs/plugin-react-swc 3.1.0 → 3.2.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.
Files changed (3) hide show
  1. package/index.cjs +32 -25
  2. package/index.mjs +32 -25
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -17,7 +17,7 @@ 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
  };
@@ -27,7 +27,9 @@ var react = (_options) => {
27
27
  apply: "serve",
28
28
  config: () => ({
29
29
  esbuild: false,
30
- optimizeDeps: { include: ["react/jsx-dev-runtime"] }
30
+ optimizeDeps: {
31
+ include: [`${options.jsxImportSource}/jsx-dev-runtime`]
32
+ }
31
33
  }),
32
34
  resolveId: (id) => id === runtimePublicPath ? id : void 0,
33
35
  load: (id) => id === runtimePublicPath ? (0, import_fs.readFileSync)((0, import_path.join)(_dirname, "refresh-runtime.js"), "utf-8") : void 0,
@@ -48,7 +50,7 @@ var react = (_options) => {
48
50
  development: true,
49
51
  useBuiltins: true,
50
52
  runtime: "automatic",
51
- importSource: options == null ? void 0 : options.jsxImportSource
53
+ importSource: options.jsxImportSource
52
54
  });
53
55
  if (!result)
54
56
  return;
@@ -56,26 +58,26 @@ var react = (_options) => {
56
58
  return result;
57
59
  }
58
60
  result.code = `import * as RefreshRuntime from "${runtimePublicPath}";
59
-
60
- if (!window.$RefreshReg$) throw new Error("React refresh preamble was not loaded. Something is wrong.");
61
- const prevRefreshReg = window.$RefreshReg$;
62
- const prevRefreshSig = window.$RefreshSig$;
63
- window.$RefreshReg$ = RefreshRuntime.getRefreshReg("${id}");
64
- window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
65
-
66
- ${result.code}
67
-
68
- window.$RefreshReg$ = prevRefreshReg;
69
- window.$RefreshSig$ = prevRefreshSig;
70
- import(/* @vite-ignore */ import.meta.url).then((currentExports) => {
71
- RefreshRuntime.registerExportsForReactRefresh("${id}", currentExports);
72
- import.meta.hot.accept((nextExports) => {
73
- if (!nextExports) return;
74
- const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(currentExports, nextExports);
75
- if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
76
- });
61
+
62
+ if (!window.$RefreshReg$) throw new Error("React refresh preamble was not loaded. Something is wrong.");
63
+ const prevRefreshReg = window.$RefreshReg$;
64
+ const prevRefreshSig = window.$RefreshSig$;
65
+ window.$RefreshReg$ = RefreshRuntime.getRefreshReg("${id}");
66
+ window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
67
+
68
+ ${result.code}
69
+
70
+ window.$RefreshReg$ = prevRefreshReg;
71
+ window.$RefreshSig$ = prevRefreshSig;
72
+ import(/* @vite-ignore */ import.meta.url).then((currentExports) => {
73
+ RefreshRuntime.registerExportsForReactRefresh("${id}", currentExports);
74
+ import.meta.hot.accept((nextExports) => {
75
+ if (!nextExports) return;
76
+ const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(currentExports, nextExports);
77
+ if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
77
78
  });
78
- `;
79
+ });
80
+ `;
79
81
  const sourceMap = JSON.parse(result.map);
80
82
  sourceMap.mappings = ";;;;;;;;" + sourceMap.mappings;
81
83
  return { code: result.code, map: sourceMap };
@@ -84,10 +86,12 @@ var react = (_options) => {
84
86
  options.plugins ? {
85
87
  name: "vite:react-swc",
86
88
  apply: "build",
89
+ enforce: "pre",
90
+ // Run before esbuild
87
91
  transform: (code, _id) => transformWithOptions(_id.split("?")[0], code, options, {
88
92
  useBuiltins: true,
89
93
  runtime: "automatic",
90
- importSource: options == null ? void 0 : options.jsxImportSource
94
+ importSource: options.jsxImportSource
91
95
  })
92
96
  } : {
93
97
  name: "vite:react-swc",
@@ -95,7 +99,7 @@ var react = (_options) => {
95
99
  config: () => ({
96
100
  esbuild: {
97
101
  jsx: "automatic",
98
- jsxImportSource: options == null ? void 0 : options.jsxImportSource,
102
+ jsxImportSource: options.jsxImportSource,
99
103
  tsconfigRaw: {
100
104
  compilerOptions: { useDefineForClassFields: true }
101
105
  }
@@ -108,7 +112,10 @@ var transformWithOptions = async (id, code, options, reactConfig) => {
108
112
  if (id.includes("node_modules"))
109
113
  return;
110
114
  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 } : void 0;
115
+ 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") ? (
116
+ // JSX is required to trigger fast refresh transformations, even if MDX already transforms it
117
+ { syntax: "ecmascript", jsx: true }
118
+ ) : void 0;
112
119
  if (!parser)
113
120
  return;
114
121
  let result;
package/index.mjs CHANGED
@@ -16,7 +16,7 @@ var resolve = createRequire(
16
16
  var refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
17
17
  var react = (_options) => {
18
18
  const options = {
19
- jsxImportSource: _options == null ? void 0 : _options.jsxImportSource,
19
+ jsxImportSource: (_options == null ? void 0 : _options.jsxImportSource) ?? "react",
20
20
  tsDecorators: _options == null ? void 0 : _options.tsDecorators,
21
21
  plugins: (_options == null ? void 0 : _options.plugins) ? _options == null ? void 0 : _options.plugins.map((el) => [resolve(el[0]), el[1]]) : void 0
22
22
  };
@@ -26,7 +26,9 @@ var react = (_options) => {
26
26
  apply: "serve",
27
27
  config: () => ({
28
28
  esbuild: false,
29
- optimizeDeps: { include: ["react/jsx-dev-runtime"] }
29
+ optimizeDeps: {
30
+ include: [`${options.jsxImportSource}/jsx-dev-runtime`]
31
+ }
30
32
  }),
31
33
  resolveId: (id) => id === runtimePublicPath ? id : void 0,
32
34
  load: (id) => id === runtimePublicPath ? readFileSync(join(_dirname, "refresh-runtime.js"), "utf-8") : void 0,
@@ -47,7 +49,7 @@ var react = (_options) => {
47
49
  development: true,
48
50
  useBuiltins: true,
49
51
  runtime: "automatic",
50
- importSource: options == null ? void 0 : options.jsxImportSource
52
+ importSource: options.jsxImportSource
51
53
  });
52
54
  if (!result)
53
55
  return;
@@ -55,26 +57,26 @@ var react = (_options) => {
55
57
  return result;
56
58
  }
57
59
  result.code = `import * as RefreshRuntime from "${runtimePublicPath}";
58
-
59
- if (!window.$RefreshReg$) throw new Error("React refresh preamble was not loaded. Something is wrong.");
60
- const prevRefreshReg = window.$RefreshReg$;
61
- const prevRefreshSig = window.$RefreshSig$;
62
- window.$RefreshReg$ = RefreshRuntime.getRefreshReg("${id}");
63
- window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
64
-
65
- ${result.code}
66
-
67
- window.$RefreshReg$ = prevRefreshReg;
68
- window.$RefreshSig$ = prevRefreshSig;
69
- import(/* @vite-ignore */ import.meta.url).then((currentExports) => {
70
- RefreshRuntime.registerExportsForReactRefresh("${id}", currentExports);
71
- import.meta.hot.accept((nextExports) => {
72
- if (!nextExports) return;
73
- const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(currentExports, nextExports);
74
- if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
75
- });
60
+
61
+ if (!window.$RefreshReg$) throw new Error("React refresh preamble was not loaded. Something is wrong.");
62
+ const prevRefreshReg = window.$RefreshReg$;
63
+ const prevRefreshSig = window.$RefreshSig$;
64
+ window.$RefreshReg$ = RefreshRuntime.getRefreshReg("${id}");
65
+ window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
66
+
67
+ ${result.code}
68
+
69
+ window.$RefreshReg$ = prevRefreshReg;
70
+ window.$RefreshSig$ = prevRefreshSig;
71
+ import(/* @vite-ignore */ import.meta.url).then((currentExports) => {
72
+ RefreshRuntime.registerExportsForReactRefresh("${id}", currentExports);
73
+ import.meta.hot.accept((nextExports) => {
74
+ if (!nextExports) return;
75
+ const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(currentExports, nextExports);
76
+ if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
76
77
  });
77
- `;
78
+ });
79
+ `;
78
80
  const sourceMap = JSON.parse(result.map);
79
81
  sourceMap.mappings = ";;;;;;;;" + sourceMap.mappings;
80
82
  return { code: result.code, map: sourceMap };
@@ -83,10 +85,12 @@ var react = (_options) => {
83
85
  options.plugins ? {
84
86
  name: "vite:react-swc",
85
87
  apply: "build",
88
+ enforce: "pre",
89
+ // Run before esbuild
86
90
  transform: (code, _id) => transformWithOptions(_id.split("?")[0], code, options, {
87
91
  useBuiltins: true,
88
92
  runtime: "automatic",
89
- importSource: options == null ? void 0 : options.jsxImportSource
93
+ importSource: options.jsxImportSource
90
94
  })
91
95
  } : {
92
96
  name: "vite:react-swc",
@@ -94,7 +98,7 @@ var react = (_options) => {
94
98
  config: () => ({
95
99
  esbuild: {
96
100
  jsx: "automatic",
97
- jsxImportSource: options == null ? void 0 : options.jsxImportSource,
101
+ jsxImportSource: options.jsxImportSource,
98
102
  tsconfigRaw: {
99
103
  compilerOptions: { useDefineForClassFields: true }
100
104
  }
@@ -107,7 +111,10 @@ var transformWithOptions = async (id, code, options, reactConfig) => {
107
111
  if (id.includes("node_modules"))
108
112
  return;
109
113
  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 } : void 0;
114
+ 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") ? (
115
+ // JSX is required to trigger fast refresh transformations, even if MDX already transforms it
116
+ { syntax: "ecmascript", jsx: true }
117
+ ) : void 0;
111
118
  if (!parser)
112
119
  return;
113
120
  let result;
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.1.0",
4
+ "version": "3.2.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"
30
+ "@swc/core": "^1.3.35"
31
31
  }
32
32
  }