@vitejs/plugin-react 1.1.0-beta.0 → 1.1.2

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/CHANGELOG.md CHANGED
@@ -1,3 +1,32 @@
1
+ ## [1.1.2](https://github.com/vitejs/vite/compare/plugin-react@1.1.1...plugin-react@1.1.2) (2021-12-13)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * ignore babel config when running restore-jsx ([#6047](https://github.com/vitejs/vite/issues/6047)) ([9c2843c](https://github.com/vitejs/vite/commit/9c2843cf0506844ee32f042a04c22c440434df2a))
7
+
8
+
9
+
10
+ ## [1.1.1](https://github.com/vitejs/vite/compare/plugin-react@1.1.0...plugin-react@1.1.1) (2021-12-07)
11
+
12
+
13
+
14
+ # [1.1.0](https://github.com/vitejs/vite/compare/plugin-react@1.1.0-beta.1...plugin-react@1.1.0) (2021-11-22)
15
+
16
+
17
+
18
+ # [1.1.0-beta.1](https://github.com/vitejs/vite/compare/plugin-react@1.1.0-beta.0...plugin-react@1.1.0-beta.1) (2021-11-19)
19
+
20
+
21
+ ### Bug Fixes
22
+
23
+ * **plugin-react:** apply `babel.plugins` to project files only ([#5255](https://github.com/vitejs/vite/issues/5255)) ([377d0be](https://github.com/vitejs/vite/commit/377d0be5cf85a50240e160beaaafda77b7199452))
24
+ * **plugin-react:** remove querystring from sourcemap filename ([#5760](https://github.com/vitejs/vite/issues/5760)) ([d93a9fa](https://github.com/vitejs/vite/commit/d93a9fab8986f3659e79d7b0b065e99ef625a5dd))
25
+ * **plugin-react:** restore usage of extension instead of id ([#5761](https://github.com/vitejs/vite/issues/5761)) ([59471b1](https://github.com/vitejs/vite/commit/59471b186612d3da0083543e23d660747d3287f3))
26
+ * **plugin-react:** uncompiled JSX in linked pkgs ([#5669](https://github.com/vitejs/vite/issues/5669)) ([41a7c9c](https://github.com/vitejs/vite/commit/41a7c9ccfbc1a7bc60aec672056eac3966ddd036))
27
+
28
+
29
+
1
30
  # [1.1.0-beta.0](https://github.com/vitejs/vite/compare/plugin-react@1.0.6...plugin-react@1.1.0-beta.0) (2021-10-28)
2
31
 
3
32
 
package/dist/index.js CHANGED
@@ -283,8 +283,15 @@ function babelImportToRequire({
283
283
 
284
284
  // src/jsx-runtime/restore-jsx.ts
285
285
  var babelRestoreJSX;
286
+ var jsxNotFound = [null, false];
286
287
  async function restoreJSX(babel2, code, filename) {
288
+ if (filename.includes("/.vite/react-dom.js")) {
289
+ return jsxNotFound;
290
+ }
287
291
  const [reactAlias, isCommonJS] = parseReactAlias(code);
292
+ if (!reactAlias) {
293
+ return jsxNotFound;
294
+ }
288
295
  const reactJsxRE = new RegExp("\\b" + reactAlias + "\\.(createElement|Fragment)\\b", "g");
289
296
  let hasCompiledJsx = false;
290
297
  code = code.replace(reactJsxRE, (_, prop) => {
@@ -292,11 +299,13 @@ async function restoreJSX(babel2, code, filename) {
292
299
  return "React." + prop;
293
300
  });
294
301
  if (!hasCompiledJsx) {
295
- return [null, false];
302
+ return jsxNotFound;
296
303
  }
297
304
  code = code.replace(/createElement\(Fragment,/g, "createElement(React.Fragment,");
298
305
  babelRestoreJSX || (babelRestoreJSX = Promise.resolve().then(() => (init_babel_restore_jsx(), babel_restore_jsx_exports)));
299
306
  const result = await babel2.transformAsync(code, {
307
+ babelrc: false,
308
+ configFile: false,
300
309
  ast: true,
301
310
  code: false,
302
311
  filename,
@@ -352,31 +361,18 @@ function viteReact(opts = {}) {
352
361
  config.plugins.forEach((plugin) => (plugin.name === "react-refresh" || plugin !== viteReactJsx && plugin.name === "vite:react-jsx") && config.logger.warn(`[@vitejs/plugin-react] You should stop using "${plugin.name}" since this plugin conflicts with it.`));
353
362
  },
354
363
  async transform(code, id, options) {
355
- var _a2, _b2;
364
+ var _a2, _b2, _c2, _d;
356
365
  const ssr = typeof options === "boolean" ? options : (options == null ? void 0 : options.ssr) === true;
357
366
  const [filepath, querystring = ""] = id.split("?");
358
367
  const [extension = ""] = querystring.match(fileExtensionRE) || filepath.match(fileExtensionRE) || [];
359
368
  if (/\.(mjs|[tj]sx?)$/.test(extension)) {
360
- const plugins = [...userPlugins];
361
- const parserPlugins = [
362
- ...userParserPlugins,
363
- "importMeta",
364
- "topLevelAwait",
365
- "classProperties",
366
- "classPrivateProperties",
367
- "classPrivateMethods"
368
- ];
369
- if (!extension.endsWith(".ts")) {
370
- parserPlugins.push("jsx");
371
- }
372
- const isTypeScript = /\.tsx?$/.test(extension);
373
- if (isTypeScript) {
374
- parserPlugins.push("typescript");
375
- }
376
- const isNodeModules = id.includes("node_modules");
369
+ const isJSX = extension.endsWith("x");
370
+ const isNodeModules = id.includes("/node_modules/");
371
+ const isProjectFile = !isNodeModules && (id[0] === "\0" || id.startsWith(projectRoot + "/"));
372
+ const plugins = isProjectFile ? [...userPlugins] : [];
377
373
  let useFastRefresh = false;
378
374
  if (!skipFastRefresh && !ssr && !isNodeModules) {
379
- const isReactModule = extension.endsWith("x") || code.includes("react");
375
+ const isReactModule = isJSX || code.includes("react");
380
376
  if (isReactModule && filter(id)) {
381
377
  useFastRefresh = true;
382
378
  plugins.push([
@@ -386,10 +382,10 @@ function viteReact(opts = {}) {
386
382
  }
387
383
  }
388
384
  let ast;
389
- if (isNodeModules || extension.endsWith("x")) {
385
+ if (!isProjectFile || isJSX) {
390
386
  if (useAutomaticRuntime) {
391
- const [restoredAst, isCommonJS] = isNodeModules ? await restoreJSX(babel, code, id) : [null, false];
392
- if (!isNodeModules || (ast = restoredAst)) {
387
+ const [restoredAst, isCommonJS] = !isProjectFile && !isJSX ? await restoreJSX(babel, code, id) : [null, false];
388
+ if (isJSX || (ast = restoredAst)) {
393
389
  plugins.push([
394
390
  await loadPlugin("@babel/plugin-transform-react-jsx" + (isProduction ? "" : "-development")),
395
391
  {
@@ -401,7 +397,7 @@ function viteReact(opts = {}) {
401
397
  plugins.push(babelImportToRequire);
402
398
  }
403
399
  }
404
- } else if (!isNodeModules) {
400
+ } else if (isProjectFile) {
405
401
  if (!isProduction) {
406
402
  plugins.push(await loadPlugin("@babel/plugin-transform-react-jsx-self"), await loadPlugin("@babel/plugin-transform-react-jsx-source"));
407
403
  }
@@ -410,6 +406,24 @@ function viteReact(opts = {}) {
410
406
  }
411
407
  }
412
408
  }
409
+ const shouldSkip = !plugins.length && !((_a2 = opts.babel) == null ? void 0 : _a2.configFile) && !(isProjectFile && ((_b2 = opts.babel) == null ? void 0 : _b2.babelrc));
410
+ if (shouldSkip) {
411
+ return;
412
+ }
413
+ const parserPlugins = [
414
+ ...userParserPlugins,
415
+ "importMeta",
416
+ "topLevelAwait",
417
+ "classProperties",
418
+ "classPrivateProperties",
419
+ "classPrivateMethods"
420
+ ];
421
+ if (!extension.endsWith(".ts")) {
422
+ parserPlugins.push("jsx");
423
+ }
424
+ if (/\.tsx?$/.test(extension)) {
425
+ parserPlugins.push("typescript");
426
+ }
413
427
  const isReasonReact = extension.endsWith(".bs.js");
414
428
  const babelOpts = __spreadProps(__spreadValues({
415
429
  babelrc: false,
@@ -418,13 +432,13 @@ function viteReact(opts = {}) {
418
432
  ast: !isReasonReact,
419
433
  root: projectRoot,
420
434
  filename: id,
421
- sourceFileName: id,
422
- parserOpts: __spreadProps(__spreadValues({}, (_a2 = opts.babel) == null ? void 0 : _a2.parserOpts), {
435
+ sourceFileName: filepath,
436
+ parserOpts: __spreadProps(__spreadValues({}, (_c2 = opts.babel) == null ? void 0 : _c2.parserOpts), {
423
437
  sourceType: "module",
424
438
  allowAwaitOutsideFunction: true,
425
439
  plugins: parserPlugins
426
440
  }),
427
- generatorOpts: __spreadProps(__spreadValues({}, (_b2 = opts.babel) == null ? void 0 : _b2.generatorOpts), {
441
+ generatorOpts: __spreadProps(__spreadValues({}, (_d = opts.babel) == null ? void 0 : _d.generatorOpts), {
428
442
  decoratorsBeforeExport: true
429
443
  }),
430
444
  plugins,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-react",
3
- "version": "1.1.0-beta.0",
3
+ "version": "1.1.2",
4
4
  "license": "MIT",
5
5
  "author": "Evan You",
6
6
  "contributors": [
@@ -18,7 +18,7 @@
18
18
  "build-bundle": "esbuild src/index.ts --bundle --platform=node --target=node12 --external:@babel/* --external:@rollup/* --external:resolve --external:react-refresh/* --outfile=dist/index.js",
19
19
  "build-types": "tsc -p . --emitDeclarationOnly --outDir temp && api-extractor run && rimraf temp",
20
20
  "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path . --lerna-package plugin-react",
21
- "release": "node ../../scripts/release.js"
21
+ "release": "node ../../scripts/release.cjs"
22
22
  },
23
23
  "engines": {
24
24
  "node": ">=12.0.0"
@@ -33,13 +33,13 @@
33
33
  },
34
34
  "homepage": "https://github.com/vitejs/vite/tree/main/packages/plugin-react#readme",
35
35
  "dependencies": {
36
- "@babel/core": "^7.15.5",
37
- "@babel/plugin-transform-react-jsx": "^7.14.9",
38
- "@babel/plugin-transform-react-jsx-development": "^7.14.5",
39
- "@babel/plugin-transform-react-jsx-self": "^7.14.9",
40
- "@babel/plugin-transform-react-jsx-source": "^7.14.5",
36
+ "@babel/core": "^7.16.0",
37
+ "@babel/plugin-transform-react-jsx": "^7.16.0",
38
+ "@babel/plugin-transform-react-jsx-development": "^7.16.0",
39
+ "@babel/plugin-transform-react-jsx-self": "^7.16.0",
40
+ "@babel/plugin-transform-react-jsx-source": "^7.16.0",
41
41
  "@rollup/pluginutils": "^4.1.1",
42
- "react-refresh": "^0.10.0",
42
+ "react-refresh": "^0.11.0",
43
43
  "resolve": "^1.20.0"
44
44
  }
45
45
  }
package/src/index.ts CHANGED
@@ -108,36 +108,17 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
108
108
  []
109
109
 
110
110
  if (/\.(mjs|[tj]sx?)$/.test(extension)) {
111
- const plugins = [...userPlugins]
111
+ const isJSX = extension.endsWith('x')
112
+ const isNodeModules = id.includes('/node_modules/')
113
+ const isProjectFile =
114
+ !isNodeModules && (id[0] === '\0' || id.startsWith(projectRoot + '/'))
112
115
 
113
- const parserPlugins: typeof userParserPlugins = [
114
- ...userParserPlugins,
115
- 'importMeta',
116
- // This plugin is applied before esbuild transforms the code,
117
- // so we need to enable some stage 3 syntax that is supported in
118
- // TypeScript and some environments already.
119
- 'topLevelAwait',
120
- 'classProperties',
121
- 'classPrivateProperties',
122
- 'classPrivateMethods'
123
- ]
124
-
125
- if (!extension.endsWith('.ts')) {
126
- parserPlugins.push('jsx')
127
- }
128
-
129
- const isTypeScript = /\.tsx?$/.test(extension)
130
- if (isTypeScript) {
131
- parserPlugins.push('typescript')
132
- }
133
-
134
- const isNodeModules = id.includes('node_modules')
116
+ const plugins = isProjectFile ? [...userPlugins] : []
135
117
 
136
118
  let useFastRefresh = false
137
119
  if (!skipFastRefresh && !ssr && !isNodeModules) {
138
120
  // Modules with .js or .ts extension must import React.
139
- const isReactModule =
140
- extension.endsWith('x') || code.includes('react')
121
+ const isReactModule = isJSX || code.includes('react')
141
122
  if (isReactModule && filter(id)) {
142
123
  useFastRefresh = true
143
124
  plugins.push([
@@ -148,16 +129,17 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
148
129
  }
149
130
 
150
131
  let ast: t.File | null | undefined
151
- if (isNodeModules || extension.endsWith('x')) {
132
+ if (!isProjectFile || isJSX) {
152
133
  if (useAutomaticRuntime) {
153
134
  // By reverse-compiling "React.createElement" calls into JSX,
154
135
  // React elements provided by dependencies will also use the
155
136
  // automatic runtime!
156
- const [restoredAst, isCommonJS] = isNodeModules
157
- ? await restoreJSX(babel, code, id)
158
- : [null, false]
137
+ const [restoredAst, isCommonJS] =
138
+ !isProjectFile && !isJSX
139
+ ? await restoreJSX(babel, code, id)
140
+ : [null, false]
159
141
 
160
- if (!isNodeModules || (ast = restoredAst)) {
142
+ if (isJSX || (ast = restoredAst)) {
161
143
  plugins.push([
162
144
  await loadPlugin(
163
145
  '@babel/plugin-transform-react-jsx' +
@@ -174,7 +156,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
174
156
  plugins.push(babelImportToRequire)
175
157
  }
176
158
  }
177
- } else if (!isNodeModules) {
159
+ } else if (isProjectFile) {
178
160
  // These plugins are only needed for the classic runtime.
179
161
  if (!isProduction) {
180
162
  plugins.push(
@@ -191,6 +173,39 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
191
173
  }
192
174
  }
193
175
 
176
+ // Plugins defined through this Vite plugin are only applied
177
+ // to modules within the project root, but "babel.config.js"
178
+ // files can define plugins that need to be applied to every
179
+ // module, including node_modules and linked packages.
180
+ const shouldSkip =
181
+ !plugins.length &&
182
+ !opts.babel?.configFile &&
183
+ !(isProjectFile && opts.babel?.babelrc)
184
+
185
+ if (shouldSkip) {
186
+ return // Avoid parsing if no plugins exist.
187
+ }
188
+
189
+ const parserPlugins: typeof userParserPlugins = [
190
+ ...userParserPlugins,
191
+ 'importMeta',
192
+ // This plugin is applied before esbuild transforms the code,
193
+ // so we need to enable some stage 3 syntax that is supported in
194
+ // TypeScript and some environments already.
195
+ 'topLevelAwait',
196
+ 'classProperties',
197
+ 'classPrivateProperties',
198
+ 'classPrivateMethods'
199
+ ]
200
+
201
+ if (!extension.endsWith('.ts')) {
202
+ parserPlugins.push('jsx')
203
+ }
204
+
205
+ if (/\.tsx?$/.test(extension)) {
206
+ parserPlugins.push('typescript')
207
+ }
208
+
194
209
  const isReasonReact = extension.endsWith('.bs.js')
195
210
 
196
211
  const babelOpts: TransformOptions = {
@@ -200,7 +215,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
200
215
  ast: !isReasonReact,
201
216
  root: projectRoot,
202
217
  filename: id,
203
- sourceFileName: id,
218
+ sourceFileName: filepath,
204
219
  parserOpts: {
205
220
  ...opts.babel?.parserOpts,
206
221
  sourceType: 'module',
@@ -4,13 +4,25 @@ type RestoredJSX = [result: t.File | null | undefined, isCommonJS: boolean]
4
4
 
5
5
  let babelRestoreJSX: Promise<PluginItem> | undefined
6
6
 
7
+ const jsxNotFound: RestoredJSX = [null, false]
8
+
7
9
  /** Restore JSX from `React.createElement` calls */
8
10
  export async function restoreJSX(
9
11
  babel: typeof import('@babel/core'),
10
12
  code: string,
11
13
  filename: string
12
14
  ): Promise<RestoredJSX> {
15
+ // Avoid parsing the optimized react-dom since it will never
16
+ // contain compiled JSX and it's a pretty big file (800kb).
17
+ if (filename.includes('/.vite/react-dom.js')) {
18
+ return jsxNotFound
19
+ }
20
+
13
21
  const [reactAlias, isCommonJS] = parseReactAlias(code)
22
+ if (!reactAlias) {
23
+ return jsxNotFound
24
+ }
25
+
14
26
  const reactJsxRE = new RegExp(
15
27
  '\\b' + reactAlias + '\\.(createElement|Fragment)\\b',
16
28
  'g'
@@ -24,7 +36,7 @@ export async function restoreJSX(
24
36
  })
25
37
 
26
38
  if (!hasCompiledJsx) {
27
- return [null, false]
39
+ return jsxNotFound
28
40
  }
29
41
 
30
42
  // Support modules that use `import {Fragment} from 'react'`
@@ -36,6 +48,8 @@ export async function restoreJSX(
36
48
  babelRestoreJSX ||= import('./babel-restore-jsx')
37
49
 
38
50
  const result = await babel.transformAsync(code, {
51
+ babelrc: false,
52
+ configFile: false,
39
53
  ast: true,
40
54
  code: false,
41
55
  filename,