@vitejs/plugin-react-swc 3.2.0 → 3.3.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/index.cjs CHANGED
@@ -22,6 +22,14 @@ var react = (_options) => {
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",
@@ -31,8 +39,16 @@ var react = (_options) => {
31
39
  include: [`${options.jsxImportSource}/jsx-dev-runtime`]
32
40
  }
33
41
  }),
34
- resolveId: (id) => id === runtimePublicPath ? id : void 0,
35
- load: (id) => id === runtimePublicPath ? (0, import_fs.readFileSync)((0, import_path.join)(_dirname, "refresh-runtime.js"), "utf-8") : void 0,
42
+ configResolved(config) {
43
+ const mdxIndex = config.plugins.findIndex(
44
+ (p) => p.name === "@mdx-js/rollup"
45
+ );
46
+ if (mdxIndex !== -1 && mdxIndex > config.plugins.findIndex((p) => p.name === "vite:react-swc")) {
47
+ throw new Error(
48
+ "[vite:react-swc] The MDX plugin should be placed before this plugin"
49
+ );
50
+ }
51
+ },
36
52
  transformIndexHtml: (_, config) => [
37
53
  {
38
54
  tag: "script",
@@ -45,10 +61,9 @@ var react = (_options) => {
45
61
  ],
46
62
  async transform(code, _id, transformOptions) {
47
63
  const id = _id.split("?")[0];
48
- const result = await transformWithOptions(id, code, options, {
64
+ const result = await transformWithOptions(id, code, "es2020", options, {
49
65
  refresh: !(transformOptions == null ? void 0 : transformOptions.ssr),
50
66
  development: true,
51
- useBuiltins: true,
52
67
  runtime: "automatic",
53
68
  importSource: options.jsxImportSource
54
69
  });
@@ -69,7 +84,7 @@ ${result.code}
69
84
 
70
85
  window.$RefreshReg$ = prevRefreshReg;
71
86
  window.$RefreshSig$ = prevRefreshSig;
72
- import(/* @vite-ignore */ import.meta.url).then((currentExports) => {
87
+ RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
73
88
  RefreshRuntime.registerExportsForReactRefresh("${id}", currentExports);
74
89
  import.meta.hot.accept((nextExports) => {
75
90
  if (!nextExports) return;
@@ -88,15 +103,18 @@ import(/* @vite-ignore */ import.meta.url).then((currentExports) => {
88
103
  apply: "build",
89
104
  enforce: "pre",
90
105
  // Run before esbuild
91
- transform: (code, _id) => transformWithOptions(_id.split("?")[0], code, options, {
92
- useBuiltins: true,
106
+ config: (userConfig) => ({
107
+ build: silenceUseClientWarning(userConfig)
108
+ }),
109
+ transform: (code, _id) => transformWithOptions(_id.split("?")[0], code, "esnext", options, {
93
110
  runtime: "automatic",
94
111
  importSource: options.jsxImportSource
95
112
  })
96
113
  } : {
97
114
  name: "vite:react-swc",
98
115
  apply: "build",
99
- config: () => ({
116
+ config: (userConfig) => ({
117
+ build: silenceUseClientWarning(userConfig),
100
118
  esbuild: {
101
119
  jsx: "automatic",
102
120
  jsxImportSource: options.jsxImportSource,
@@ -108,9 +126,7 @@ import(/* @vite-ignore */ import.meta.url).then((currentExports) => {
108
126
  }
109
127
  ];
110
128
  };
111
- var transformWithOptions = async (id, code, options, reactConfig) => {
112
- if (id.includes("node_modules"))
113
- return;
129
+ var transformWithOptions = async (id, code, target, options, reactConfig) => {
114
130
  const decorators = (options == null ? void 0 : options.tsDecorators) ?? false;
115
131
  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
132
  // JSX is required to trigger fast refresh transformations, even if MDX already transforms it
@@ -126,7 +142,7 @@ var transformWithOptions = async (id, code, options, reactConfig) => {
126
142
  configFile: false,
127
143
  sourceMaps: true,
128
144
  jsc: {
129
- target: "es2020",
145
+ target,
130
146
  parser,
131
147
  experimental: { plugins: options.plugins },
132
148
  transform: {
@@ -149,6 +165,21 @@ var transformWithOptions = async (id, code, options, reactConfig) => {
149
165
  }
150
166
  return result;
151
167
  };
168
+ var silenceUseClientWarning = (userConfig) => ({
169
+ rollupOptions: {
170
+ onwarn(warning, defaultHandler) {
171
+ var _a, _b;
172
+ if (warning.code === "MODULE_LEVEL_DIRECTIVE" && warning.message.includes("use client")) {
173
+ return;
174
+ }
175
+ if ((_b = (_a = userConfig.build) == null ? void 0 : _a.rollupOptions) == null ? void 0 : _b.onwarn) {
176
+ userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
177
+ } else {
178
+ defaultHandler(warning);
179
+ }
180
+ }
181
+ }
182
+ });
152
183
  var src_default = react;
153
184
 
154
185
  // <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 { transform } from "@swc/core";
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__";
@@ -21,6 +23,14 @@ var react = (_options) => {
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",
@@ -30,8 +40,16 @@ var react = (_options) => {
30
40
  include: [`${options.jsxImportSource}/jsx-dev-runtime`]
31
41
  }
32
42
  }),
33
- resolveId: (id) => id === runtimePublicPath ? id : void 0,
34
- load: (id) => id === runtimePublicPath ? readFileSync(join(_dirname, "refresh-runtime.js"), "utf-8") : void 0,
43
+ configResolved(config) {
44
+ const mdxIndex = config.plugins.findIndex(
45
+ (p) => p.name === "@mdx-js/rollup"
46
+ );
47
+ if (mdxIndex !== -1 && mdxIndex > config.plugins.findIndex((p) => p.name === "vite:react-swc")) {
48
+ throw new Error(
49
+ "[vite:react-swc] The MDX plugin should be placed before this plugin"
50
+ );
51
+ }
52
+ },
35
53
  transformIndexHtml: (_, config) => [
36
54
  {
37
55
  tag: "script",
@@ -44,10 +62,9 @@ var react = (_options) => {
44
62
  ],
45
63
  async transform(code, _id, transformOptions) {
46
64
  const id = _id.split("?")[0];
47
- const result = await transformWithOptions(id, code, options, {
65
+ const result = await transformWithOptions(id, code, "es2020", options, {
48
66
  refresh: !(transformOptions == null ? void 0 : transformOptions.ssr),
49
67
  development: true,
50
- useBuiltins: true,
51
68
  runtime: "automatic",
52
69
  importSource: options.jsxImportSource
53
70
  });
@@ -68,7 +85,7 @@ ${result.code}
68
85
 
69
86
  window.$RefreshReg$ = prevRefreshReg;
70
87
  window.$RefreshSig$ = prevRefreshSig;
71
- import(/* @vite-ignore */ import.meta.url).then((currentExports) => {
88
+ RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
72
89
  RefreshRuntime.registerExportsForReactRefresh("${id}", currentExports);
73
90
  import.meta.hot.accept((nextExports) => {
74
91
  if (!nextExports) return;
@@ -87,15 +104,18 @@ import(/* @vite-ignore */ import.meta.url).then((currentExports) => {
87
104
  apply: "build",
88
105
  enforce: "pre",
89
106
  // Run before esbuild
90
- transform: (code, _id) => transformWithOptions(_id.split("?")[0], code, options, {
91
- useBuiltins: true,
107
+ config: (userConfig) => ({
108
+ build: silenceUseClientWarning(userConfig)
109
+ }),
110
+ transform: (code, _id) => transformWithOptions(_id.split("?")[0], code, "esnext", options, {
92
111
  runtime: "automatic",
93
112
  importSource: options.jsxImportSource
94
113
  })
95
114
  } : {
96
115
  name: "vite:react-swc",
97
116
  apply: "build",
98
- config: () => ({
117
+ config: (userConfig) => ({
118
+ build: silenceUseClientWarning(userConfig),
99
119
  esbuild: {
100
120
  jsx: "automatic",
101
121
  jsxImportSource: options.jsxImportSource,
@@ -107,9 +127,7 @@ import(/* @vite-ignore */ import.meta.url).then((currentExports) => {
107
127
  }
108
128
  ];
109
129
  };
110
- var transformWithOptions = async (id, code, options, reactConfig) => {
111
- if (id.includes("node_modules"))
112
- return;
130
+ var transformWithOptions = async (id, code, target, options, reactConfig) => {
113
131
  const decorators = (options == null ? void 0 : options.tsDecorators) ?? false;
114
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") ? (
115
133
  // JSX is required to trigger fast refresh transformations, even if MDX already transforms it
@@ -125,7 +143,7 @@ var transformWithOptions = async (id, code, options, reactConfig) => {
125
143
  configFile: false,
126
144
  sourceMaps: true,
127
145
  jsc: {
128
- target: "es2020",
146
+ target,
129
147
  parser,
130
148
  experimental: { plugins: options.plugins },
131
149
  transform: {
@@ -148,6 +166,21 @@ var transformWithOptions = async (id, code, options, reactConfig) => {
148
166
  }
149
167
  return result;
150
168
  };
169
+ var silenceUseClientWarning = (userConfig) => ({
170
+ rollupOptions: {
171
+ onwarn(warning, defaultHandler) {
172
+ var _a, _b;
173
+ if (warning.code === "MODULE_LEVEL_DIRECTIVE" && warning.message.includes("use client")) {
174
+ return;
175
+ }
176
+ if ((_b = (_a = userConfig.build) == null ? void 0 : _a.rollupOptions) == null ? void 0 : _b.onwarn) {
177
+ userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
178
+ } else {
179
+ defaultHandler(warning);
180
+ }
181
+ }
182
+ }
183
+ });
151
184
  var src_default = react;
152
185
  export {
153
186
  src_default as default
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-react-swc",
3
3
  "description": "Speed up your Vite dev server with SWC",
4
- "version": "3.2.0",
4
+ "version": "3.3.1",
5
5
  "author": "Arnaud Barré (https://github.com/ArnaudBarre)",
6
6
  "license": "MIT",
7
7
  "repository": "github:vitejs/vite-plugin-react-swc",
8
+ "type": "module",
8
9
  "main": "index.cjs",
9
10
  "types": "index.d.ts",
10
11
  "module": "index.mjs",
@@ -27,6 +28,6 @@
27
28
  "vite": "^4"
28
29
  },
29
30
  "dependencies": {
30
- "@swc/core": "^1.3.35"
31
+ "@swc/core": "^1.3.56"
31
32
  }
32
33
  }
@@ -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,