@vitejs/plugin-react 2.0.0-beta.1 → 2.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/dist/index.cjs CHANGED
@@ -3,6 +3,7 @@
3
3
  const path = require('node:path');
4
4
  const babel = require('@babel/core');
5
5
  const vite = require('vite');
6
+ const MagicString = require('magic-string');
6
7
  const fs = require('node:fs');
7
8
  const node_module = require('node:module');
8
9
 
@@ -22,6 +23,7 @@ function _interopNamespace(e) {
22
23
 
23
24
  const path__default = /*#__PURE__*/_interopDefaultLegacy(path);
24
25
  const babel__namespace = /*#__PURE__*/_interopNamespace(babel);
26
+ const MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
25
27
  const fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
26
28
 
27
29
  const runtimePublicPath = "/@react-refresh";
@@ -183,10 +185,12 @@ function parseReactAlias(code) {
183
185
  return [void 0, false];
184
186
  }
185
187
 
188
+ const prependReactImportCode = "import React from 'react'; ";
186
189
  function viteReact(opts = {}) {
187
190
  let devBase = "/";
188
191
  let resolvedCacheDir;
189
192
  let filter = vite.createFilter(opts.include, opts.exclude);
193
+ let needHiresSourcemap = false;
190
194
  let isProduction = true;
191
195
  let projectRoot = process.cwd();
192
196
  let skipFastRefresh = opts.fastRefresh === false;
@@ -217,6 +221,7 @@ function viteReact(opts = {}) {
217
221
  filter = vite.createFilter(opts.include, opts.exclude, {
218
222
  resolve: projectRoot
219
223
  });
224
+ needHiresSourcemap = config.command === "build" && !!config.build.sourcemap;
220
225
  isProduction = config.isProduction;
221
226
  skipFastRefresh || (skipFastRefresh = isProduction || config.command === "build");
222
227
  const jsxInject = config.esbuild && config.esbuild.jsxInject;
@@ -273,6 +278,7 @@ function viteReact(opts = {}) {
273
278
  }
274
279
  }
275
280
  let ast;
281
+ let prependReactImport = false;
276
282
  if (!isProjectFile || isJSX) {
277
283
  if (useAutomaticRuntime) {
278
284
  const isOptimizedReactDom = id.startsWith(resolvedCacheDir) && id.includes("/react-dom.js");
@@ -295,14 +301,26 @@ function viteReact(opts = {}) {
295
301
  plugins.push(await loadPlugin("@babel/plugin-transform-react-jsx-self"), await loadPlugin("@babel/plugin-transform-react-jsx-source"));
296
302
  }
297
303
  if (!skipReactImport && !importReactRE.test(code)) {
298
- code = `import React from 'react'; ` + code;
304
+ prependReactImport = true;
299
305
  }
300
306
  }
301
307
  }
308
+ let inputMap;
309
+ if (prependReactImport) {
310
+ if (needHiresSourcemap) {
311
+ const s = new MagicString__default(code);
312
+ s.prepend(prependReactImportCode);
313
+ code = s.toString();
314
+ inputMap = s.generateMap({ hires: true, source: id });
315
+ } else {
316
+ code = prependReactImportCode + code;
317
+ }
318
+ }
302
319
  const shouldSkip = !plugins.length && !babelOptions.configFile && !(isProjectFile && babelOptions.babelrc);
303
320
  if (shouldSkip) {
304
321
  return {
305
- code
322
+ code,
323
+ map: inputMap ?? null
306
324
  };
307
325
  }
308
326
  const parserPlugins = [
@@ -339,7 +357,7 @@ function viteReact(opts = {}) {
339
357
  },
340
358
  plugins,
341
359
  sourceMaps: true,
342
- inputSourceMap: false
360
+ inputSourceMap: inputMap ?? false
343
361
  });
344
362
  if (result) {
345
363
  let code2 = result.code;
package/dist/index.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import path from 'node:path';
2
2
  import * as babel from '@babel/core';
3
3
  import { createFilter, normalizePath } from 'vite';
4
+ import MagicString from 'magic-string';
4
5
  import fs from 'node:fs';
5
6
  import { createRequire } from 'node:module';
6
7
 
@@ -163,10 +164,12 @@ function parseReactAlias(code) {
163
164
  return [void 0, false];
164
165
  }
165
166
 
167
+ const prependReactImportCode = "import React from 'react'; ";
166
168
  function viteReact(opts = {}) {
167
169
  let devBase = "/";
168
170
  let resolvedCacheDir;
169
171
  let filter = createFilter(opts.include, opts.exclude);
172
+ let needHiresSourcemap = false;
170
173
  let isProduction = true;
171
174
  let projectRoot = process.cwd();
172
175
  let skipFastRefresh = opts.fastRefresh === false;
@@ -197,6 +200,7 @@ function viteReact(opts = {}) {
197
200
  filter = createFilter(opts.include, opts.exclude, {
198
201
  resolve: projectRoot
199
202
  });
203
+ needHiresSourcemap = config.command === "build" && !!config.build.sourcemap;
200
204
  isProduction = config.isProduction;
201
205
  skipFastRefresh || (skipFastRefresh = isProduction || config.command === "build");
202
206
  const jsxInject = config.esbuild && config.esbuild.jsxInject;
@@ -253,6 +257,7 @@ function viteReact(opts = {}) {
253
257
  }
254
258
  }
255
259
  let ast;
260
+ let prependReactImport = false;
256
261
  if (!isProjectFile || isJSX) {
257
262
  if (useAutomaticRuntime) {
258
263
  const isOptimizedReactDom = id.startsWith(resolvedCacheDir) && id.includes("/react-dom.js");
@@ -275,14 +280,26 @@ function viteReact(opts = {}) {
275
280
  plugins.push(await loadPlugin("@babel/plugin-transform-react-jsx-self"), await loadPlugin("@babel/plugin-transform-react-jsx-source"));
276
281
  }
277
282
  if (!skipReactImport && !importReactRE.test(code)) {
278
- code = `import React from 'react'; ` + code;
283
+ prependReactImport = true;
279
284
  }
280
285
  }
281
286
  }
287
+ let inputMap;
288
+ if (prependReactImport) {
289
+ if (needHiresSourcemap) {
290
+ const s = new MagicString(code);
291
+ s.prepend(prependReactImportCode);
292
+ code = s.toString();
293
+ inputMap = s.generateMap({ hires: true, source: id });
294
+ } else {
295
+ code = prependReactImportCode + code;
296
+ }
297
+ }
282
298
  const shouldSkip = !plugins.length && !babelOptions.configFile && !(isProjectFile && babelOptions.babelrc);
283
299
  if (shouldSkip) {
284
300
  return {
285
- code
301
+ code,
302
+ map: inputMap ?? null
286
303
  };
287
304
  }
288
305
  const parserPlugins = [
@@ -319,7 +336,7 @@ function viteReact(opts = {}) {
319
336
  },
320
337
  plugins,
321
338
  sourceMaps: true,
322
- inputSourceMap: false
339
+ inputSourceMap: inputMap ?? false
323
340
  });
324
341
  if (result) {
325
342
  let code2 = result.code;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-react",
3
- "version": "2.0.0-beta.1",
3
+ "version": "2.0.0",
4
4
  "license": "MIT",
5
5
  "author": "Evan You",
6
6
  "contributors": [
@@ -44,10 +44,11 @@
44
44
  "@babel/plugin-transform-react-jsx-development": "^7.18.6",
45
45
  "@babel/plugin-transform-react-jsx-self": "^7.18.6",
46
46
  "@babel/plugin-transform-react-jsx-source": "^7.18.6",
47
+ "magic-string": "^0.26.2",
47
48
  "react-refresh": "^0.14.0"
48
49
  },
49
50
  "peerDependencies": {
50
- "vite": "^3.0.0-alpha.11"
51
+ "vite": "^3.0.0"
51
52
  },
52
53
  "devDependencies": {
53
54
  "vite": "workspace:*"
package/src/index.ts CHANGED
@@ -3,6 +3,8 @@ import type { ParserOptions, TransformOptions, types as t } from '@babel/core'
3
3
  import * as babel from '@babel/core'
4
4
  import { createFilter, normalizePath } from 'vite'
5
5
  import type { Plugin, PluginOption, ResolvedConfig } from 'vite'
6
+ import MagicString from 'magic-string'
7
+ import type { SourceMap } from 'magic-string'
6
8
  import {
7
9
  addRefreshWrapper,
8
10
  isRefreshBoundary,
@@ -88,11 +90,14 @@ declare module 'vite' {
88
90
  }
89
91
  }
90
92
 
93
+ const prependReactImportCode = "import React from 'react'; "
94
+
91
95
  export default function viteReact(opts: Options = {}): PluginOption[] {
92
96
  // Provide default values for Rollup compat.
93
97
  let devBase = '/'
94
98
  let resolvedCacheDir: string
95
99
  let filter = createFilter(opts.include, opts.exclude)
100
+ let needHiresSourcemap = false
96
101
  let isProduction = true
97
102
  let projectRoot = process.cwd()
98
103
  let skipFastRefresh = opts.fastRefresh === false
@@ -135,6 +140,8 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
135
140
  filter = createFilter(opts.include, opts.exclude, {
136
141
  resolve: projectRoot
137
142
  })
143
+ needHiresSourcemap =
144
+ config.command === 'build' && !!config.build.sourcemap
138
145
  isProduction = config.isProduction
139
146
  skipFastRefresh ||= isProduction || config.command === 'build'
140
147
 
@@ -217,6 +224,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
217
224
  }
218
225
 
219
226
  let ast: t.File | null | undefined
227
+ let prependReactImport = false
220
228
  if (!isProjectFile || isJSX) {
221
229
  if (useAutomaticRuntime) {
222
230
  // By reverse-compiling "React.createElement" calls into JSX,
@@ -261,11 +269,23 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
261
269
  // Even if the automatic JSX runtime is not used, we can still
262
270
  // inject the React import for .jsx and .tsx modules.
263
271
  if (!skipReactImport && !importReactRE.test(code)) {
264
- code = `import React from 'react'; ` + code
272
+ prependReactImport = true
265
273
  }
266
274
  }
267
275
  }
268
276
 
277
+ let inputMap: SourceMap | undefined
278
+ if (prependReactImport) {
279
+ if (needHiresSourcemap) {
280
+ const s = new MagicString(code)
281
+ s.prepend(prependReactImportCode)
282
+ code = s.toString()
283
+ inputMap = s.generateMap({ hires: true, source: id })
284
+ } else {
285
+ code = prependReactImportCode + code
286
+ }
287
+ }
288
+
269
289
  // Plugins defined through this Vite plugin are only applied
270
290
  // to modules within the project root, but "babel.config.js"
271
291
  // files can define plugins that need to be applied to every
@@ -275,10 +295,11 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
275
295
  !babelOptions.configFile &&
276
296
  !(isProjectFile && babelOptions.babelrc)
277
297
 
298
+ // Avoid parsing if no plugins exist.
278
299
  if (shouldSkip) {
279
- // Avoid parsing if no plugins exist.
280
300
  return {
281
- code
301
+ code,
302
+ map: inputMap ?? null
282
303
  }
283
304
  }
284
305
 
@@ -326,7 +347,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
326
347
  plugins,
327
348
  sourceMaps: true,
328
349
  // Vite handles sourcemap flattening
329
- inputSourceMap: false as any
350
+ inputSourceMap: inputMap ?? (false as any)
330
351
  })
331
352
 
332
353
  if (result) {