@vitejs/plugin-react 2.2.0-beta.0 → 3.0.0-alpha.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/dist/index.cjs CHANGED
@@ -1,43 +1,37 @@
1
1
  'use strict';
2
2
 
3
- const path = require('node:path');
4
3
  const babel = require('@babel/core');
5
4
  const vite = require('vite');
6
5
  const MagicString = require('magic-string');
7
6
  const fs = require('node:fs');
7
+ const path = require('node:path');
8
8
  const node_module = require('node:module');
9
9
 
10
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
11
-
12
- function _interopNamespace(e) {
13
- if (e && e.__esModule) return e;
10
+ function _interopNamespaceDefault(e) {
14
11
  const n = Object.create(null);
15
12
  if (e) {
16
13
  for (const k in e) {
17
14
  n[k] = e[k];
18
15
  }
19
16
  }
20
- n["default"] = e;
17
+ n.default = e;
21
18
  return n;
22
19
  }
23
20
 
24
- const path__default = /*#__PURE__*/_interopDefaultLegacy(path);
25
- const babel__namespace = /*#__PURE__*/_interopNamespace(babel);
26
- const MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
27
- const fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
21
+ const babel__namespace = /*#__PURE__*/_interopNamespaceDefault(babel);
28
22
 
29
23
  const runtimePublicPath = "/@react-refresh";
30
24
  const _require = node_module.createRequire((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)));
31
- const reactRefreshDir = path__default.dirname(
25
+ const reactRefreshDir = path.dirname(
32
26
  _require.resolve("react-refresh/package.json")
33
27
  );
34
- const runtimeFilePath = path__default.join(
28
+ const runtimeFilePath = path.join(
35
29
  reactRefreshDir,
36
30
  "cjs/react-refresh-runtime.development.js"
37
31
  );
38
32
  const runtimeCode = `
39
33
  const exports = {}
40
- ${fs__default.readFileSync(runtimeFilePath, "utf-8")}
34
+ ${fs.readFileSync(runtimeFilePath, "utf-8")}
41
35
  function debounce(fn, delay) {
42
36
  let handle
43
37
  return () => {
@@ -157,83 +151,9 @@ function isComponentLikeName(name) {
157
151
  return typeof name === "string" && name[0] >= "A" && name[0] <= "Z";
158
152
  }
159
153
 
160
- function babelImportToRequire({ types: t }) {
161
- return {
162
- visitor: {
163
- ImportDeclaration(path) {
164
- const decl = path.node;
165
- const spec = decl.specifiers[0];
166
- path.replaceWith(
167
- t.variableDeclaration("var", [
168
- t.variableDeclarator(
169
- spec.local,
170
- t.memberExpression(
171
- t.callExpression(t.identifier("require"), [decl.source]),
172
- spec.imported
173
- )
174
- )
175
- ])
176
- );
177
- }
178
- }
179
- };
180
- }
181
-
182
- let babelRestoreJSX;
183
- const jsxNotFound = [null, false];
184
- async function getBabelRestoreJSX() {
185
- if (!babelRestoreJSX)
186
- babelRestoreJSX = import('./chunks/babel-restore-jsx.cjs').then((r) => {
187
- const fn = r.default;
188
- if ("default" in fn)
189
- return fn.default;
190
- return fn;
191
- });
192
- return babelRestoreJSX;
193
- }
194
- async function restoreJSX(babel, code, filename) {
195
- const [reactAlias, isCommonJS] = parseReactAlias(code);
196
- if (!reactAlias) {
197
- return jsxNotFound;
198
- }
199
- const reactJsxRE = new RegExp(
200
- `\\b${reactAlias}\\.(createElement|Fragment)\\b`,
201
- "g"
202
- );
203
- if (!reactJsxRE.test(code)) {
204
- return jsxNotFound;
205
- }
206
- const result = await babel.transformAsync(code, {
207
- babelrc: false,
208
- configFile: false,
209
- ast: true,
210
- code: false,
211
- filename,
212
- parserOpts: {
213
- plugins: ["jsx"]
214
- },
215
- plugins: [[await getBabelRestoreJSX(), { reactAlias }]]
216
- });
217
- return [result?.ast, isCommonJS];
218
- }
219
- function parseReactAlias(code) {
220
- let match = code.match(
221
- /\b(var|let|const)\s+([^=\{\s]+)\s*=\s*require\(["']react["']\)/
222
- );
223
- if (match) {
224
- return [match[2], true];
225
- }
226
- match = code.match(/^import\s+(?:\*\s+as\s+)?(\w+).+?\bfrom\s*["']react["']/m);
227
- if (match) {
228
- return [match[1], false];
229
- }
230
- return [void 0, false];
231
- }
232
-
233
154
  const prependReactImportCode = "import React from 'react'; ";
234
155
  function viteReact(opts = {}) {
235
156
  let devBase = "/";
236
- let resolvedCacheDir;
237
157
  let filter = vite.createFilter(opts.include, opts.exclude);
238
158
  let needHiresSourcemap = false;
239
159
  let isProduction = true;
@@ -248,13 +168,26 @@ function viteReact(opts = {}) {
248
168
  const viteBabel = {
249
169
  name: "vite:react-babel",
250
170
  enforce: "pre",
251
- config() {
171
+ config(_, { mode }) {
172
+ const isProduction2 = (process.env.NODE_ENV || process.env.VITE_USER_NODE_ENV || mode) === "production";
252
173
  if (opts.jsxRuntime === "classic") {
253
174
  return {
254
175
  esbuild: {
255
176
  logOverride: {
256
177
  "this-is-undefined-in-esm": "silent"
257
- }
178
+ },
179
+ jsx: "transform",
180
+ jsxImportSource: opts.jsxImportSource,
181
+ jsxSideEffects: opts.jsxPure === false
182
+ }
183
+ };
184
+ } else {
185
+ return {
186
+ esbuild: {
187
+ jsxDev: !isProduction2,
188
+ jsx: "automatic",
189
+ jsxImportSource: opts.jsxImportSource,
190
+ jsxSideEffects: opts.jsxPure === false
258
191
  }
259
192
  };
260
193
  }
@@ -262,7 +195,6 @@ function viteReact(opts = {}) {
262
195
  configResolved(config) {
263
196
  devBase = config.base;
264
197
  projectRoot = config.root;
265
- resolvedCacheDir = vite.normalizePath(path__default.resolve(config.cacheDir));
266
198
  filter = vite.createFilter(opts.include, opts.exclude, {
267
199
  resolve: projectRoot
268
200
  });
@@ -326,29 +258,9 @@ function viteReact(opts = {}) {
326
258
  ]);
327
259
  }
328
260
  }
329
- let ast;
330
261
  let prependReactImport = false;
331
262
  if (!isProjectFile || isJSX) {
332
- if (useAutomaticRuntime) {
333
- const isOptimizedReactDom = id.startsWith(resolvedCacheDir) && id.includes("/react-dom.js");
334
- const [restoredAst, isCommonJS] = !isProjectFile && !isJSX && !isOptimizedReactDom ? await restoreJSX(babel__namespace, code, id) : [null, false];
335
- if (isJSX || (ast = restoredAst)) {
336
- plugins.push([
337
- await loadPlugin(
338
- "@babel/plugin-transform-react-jsx" + (isProduction ? "" : "-development")
339
- ),
340
- {
341
- runtime: "automatic",
342
- importSource: opts.jsxImportSource,
343
- pure: opts.jsxPure !== false,
344
- throwIfNamespace: opts.jsxThrowIfNamespace
345
- }
346
- ]);
347
- if (isCommonJS) {
348
- plugins.push(babelImportToRequire);
349
- }
350
- }
351
- } else if (isProjectFile) {
263
+ if (!useAutomaticRuntime && isProjectFile) {
352
264
  if (!isProduction) {
353
265
  plugins.push(
354
266
  await loadPlugin("@babel/plugin-transform-react-jsx-self"),
@@ -363,7 +275,7 @@ function viteReact(opts = {}) {
363
275
  let inputMap;
364
276
  if (prependReactImport) {
365
277
  if (needHiresSourcemap) {
366
- const s = new MagicString__default(code);
278
+ const s = new MagicString(code);
367
279
  s.prepend(prependReactImportCode);
368
280
  code = s.toString();
369
281
  inputMap = s.generateMap({ hires: true, source: id });
@@ -392,7 +304,7 @@ function viteReact(opts = {}) {
392
304
  if (/\.tsx?$/.test(extension)) {
393
305
  parserPlugins.push("typescript");
394
306
  }
395
- const transformAsync = ast ? babel__namespace.transformFromAstAsync.bind(babel__namespace, ast, code) : babel__namespace.transformAsync.bind(babel__namespace, code);
307
+ const transformAsync = babel__namespace.transformAsync.bind(babel__namespace, code);
396
308
  const isReasonReact = extension.endsWith(".bs.js");
397
309
  const result = await transformAsync({
398
310
  ...babelOptions,
@@ -500,8 +412,8 @@ function viteReact(opts = {}) {
500
412
  return [viteBabel, viteReactRefresh, useAutomaticRuntime && viteReactJsx];
501
413
  }
502
414
  viteReact.preambleCode = preambleCode;
503
- function loadPlugin(path2) {
504
- return import(path2).then((module) => module.default || module);
415
+ function loadPlugin(path) {
416
+ return import(path).then((module) => module.default || module);
505
417
  }
506
418
  function createBabelOptions(rawOptions) {
507
419
  var _a;
@@ -519,4 +431,4 @@ function createBabelOptions(rawOptions) {
519
431
  }
520
432
 
521
433
  module.exports = viteReact;
522
- module.exports["default"] = viteReact;
434
+ module.exports.default = viteReact;
package/dist/index.d.ts CHANGED
@@ -26,11 +26,6 @@ interface Options {
26
26
  * @default true
27
27
  */
28
28
  jsxPure?: boolean;
29
- /**
30
- * Toggles whether or not to throw an error if an XML namespaced tag name is used.
31
- * @default true
32
- */
33
- jsxThrowIfNamespace?: boolean;
34
29
  /**
35
30
  * Babel configuration applied in both dev and prod.
36
31
  */
package/dist/index.mjs CHANGED
@@ -1,8 +1,8 @@
1
- import path from 'node:path';
2
1
  import * as babel from '@babel/core';
3
- import { createFilter, normalizePath } from 'vite';
2
+ import { createFilter } from 'vite';
4
3
  import MagicString from 'magic-string';
5
4
  import fs from 'node:fs';
5
+ import path from 'node:path';
6
6
  import { createRequire } from 'node:module';
7
7
 
8
8
  const runtimePublicPath = "/@react-refresh";
@@ -136,83 +136,9 @@ function isComponentLikeName(name) {
136
136
  return typeof name === "string" && name[0] >= "A" && name[0] <= "Z";
137
137
  }
138
138
 
139
- function babelImportToRequire({ types: t }) {
140
- return {
141
- visitor: {
142
- ImportDeclaration(path) {
143
- const decl = path.node;
144
- const spec = decl.specifiers[0];
145
- path.replaceWith(
146
- t.variableDeclaration("var", [
147
- t.variableDeclarator(
148
- spec.local,
149
- t.memberExpression(
150
- t.callExpression(t.identifier("require"), [decl.source]),
151
- spec.imported
152
- )
153
- )
154
- ])
155
- );
156
- }
157
- }
158
- };
159
- }
160
-
161
- let babelRestoreJSX;
162
- const jsxNotFound = [null, false];
163
- async function getBabelRestoreJSX() {
164
- if (!babelRestoreJSX)
165
- babelRestoreJSX = import('./chunks/babel-restore-jsx.mjs').then((r) => {
166
- const fn = r.default;
167
- if ("default" in fn)
168
- return fn.default;
169
- return fn;
170
- });
171
- return babelRestoreJSX;
172
- }
173
- async function restoreJSX(babel, code, filename) {
174
- const [reactAlias, isCommonJS] = parseReactAlias(code);
175
- if (!reactAlias) {
176
- return jsxNotFound;
177
- }
178
- const reactJsxRE = new RegExp(
179
- `\\b${reactAlias}\\.(createElement|Fragment)\\b`,
180
- "g"
181
- );
182
- if (!reactJsxRE.test(code)) {
183
- return jsxNotFound;
184
- }
185
- const result = await babel.transformAsync(code, {
186
- babelrc: false,
187
- configFile: false,
188
- ast: true,
189
- code: false,
190
- filename,
191
- parserOpts: {
192
- plugins: ["jsx"]
193
- },
194
- plugins: [[await getBabelRestoreJSX(), { reactAlias }]]
195
- });
196
- return [result?.ast, isCommonJS];
197
- }
198
- function parseReactAlias(code) {
199
- let match = code.match(
200
- /\b(var|let|const)\s+([^=\{\s]+)\s*=\s*require\(["']react["']\)/
201
- );
202
- if (match) {
203
- return [match[2], true];
204
- }
205
- match = code.match(/^import\s+(?:\*\s+as\s+)?(\w+).+?\bfrom\s*["']react["']/m);
206
- if (match) {
207
- return [match[1], false];
208
- }
209
- return [void 0, false];
210
- }
211
-
212
139
  const prependReactImportCode = "import React from 'react'; ";
213
140
  function viteReact(opts = {}) {
214
141
  let devBase = "/";
215
- let resolvedCacheDir;
216
142
  let filter = createFilter(opts.include, opts.exclude);
217
143
  let needHiresSourcemap = false;
218
144
  let isProduction = true;
@@ -227,13 +153,26 @@ function viteReact(opts = {}) {
227
153
  const viteBabel = {
228
154
  name: "vite:react-babel",
229
155
  enforce: "pre",
230
- config() {
156
+ config(_, { mode }) {
157
+ const isProduction2 = (process.env.NODE_ENV || process.env.VITE_USER_NODE_ENV || mode) === "production";
231
158
  if (opts.jsxRuntime === "classic") {
232
159
  return {
233
160
  esbuild: {
234
161
  logOverride: {
235
162
  "this-is-undefined-in-esm": "silent"
236
- }
163
+ },
164
+ jsx: "transform",
165
+ jsxImportSource: opts.jsxImportSource,
166
+ jsxSideEffects: opts.jsxPure === false
167
+ }
168
+ };
169
+ } else {
170
+ return {
171
+ esbuild: {
172
+ jsxDev: !isProduction2,
173
+ jsx: "automatic",
174
+ jsxImportSource: opts.jsxImportSource,
175
+ jsxSideEffects: opts.jsxPure === false
237
176
  }
238
177
  };
239
178
  }
@@ -241,7 +180,6 @@ function viteReact(opts = {}) {
241
180
  configResolved(config) {
242
181
  devBase = config.base;
243
182
  projectRoot = config.root;
244
- resolvedCacheDir = normalizePath(path.resolve(config.cacheDir));
245
183
  filter = createFilter(opts.include, opts.exclude, {
246
184
  resolve: projectRoot
247
185
  });
@@ -305,29 +243,9 @@ function viteReact(opts = {}) {
305
243
  ]);
306
244
  }
307
245
  }
308
- let ast;
309
246
  let prependReactImport = false;
310
247
  if (!isProjectFile || isJSX) {
311
- if (useAutomaticRuntime) {
312
- const isOptimizedReactDom = id.startsWith(resolvedCacheDir) && id.includes("/react-dom.js");
313
- const [restoredAst, isCommonJS] = !isProjectFile && !isJSX && !isOptimizedReactDom ? await restoreJSX(babel, code, id) : [null, false];
314
- if (isJSX || (ast = restoredAst)) {
315
- plugins.push([
316
- await loadPlugin(
317
- "@babel/plugin-transform-react-jsx" + (isProduction ? "" : "-development")
318
- ),
319
- {
320
- runtime: "automatic",
321
- importSource: opts.jsxImportSource,
322
- pure: opts.jsxPure !== false,
323
- throwIfNamespace: opts.jsxThrowIfNamespace
324
- }
325
- ]);
326
- if (isCommonJS) {
327
- plugins.push(babelImportToRequire);
328
- }
329
- }
330
- } else if (isProjectFile) {
248
+ if (!useAutomaticRuntime && isProjectFile) {
331
249
  if (!isProduction) {
332
250
  plugins.push(
333
251
  await loadPlugin("@babel/plugin-transform-react-jsx-self"),
@@ -371,7 +289,7 @@ function viteReact(opts = {}) {
371
289
  if (/\.tsx?$/.test(extension)) {
372
290
  parserPlugins.push("typescript");
373
291
  }
374
- const transformAsync = ast ? babel.transformFromAstAsync.bind(babel, ast, code) : babel.transformAsync.bind(babel, code);
292
+ const transformAsync = babel.transformAsync.bind(babel, code);
375
293
  const isReasonReact = extension.endsWith(".bs.js");
376
294
  const result = await transformAsync({
377
295
  ...babelOptions,
@@ -479,8 +397,8 @@ function viteReact(opts = {}) {
479
397
  return [viteBabel, viteReactRefresh, useAutomaticRuntime && viteReactJsx];
480
398
  }
481
399
  viteReact.preambleCode = preambleCode;
482
- function loadPlugin(path2) {
483
- return import(path2).then((module) => module.default || module);
400
+ function loadPlugin(path) {
401
+ return import(path).then((module) => module.default || module);
484
402
  }
485
403
  function createBabelOptions(rawOptions) {
486
404
  var _a;
package/package.json CHANGED
@@ -1,14 +1,13 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-react",
3
- "version": "2.2.0-beta.0",
3
+ "version": "3.0.0-alpha.0",
4
4
  "license": "MIT",
5
5
  "author": "Evan You",
6
6
  "contributors": [
7
7
  "Alec Larson"
8
8
  ],
9
9
  "files": [
10
- "dist",
11
- "src"
10
+ "dist"
12
11
  ],
13
12
  "main": "./dist/index.cjs",
14
13
  "module": "./dist/index.mjs",
@@ -39,12 +38,12 @@
39
38
  },
40
39
  "homepage": "https://github.com/vitejs/vite/tree/main/packages/plugin-react#readme",
41
40
  "dependencies": {
42
- "@babel/core": "^7.19.3",
41
+ "@babel/core": "^7.20.2",
43
42
  "@babel/plugin-transform-react-jsx": "^7.19.0",
44
43
  "@babel/plugin-transform-react-jsx-development": "^7.18.6",
45
44
  "@babel/plugin-transform-react-jsx-self": "^7.18.6",
46
- "@babel/plugin-transform-react-jsx-source": "^7.18.6",
47
- "magic-string": "^0.26.5",
45
+ "@babel/plugin-transform-react-jsx-source": "^7.19.6",
46
+ "magic-string": "^0.26.7",
48
47
  "react-refresh": "^0.14.0"
49
48
  },
50
49
  "peerDependencies": {
@@ -1,138 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * https://github.com/flying-sheep/babel-plugin-transform-react-createelement-to-jsx
5
- * @license GNU General Public License v3.0
6
- */
7
- function babelRestoreJsx({ types: t }, { reactAlias = "React" }) {
8
- function getJSXNode(node) {
9
- if (!isReactCreateElement(node)) {
10
- return null;
11
- }
12
- const [nameNode, propsNode, ...childNodes] = node.arguments;
13
- const name = getJSXName(nameNode);
14
- if (name == null) {
15
- return null;
16
- }
17
- const props = getJSXProps(propsNode);
18
- if (props == null) {
19
- return null;
20
- }
21
- const children = getJSXChildren(childNodes);
22
- if (children == null) {
23
- return null;
24
- }
25
- if (t.isJSXMemberExpression(name) && t.isJSXIdentifier(name.object) && name.object.name === reactAlias && name.property.name === "Fragment") {
26
- return t.jsxFragment(
27
- t.jsxOpeningFragment(),
28
- t.jsxClosingFragment(),
29
- children
30
- );
31
- }
32
- const selfClosing = children.length === 0;
33
- const startTag = t.jsxOpeningElement(name, props, selfClosing);
34
- startTag.loc = node.loc;
35
- const endTag = selfClosing ? null : t.jsxClosingElement(name);
36
- return t.jsxElement(startTag, endTag, children, selfClosing);
37
- }
38
- function getJSXName(node) {
39
- if (node == null) {
40
- return null;
41
- }
42
- const name = getJSXIdentifier(node, true);
43
- if (name != null) {
44
- return name;
45
- }
46
- if (!t.isMemberExpression(node)) {
47
- return null;
48
- }
49
- const object = getJSXName(node.object);
50
- const property = getJSXName(node.property);
51
- if (object == null || property == null) {
52
- return null;
53
- }
54
- return t.jsxMemberExpression(object, property);
55
- }
56
- function getJSXProps(node) {
57
- if (node == null || isNullLikeNode(node)) {
58
- return [];
59
- }
60
- if (t.isCallExpression(node) && t.isIdentifier(node.callee, { name: "_extends" })) {
61
- const props = node.arguments.map(getJSXProps);
62
- if (props.every((prop) => prop != null)) {
63
- return [].concat(...props);
64
- }
65
- }
66
- if (!t.isObjectExpression(node) && t.isExpression(node))
67
- return [t.jsxSpreadAttribute(node)];
68
- if (!isPlainObjectExpression(node)) {
69
- return null;
70
- }
71
- return node.properties.map(
72
- (prop) => t.isObjectProperty(prop) ? t.jsxAttribute(
73
- getJSXIdentifier(prop.key),
74
- getJSXAttributeValue(prop.value)
75
- ) : t.jsxSpreadAttribute(prop.argument)
76
- ).filter(
77
- (prop) => t.isJSXIdentifier(prop.name) ? prop.name.name !== "__self" && prop.name.name !== "__source" : true
78
- );
79
- }
80
- function getJSXChild(node) {
81
- if (t.isStringLiteral(node)) {
82
- return t.jsxText(node.value);
83
- }
84
- if (isReactCreateElement(node)) {
85
- return getJSXNode(node);
86
- }
87
- if (t.isExpression(node)) {
88
- return t.jsxExpressionContainer(node);
89
- }
90
- return null;
91
- }
92
- function getJSXChildren(nodes) {
93
- const children = nodes.filter((node) => !isNullLikeNode(node)).map(getJSXChild);
94
- if (children.some((child) => child == null)) {
95
- return null;
96
- }
97
- return children;
98
- }
99
- function getJSXIdentifier(node, tag = false) {
100
- if (t.isIdentifier(node) && (!tag || node.name.match(/^[A-Z]/))) {
101
- return t.jsxIdentifier(node.name);
102
- }
103
- if (t.isStringLiteral(node)) {
104
- return t.jsxIdentifier(node.value);
105
- }
106
- return null;
107
- }
108
- function getJSXAttributeValue(node) {
109
- if (t.isStringLiteral(node)) {
110
- return node;
111
- }
112
- if (t.isJSXElement(node)) {
113
- return node;
114
- }
115
- if (t.isExpression(node)) {
116
- return t.jsxExpressionContainer(node);
117
- }
118
- return null;
119
- }
120
- const isReactCreateElement = (node) => t.isCallExpression(node) && t.isMemberExpression(node.callee) && t.isIdentifier(node.callee.object, { name: reactAlias }) && t.isIdentifier(node.callee.property, { name: "createElement" }) && !node.callee.computed;
121
- const isNullLikeNode = (node) => t.isNullLiteral(node) || t.isIdentifier(node, { name: "undefined" });
122
- const isPlainObjectExpression = (node) => t.isObjectExpression(node) && node.properties.every(
123
- (property) => t.isSpreadElement(property) || t.isObjectProperty(property, { computed: false }) && getJSXIdentifier(property.key) != null && getJSXAttributeValue(property.value) != null
124
- );
125
- return {
126
- visitor: {
127
- CallExpression(path) {
128
- const node = getJSXNode(path.node);
129
- if (node == null) {
130
- return null;
131
- }
132
- path.replaceWith(node);
133
- }
134
- }
135
- };
136
- }
137
-
138
- exports["default"] = babelRestoreJsx;