@vitejs/plugin-react 4.0.0-beta.1 → 4.0.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 CHANGED
@@ -46,6 +46,14 @@ Control where the JSX factory is imported from. Default to `'react'`
46
46
  react({ jsxImportSource: '@emotion/react' })
47
47
  ```
48
48
 
49
+ ## jsxRuntime
50
+
51
+ By default, the plugin uses the [automatic JSX runtime](https://legacy.reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html). However, if you encounter any issues, you may opt out using the `jsxRuntime` option.
52
+
53
+ ```js
54
+ react({ jsxRuntime: 'classic' })
55
+ ```
56
+
49
57
  ### babel
50
58
 
51
59
  The `babel` option lets you add plugins, presets, and [other configuration](https://babeljs.io/docs/en/options) to the Babel transformation performed on each included file.
package/dist/index.cjs CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  const babel = require('@babel/core');
4
4
  const vite = require('vite');
5
- const MagicString = require('magic-string');
6
5
  const fs = require('node:fs');
7
6
  const path = require('node:path');
8
7
  const node_module = require('node:module');
@@ -22,7 +21,6 @@ function _interopNamespaceCompat(e) {
22
21
  }
23
22
 
24
23
  const babel__namespace = /*#__PURE__*/_interopNamespaceCompat(babel);
25
- const MagicString__default = /*#__PURE__*/_interopDefaultCompat(MagicString);
26
24
  const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
27
25
  const path__default = /*#__PURE__*/_interopDefaultCompat(path);
28
26
 
@@ -51,10 +49,11 @@ window.__vite_plugin_react_preamble_installed__ = true
51
49
  const header = `
52
50
  import RefreshRuntime from "${runtimePublicPath}";
53
51
 
52
+ const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
54
53
  let prevRefreshReg;
55
54
  let prevRefreshSig;
56
55
 
57
- if (import.meta.hot) {
56
+ if (import.meta.hot && !inWebWorker) {
58
57
  if (!window.__vite_plugin_react_preamble_installed__) {
59
58
  throw new Error(
60
59
  "@vitejs/plugin-react can't detect preamble. Something is wrong. " +
@@ -70,7 +69,7 @@ if (import.meta.hot) {
70
69
  window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
71
70
  }`.replace(/\n+/g, "");
72
71
  const footer = `
73
- if (import.meta.hot) {
72
+ if (import.meta.hot && !inWebWorker) {
74
73
  window.$RefreshReg$ = prevRefreshReg;
75
74
  window.$RefreshSig$ = prevRefreshSig;
76
75
 
@@ -87,7 +86,6 @@ function addRefreshWrapper(code, id) {
87
86
  return header.replace("__SOURCE__", JSON.stringify(id)) + code + footer.replace("__SOURCE__", JSON.stringify(id));
88
87
  }
89
88
 
90
- const prependReactImportCode = "import React from 'react'; ";
91
89
  const refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
92
90
  const defaultIncludeRE = /\.[tj]sx?$/;
93
91
  const tsRE = /\.tsx?$/;
@@ -95,13 +93,12 @@ function viteReact(opts = {}) {
95
93
  let devBase = "/";
96
94
  const filter = vite.createFilter(opts.include ?? defaultIncludeRE, opts.exclude);
97
95
  const devRuntime = `${opts.jsxImportSource ?? "react"}/jsx-dev-runtime`;
98
- let needHiresSourcemap = false;
99
96
  let isProduction = true;
100
97
  let projectRoot = process.cwd();
101
98
  let skipFastRefresh = false;
102
99
  let runPluginOverrides;
103
100
  let staticBabelOptions;
104
- const importReactRE = /(?:^|\n)import\s+(?:\*\s+as\s+)?React(?:,|\s+)/;
101
+ const importReactRE = /(?:^|\s)import\s+(?:\*\s+as\s+)?React(?:,|\s+)/;
105
102
  const viteBabel = {
106
103
  name: "vite:react-babel",
107
104
  enforce: "pre",
@@ -124,14 +121,8 @@ function viteReact(opts = {}) {
124
121
  configResolved(config) {
125
122
  devBase = config.base;
126
123
  projectRoot = config.root;
127
- needHiresSourcemap = config.command === "build" && !!config.build.sourcemap;
128
124
  isProduction = config.isProduction;
129
- skipFastRefresh = isProduction || config.command === "build";
130
- if (opts.jsxRuntime === "classic") {
131
- config.logger.warnOnce(
132
- "[@vitejs/plugin-react] Support for classic runtime is deprecated."
133
- );
134
- }
125
+ skipFastRefresh = isProduction || config.command === "build" || config.server.hmr === false;
135
126
  if ("jsxPure" in opts) {
136
127
  config.logger.warnOnce(
137
128
  "[@vitejs/plugin-react] jsxPure was removed. You can configure esbuild.jsxSideEffects directly."
@@ -171,7 +162,6 @@ function viteReact(opts = {}) {
171
162
  { skipEnvCheck: true }
172
163
  ]);
173
164
  }
174
- let prependReactImport = false;
175
165
  if (opts.jsxRuntime === "classic" && isJSX) {
176
166
  if (!isProduction) {
177
167
  plugins.push(
@@ -179,23 +169,9 @@ function viteReact(opts = {}) {
179
169
  await loadPlugin("@babel/plugin-transform-react-jsx-source")
180
170
  );
181
171
  }
182
- if (!importReactRE.test(code)) {
183
- prependReactImport = true;
184
- }
185
- }
186
- let inputMap;
187
- if (prependReactImport) {
188
- if (needHiresSourcemap) {
189
- const s = new MagicString__default(code);
190
- s.prepend(prependReactImportCode);
191
- code = s.toString();
192
- inputMap = s.generateMap({ hires: true, source: id });
193
- } else {
194
- code = prependReactImportCode + code;
195
- }
196
172
  }
197
173
  if (!plugins.length && !babelOptions.configFile && !babelOptions.babelrc) {
198
- return { code, map: inputMap ?? null };
174
+ return;
199
175
  }
200
176
  const parserPlugins = [...babelOptions.parserOpts.plugins];
201
177
  if (!filepath.endsWith(".ts")) {
@@ -220,9 +196,7 @@ function viteReact(opts = {}) {
220
196
  decoratorsBeforeExport: true
221
197
  },
222
198
  plugins,
223
- sourceMaps: true,
224
- // Vite handles sourcemap flattening
225
- inputSourceMap: inputMap ?? false
199
+ sourceMaps: true
226
200
  });
227
201
  if (result) {
228
202
  let code2 = result.code;
package/dist/index.d.ts CHANGED
@@ -4,18 +4,17 @@ import { ResolvedConfig, PluginOption } from 'vite';
4
4
  interface Options {
5
5
  include?: string | RegExp | Array<string | RegExp>;
6
6
  exclude?: string | RegExp | Array<string | RegExp>;
7
- /**
8
- * @deprecated All tools now support the automatic runtime, and it has been backported
9
- * up to React 16. This allows to skip the React import and can produce smaller bundlers.
10
- * @default "automatic"
11
- */
12
- jsxRuntime?: 'classic' | 'automatic';
13
7
  /**
14
8
  * Control where the JSX factory is imported from.
15
9
  * https://esbuild.github.io/api/#jsx-import-source
16
10
  * @default 'react'
17
11
  */
18
12
  jsxImportSource?: string;
13
+ /**
14
+ * Note: Skipping React import with classic runtime is not supported from v4
15
+ * @default "automatic"
16
+ */
17
+ jsxRuntime?: 'classic' | 'automatic';
19
18
  /**
20
19
  * Babel configuration applied in both dev and prod.
21
20
  */
package/dist/index.mjs CHANGED
@@ -1,6 +1,5 @@
1
1
  import * as babel from '@babel/core';
2
2
  import { createFilter } from 'vite';
3
- import MagicString from 'magic-string';
4
3
  import fs from 'node:fs';
5
4
  import path from 'node:path';
6
5
  import { createRequire } from 'node:module';
@@ -30,10 +29,11 @@ window.__vite_plugin_react_preamble_installed__ = true
30
29
  const header = `
31
30
  import RefreshRuntime from "${runtimePublicPath}";
32
31
 
32
+ const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
33
33
  let prevRefreshReg;
34
34
  let prevRefreshSig;
35
35
 
36
- if (import.meta.hot) {
36
+ if (import.meta.hot && !inWebWorker) {
37
37
  if (!window.__vite_plugin_react_preamble_installed__) {
38
38
  throw new Error(
39
39
  "@vitejs/plugin-react can't detect preamble. Something is wrong. " +
@@ -49,7 +49,7 @@ if (import.meta.hot) {
49
49
  window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
50
50
  }`.replace(/\n+/g, "");
51
51
  const footer = `
52
- if (import.meta.hot) {
52
+ if (import.meta.hot && !inWebWorker) {
53
53
  window.$RefreshReg$ = prevRefreshReg;
54
54
  window.$RefreshSig$ = prevRefreshSig;
55
55
 
@@ -66,7 +66,6 @@ function addRefreshWrapper(code, id) {
66
66
  return header.replace("__SOURCE__", JSON.stringify(id)) + code + footer.replace("__SOURCE__", JSON.stringify(id));
67
67
  }
68
68
 
69
- const prependReactImportCode = "import React from 'react'; ";
70
69
  const refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
71
70
  const defaultIncludeRE = /\.[tj]sx?$/;
72
71
  const tsRE = /\.tsx?$/;
@@ -74,13 +73,12 @@ function viteReact(opts = {}) {
74
73
  let devBase = "/";
75
74
  const filter = createFilter(opts.include ?? defaultIncludeRE, opts.exclude);
76
75
  const devRuntime = `${opts.jsxImportSource ?? "react"}/jsx-dev-runtime`;
77
- let needHiresSourcemap = false;
78
76
  let isProduction = true;
79
77
  let projectRoot = process.cwd();
80
78
  let skipFastRefresh = false;
81
79
  let runPluginOverrides;
82
80
  let staticBabelOptions;
83
- const importReactRE = /(?:^|\n)import\s+(?:\*\s+as\s+)?React(?:,|\s+)/;
81
+ const importReactRE = /(?:^|\s)import\s+(?:\*\s+as\s+)?React(?:,|\s+)/;
84
82
  const viteBabel = {
85
83
  name: "vite:react-babel",
86
84
  enforce: "pre",
@@ -103,14 +101,8 @@ function viteReact(opts = {}) {
103
101
  configResolved(config) {
104
102
  devBase = config.base;
105
103
  projectRoot = config.root;
106
- needHiresSourcemap = config.command === "build" && !!config.build.sourcemap;
107
104
  isProduction = config.isProduction;
108
- skipFastRefresh = isProduction || config.command === "build";
109
- if (opts.jsxRuntime === "classic") {
110
- config.logger.warnOnce(
111
- "[@vitejs/plugin-react] Support for classic runtime is deprecated."
112
- );
113
- }
105
+ skipFastRefresh = isProduction || config.command === "build" || config.server.hmr === false;
114
106
  if ("jsxPure" in opts) {
115
107
  config.logger.warnOnce(
116
108
  "[@vitejs/plugin-react] jsxPure was removed. You can configure esbuild.jsxSideEffects directly."
@@ -150,7 +142,6 @@ function viteReact(opts = {}) {
150
142
  { skipEnvCheck: true }
151
143
  ]);
152
144
  }
153
- let prependReactImport = false;
154
145
  if (opts.jsxRuntime === "classic" && isJSX) {
155
146
  if (!isProduction) {
156
147
  plugins.push(
@@ -158,23 +149,9 @@ function viteReact(opts = {}) {
158
149
  await loadPlugin("@babel/plugin-transform-react-jsx-source")
159
150
  );
160
151
  }
161
- if (!importReactRE.test(code)) {
162
- prependReactImport = true;
163
- }
164
- }
165
- let inputMap;
166
- if (prependReactImport) {
167
- if (needHiresSourcemap) {
168
- const s = new MagicString(code);
169
- s.prepend(prependReactImportCode);
170
- code = s.toString();
171
- inputMap = s.generateMap({ hires: true, source: id });
172
- } else {
173
- code = prependReactImportCode + code;
174
- }
175
152
  }
176
153
  if (!plugins.length && !babelOptions.configFile && !babelOptions.babelrc) {
177
- return { code, map: inputMap ?? null };
154
+ return;
178
155
  }
179
156
  const parserPlugins = [...babelOptions.parserOpts.plugins];
180
157
  if (!filepath.endsWith(".ts")) {
@@ -199,9 +176,7 @@ function viteReact(opts = {}) {
199
176
  decoratorsBeforeExport: true
200
177
  },
201
178
  plugins,
202
- sourceMaps: true,
203
- // Vite handles sourcemap flattening
204
- inputSourceMap: inputMap ?? false
179
+ sourceMaps: true
205
180
  });
206
181
  if (result) {
207
182
  let code2 = result.code;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-react",
3
- "version": "4.0.0-beta.1",
3
+ "version": "4.0.1",
4
4
  "license": "MIT",
5
5
  "author": "Evan You",
6
6
  "contributors": [
@@ -39,10 +39,9 @@
39
39
  },
40
40
  "homepage": "https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react#readme",
41
41
  "dependencies": {
42
- "@babel/core": "^7.21.4",
43
- "@babel/plugin-transform-react-jsx-self": "^7.21.0",
44
- "@babel/plugin-transform-react-jsx-source": "^7.19.6",
45
- "magic-string": "^0.30.0",
42
+ "@babel/core": "^7.22.5",
43
+ "@babel/plugin-transform-react-jsx-self": "^7.22.5",
44
+ "@babel/plugin-transform-react-jsx-source": "^7.22.5",
46
45
  "react-refresh": "^0.14.0"
47
46
  },
48
47
  "peerDependencies": {