@vitejs/plugin-react 4.0.0-beta.1 → 4.0.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
@@ -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
 
@@ -87,7 +85,6 @@ function addRefreshWrapper(code, id) {
87
85
  return header.replace("__SOURCE__", JSON.stringify(id)) + code + footer.replace("__SOURCE__", JSON.stringify(id));
88
86
  }
89
87
 
90
- const prependReactImportCode = "import React from 'react'; ";
91
88
  const refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
92
89
  const defaultIncludeRE = /\.[tj]sx?$/;
93
90
  const tsRE = /\.tsx?$/;
@@ -95,13 +92,12 @@ function viteReact(opts = {}) {
95
92
  let devBase = "/";
96
93
  const filter = vite.createFilter(opts.include ?? defaultIncludeRE, opts.exclude);
97
94
  const devRuntime = `${opts.jsxImportSource ?? "react"}/jsx-dev-runtime`;
98
- let needHiresSourcemap = false;
99
95
  let isProduction = true;
100
96
  let projectRoot = process.cwd();
101
97
  let skipFastRefresh = false;
102
98
  let runPluginOverrides;
103
99
  let staticBabelOptions;
104
- const importReactRE = /(?:^|\n)import\s+(?:\*\s+as\s+)?React(?:,|\s+)/;
100
+ const importReactRE = /(?:^|\s)import\s+(?:\*\s+as\s+)?React(?:,|\s+)/;
105
101
  const viteBabel = {
106
102
  name: "vite:react-babel",
107
103
  enforce: "pre",
@@ -124,14 +120,8 @@ function viteReact(opts = {}) {
124
120
  configResolved(config) {
125
121
  devBase = config.base;
126
122
  projectRoot = config.root;
127
- needHiresSourcemap = config.command === "build" && !!config.build.sourcemap;
128
123
  isProduction = config.isProduction;
129
124
  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
- }
135
125
  if ("jsxPure" in opts) {
136
126
  config.logger.warnOnce(
137
127
  "[@vitejs/plugin-react] jsxPure was removed. You can configure esbuild.jsxSideEffects directly."
@@ -171,7 +161,6 @@ function viteReact(opts = {}) {
171
161
  { skipEnvCheck: true }
172
162
  ]);
173
163
  }
174
- let prependReactImport = false;
175
164
  if (opts.jsxRuntime === "classic" && isJSX) {
176
165
  if (!isProduction) {
177
166
  plugins.push(
@@ -179,23 +168,9 @@ function viteReact(opts = {}) {
179
168
  await loadPlugin("@babel/plugin-transform-react-jsx-source")
180
169
  );
181
170
  }
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
171
  }
197
172
  if (!plugins.length && !babelOptions.configFile && !babelOptions.babelrc) {
198
- return { code, map: inputMap ?? null };
173
+ return;
199
174
  }
200
175
  const parserPlugins = [...babelOptions.parserOpts.plugins];
201
176
  if (!filepath.endsWith(".ts")) {
@@ -220,9 +195,7 @@ function viteReact(opts = {}) {
220
195
  decoratorsBeforeExport: true
221
196
  },
222
197
  plugins,
223
- sourceMaps: true,
224
- // Vite handles sourcemap flattening
225
- inputSourceMap: inputMap ?? false
198
+ sourceMaps: true
226
199
  });
227
200
  if (result) {
228
201
  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';
@@ -66,7 +65,6 @@ function addRefreshWrapper(code, id) {
66
65
  return header.replace("__SOURCE__", JSON.stringify(id)) + code + footer.replace("__SOURCE__", JSON.stringify(id));
67
66
  }
68
67
 
69
- const prependReactImportCode = "import React from 'react'; ";
70
68
  const refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
71
69
  const defaultIncludeRE = /\.[tj]sx?$/;
72
70
  const tsRE = /\.tsx?$/;
@@ -74,13 +72,12 @@ function viteReact(opts = {}) {
74
72
  let devBase = "/";
75
73
  const filter = createFilter(opts.include ?? defaultIncludeRE, opts.exclude);
76
74
  const devRuntime = `${opts.jsxImportSource ?? "react"}/jsx-dev-runtime`;
77
- let needHiresSourcemap = false;
78
75
  let isProduction = true;
79
76
  let projectRoot = process.cwd();
80
77
  let skipFastRefresh = false;
81
78
  let runPluginOverrides;
82
79
  let staticBabelOptions;
83
- const importReactRE = /(?:^|\n)import\s+(?:\*\s+as\s+)?React(?:,|\s+)/;
80
+ const importReactRE = /(?:^|\s)import\s+(?:\*\s+as\s+)?React(?:,|\s+)/;
84
81
  const viteBabel = {
85
82
  name: "vite:react-babel",
86
83
  enforce: "pre",
@@ -103,14 +100,8 @@ function viteReact(opts = {}) {
103
100
  configResolved(config) {
104
101
  devBase = config.base;
105
102
  projectRoot = config.root;
106
- needHiresSourcemap = config.command === "build" && !!config.build.sourcemap;
107
103
  isProduction = config.isProduction;
108
104
  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
- }
114
105
  if ("jsxPure" in opts) {
115
106
  config.logger.warnOnce(
116
107
  "[@vitejs/plugin-react] jsxPure was removed. You can configure esbuild.jsxSideEffects directly."
@@ -150,7 +141,6 @@ function viteReact(opts = {}) {
150
141
  { skipEnvCheck: true }
151
142
  ]);
152
143
  }
153
- let prependReactImport = false;
154
144
  if (opts.jsxRuntime === "classic" && isJSX) {
155
145
  if (!isProduction) {
156
146
  plugins.push(
@@ -158,23 +148,9 @@ function viteReact(opts = {}) {
158
148
  await loadPlugin("@babel/plugin-transform-react-jsx-source")
159
149
  );
160
150
  }
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
151
  }
176
152
  if (!plugins.length && !babelOptions.configFile && !babelOptions.babelrc) {
177
- return { code, map: inputMap ?? null };
153
+ return;
178
154
  }
179
155
  const parserPlugins = [...babelOptions.parserOpts.plugins];
180
156
  if (!filepath.endsWith(".ts")) {
@@ -199,9 +175,7 @@ function viteReact(opts = {}) {
199
175
  decoratorsBeforeExport: true
200
176
  },
201
177
  plugins,
202
- sourceMaps: true,
203
- // Vite handles sourcemap flattening
204
- inputSourceMap: inputMap ?? false
178
+ sourceMaps: true
205
179
  });
206
180
  if (result) {
207
181
  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.0",
4
4
  "license": "MIT",
5
5
  "author": "Evan You",
6
6
  "contributors": [
@@ -42,7 +42,6 @@
42
42
  "@babel/core": "^7.21.4",
43
43
  "@babel/plugin-transform-react-jsx-self": "^7.21.0",
44
44
  "@babel/plugin-transform-react-jsx-source": "^7.19.6",
45
- "magic-string": "^0.30.0",
46
45
  "react-refresh": "^0.14.0"
47
46
  },
48
47
  "peerDependencies": {