@vitejs/plugin-react 1.3.2 → 2.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.d.ts CHANGED
@@ -1,62 +1,62 @@
1
- import type { ParserOptions } from '@babel/core';
2
- import type { PluginOption } from 'vite';
3
- import type { TransformOptions } from '@babel/core';
4
-
5
- export declare type BabelOptions = Omit<TransformOptions, 'ast' | 'filename' | 'root' | 'sourceFileName' | 'sourceMaps' | 'inputSourceMap'>;
6
-
7
- export declare interface Options {
8
- include?: string | RegExp | Array<string | RegExp>;
9
- exclude?: string | RegExp | Array<string | RegExp>;
10
- /**
11
- * Enable `react-refresh` integration. Vite disables this in prod env or build mode.
12
- * @default true
13
- */
14
- fastRefresh?: boolean;
15
- /**
16
- * Set this to `"automatic"` to use [vite-react-jsx](https://github.com/alloc/vite-react-jsx).
17
- * @default "automatic"
18
- */
19
- jsxRuntime?: 'classic' | 'automatic';
20
- /**
21
- * Control where the JSX factory is imported from.
22
- * This option is ignored when `jsxRuntime` is not `"automatic"`.
23
- * @default "react"
24
- */
25
- jsxImportSource?: string;
26
- /**
27
- * Set this to `true` to annotate the JSX factory with `\/* @__PURE__ *\/`.
28
- * This option is ignored when `jsxRuntime` is not `"automatic"`.
29
- * @default true
30
- */
31
- jsxPure?: boolean;
32
- /**
33
- * Babel configuration applied in both dev and prod.
34
- */
35
- babel?: BabelOptions;
36
- /**
37
- * @deprecated Use `babel.parserOpts.plugins` instead
38
- */
39
- parserPlugins?: ParserOptions['plugins'];
40
- }
41
-
42
- /**
43
- * The object type used by the `options` passed to plugins with
44
- * an `api.reactBabel` method.
45
- */
46
- export declare interface ReactBabelOptions extends BabelOptions {
47
- plugins: Extract<BabelOptions['plugins'], any[]>;
48
- presets: Extract<BabelOptions['presets'], any[]>;
49
- overrides: Extract<BabelOptions['overrides'], any[]>;
50
- parserOpts: ParserOptions & {
51
- plugins: Extract<ParserOptions['plugins'], any[]>;
52
- };
53
- }
54
-
55
- declare function viteReact(opts?: Options): PluginOption[];
56
-
57
- declare namespace viteReact {
58
- var preambleCode: string;
59
- }
60
- export default viteReact;
61
-
62
- export { }
1
+ import { TransformOptions, ParserOptions } from '@babel/core';
2
+ import { ResolvedConfig, PluginOption } from 'vite';
3
+
4
+ interface Options {
5
+ include?: string | RegExp | Array<string | RegExp>;
6
+ exclude?: string | RegExp | Array<string | RegExp>;
7
+ /**
8
+ * Enable `react-refresh` integration. Vite disables this in prod env or build mode.
9
+ * @default true
10
+ */
11
+ fastRefresh?: boolean;
12
+ /**
13
+ * Set this to `"automatic"` to use [vite-react-jsx](https://github.com/alloc/vite-react-jsx).
14
+ * @default "automatic"
15
+ */
16
+ jsxRuntime?: 'classic' | 'automatic';
17
+ /**
18
+ * Control where the JSX factory is imported from.
19
+ * This option is ignored when `jsxRuntime` is not `"automatic"`.
20
+ * @default "react"
21
+ */
22
+ jsxImportSource?: string;
23
+ /**
24
+ * Set this to `true` to annotate the JSX factory with `\/* @__PURE__ *\/`.
25
+ * This option is ignored when `jsxRuntime` is not `"automatic"`.
26
+ * @default true
27
+ */
28
+ jsxPure?: boolean;
29
+ /**
30
+ * Babel configuration applied in both dev and prod.
31
+ */
32
+ babel?: BabelOptions;
33
+ }
34
+ declare type BabelOptions = Omit<TransformOptions, 'ast' | 'filename' | 'root' | 'sourceFileName' | 'sourceMaps' | 'inputSourceMap'>;
35
+ /**
36
+ * The object type used by the `options` passed to plugins with
37
+ * an `api.reactBabel` method.
38
+ */
39
+ interface ReactBabelOptions extends BabelOptions {
40
+ plugins: Extract<BabelOptions['plugins'], any[]>;
41
+ presets: Extract<BabelOptions['presets'], any[]>;
42
+ overrides: Extract<BabelOptions['overrides'], any[]>;
43
+ parserOpts: ParserOptions & {
44
+ plugins: Extract<ParserOptions['plugins'], any[]>;
45
+ };
46
+ }
47
+ declare module 'vite' {
48
+ interface Plugin {
49
+ api?: {
50
+ /**
51
+ * Manipulate the Babel options of `@vitejs/plugin-react`
52
+ */
53
+ reactBabel?: (options: ReactBabelOptions, config: ResolvedConfig) => void;
54
+ };
55
+ }
56
+ }
57
+ declare function viteReact(opts?: Options): PluginOption[];
58
+ declare namespace viteReact {
59
+ var preambleCode: string;
60
+ }
61
+
62
+ export { BabelOptions, Options, ReactBabelOptions, viteReact as default };
@@ -1,191 +1,15 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __defProps = Object.defineProperties;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
- var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
- var __getProtoOf = Object.getPrototypeOf;
9
- var __hasOwnProp = Object.prototype.hasOwnProperty;
10
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
11
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
- var __spreadValues = (a, b) => {
13
- for (var prop in b || (b = {}))
14
- if (__hasOwnProp.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- if (__getOwnPropSymbols)
17
- for (var prop of __getOwnPropSymbols(b)) {
18
- if (__propIsEnum.call(b, prop))
19
- __defNormalProp(a, prop, b[prop]);
20
- }
21
- return a;
22
- };
23
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
24
- var __esm = (fn, res) => function __init() {
25
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
26
- };
27
- var __export = (target, all) => {
28
- for (var name in all)
29
- __defProp(target, name, { get: all[name], enumerable: true });
30
- };
31
- var __copyProps = (to, from, except, desc) => {
32
- if (from && typeof from === "object" || typeof from === "function") {
33
- for (let key of __getOwnPropNames(from))
34
- if (!__hasOwnProp.call(to, key) && key !== except)
35
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
36
- }
37
- return to;
38
- };
39
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
40
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
1
+ import * as babel from '@babel/core';
2
+ import { createFilter } from '@rollup/pluginutils';
3
+ import resolve from 'resolve';
4
+ import fs from 'fs';
5
+ import path from 'path';
41
6
 
42
- // src/jsx-runtime/babel-restore-jsx.ts
43
- var babel_restore_jsx_exports = {};
44
- __export(babel_restore_jsx_exports, {
45
- default: () => babel_restore_jsx_default
46
- });
47
- function babel_restore_jsx_default({ types: t }) {
48
- function getJSXNode(node) {
49
- if (!isReactCreateElement(node)) {
50
- return null;
51
- }
52
- const [nameNode, propsNode, ...childNodes] = node.arguments;
53
- const name = getJSXName(nameNode);
54
- if (name == null) {
55
- return null;
56
- }
57
- const props = getJSXProps(propsNode);
58
- if (props == null) {
59
- return null;
60
- }
61
- const children = getJSXChildren(childNodes);
62
- if (children == null) {
63
- return null;
64
- }
65
- if (t.isJSXMemberExpression(name) && t.isJSXIdentifier(name.object) && name.object.name === "React" && name.property.name === "Fragment") {
66
- return t.jsxFragment(t.jsxOpeningFragment(), t.jsxClosingFragment(), children);
67
- }
68
- const selfClosing = children.length === 0;
69
- const startTag = t.jsxOpeningElement(name, props, selfClosing);
70
- startTag.loc = node.loc;
71
- const endTag = selfClosing ? null : t.jsxClosingElement(name);
72
- return t.jsxElement(startTag, endTag, children, selfClosing);
73
- }
74
- function getJSXName(node) {
75
- if (node == null) {
76
- return null;
77
- }
78
- const name = getJSXIdentifier(node, true);
79
- if (name != null) {
80
- return name;
81
- }
82
- if (!t.isMemberExpression(node)) {
83
- return null;
84
- }
85
- const object = getJSXName(node.object);
86
- const property = getJSXName(node.property);
87
- if (object == null || property == null) {
88
- return null;
89
- }
90
- return t.jsxMemberExpression(object, property);
91
- }
92
- function getJSXProps(node) {
93
- if (node == null || isNullLikeNode(node)) {
94
- return [];
95
- }
96
- if (t.isCallExpression(node) && t.isIdentifier(node.callee, { name: "_extends" })) {
97
- const props = node.arguments.map(getJSXProps);
98
- if (props.every((prop) => prop != null)) {
99
- return [].concat(...props);
100
- }
101
- }
102
- if (!t.isObjectExpression(node) && t.isExpression(node))
103
- return [t.jsxSpreadAttribute(node)];
104
- if (!isPlainObjectExpression(node)) {
105
- return null;
106
- }
107
- return node.properties.map((prop) => t.isObjectProperty(prop) ? t.jsxAttribute(getJSXIdentifier(prop.key), getJSXAttributeValue(prop.value)) : t.jsxSpreadAttribute(prop.argument));
108
- }
109
- function getJSXChild(node) {
110
- if (t.isStringLiteral(node)) {
111
- return t.jsxText(node.value);
112
- }
113
- if (isReactCreateElement(node)) {
114
- return getJSXNode(node);
115
- }
116
- if (t.isExpression(node)) {
117
- return t.jsxExpressionContainer(node);
118
- }
119
- return null;
120
- }
121
- function getJSXChildren(nodes) {
122
- const children = nodes.filter((node) => !isNullLikeNode(node)).map(getJSXChild);
123
- if (children.some((child) => child == null)) {
124
- return null;
125
- }
126
- return children;
127
- }
128
- function getJSXIdentifier(node, tag = false) {
129
- if (t.isIdentifier(node) && (!tag || node.name.match(/^[A-Z]/))) {
130
- return t.jsxIdentifier(node.name);
131
- }
132
- if (t.isStringLiteral(node)) {
133
- return t.jsxIdentifier(node.value);
134
- }
135
- return null;
136
- }
137
- function getJSXAttributeValue(node) {
138
- if (t.isStringLiteral(node)) {
139
- return node;
140
- }
141
- if (t.isJSXElement(node)) {
142
- return node;
143
- }
144
- if (t.isExpression(node)) {
145
- return t.jsxExpressionContainer(node);
146
- }
147
- return null;
148
- }
149
- const isReactCreateElement = (node) => t.isCallExpression(node) && t.isMemberExpression(node.callee) && t.isIdentifier(node.callee.object, { name: "React" }) && t.isIdentifier(node.callee.property, { name: "createElement" }) && !node.callee.computed;
150
- const isNullLikeNode = (node) => t.isNullLiteral(node) || t.isIdentifier(node, { name: "undefined" });
151
- const isPlainObjectExpression = (node) => t.isObjectExpression(node) && node.properties.every((property) => t.isSpreadElement(property) || t.isObjectProperty(property, { computed: false }) && getJSXIdentifier(property.key) != null && getJSXAttributeValue(property.value) != null);
152
- return {
153
- visitor: {
154
- CallExpression(path2) {
155
- const node = getJSXNode(path2.node);
156
- if (node == null) {
157
- return null;
158
- }
159
- path2.replaceWith(node);
160
- }
161
- }
162
- };
163
- }
164
- var init_babel_restore_jsx = __esm({
165
- "src/jsx-runtime/babel-restore-jsx.ts"() {
166
- }
167
- });
168
-
169
- // src/index.ts
170
- var src_exports = {};
171
- __export(src_exports, {
172
- default: () => viteReact
173
- });
174
- module.exports = viteReact;
175
- viteReact['default'] = viteReact;
176
- var babel = __toESM(require("@babel/core"));
177
- var import_pluginutils = require("@rollup/pluginutils");
178
- var import_resolve = __toESM(require("resolve"));
179
-
180
- // src/fast-refresh.ts
181
- var import_fs = __toESM(require("fs"));
182
- var import_path = __toESM(require("path"));
183
- var runtimePublicPath = "/@react-refresh";
184
- var reactRefreshDir = import_path.default.dirname(require.resolve("react-refresh/package.json"));
185
- var runtimeFilePath = import_path.default.join(reactRefreshDir, "cjs/react-refresh-runtime.development.js");
186
- var runtimeCode = `
7
+ const runtimePublicPath = "/@react-refresh";
8
+ const reactRefreshDir = path.dirname(require.resolve("react-refresh/package.json"));
9
+ const runtimeFilePath = path.join(reactRefreshDir, "cjs/react-refresh-runtime.development.js");
10
+ const runtimeCode = `
187
11
  const exports = {}
188
- ${import_fs.default.readFileSync(runtimeFilePath, "utf-8")}
12
+ ${fs.readFileSync(runtimeFilePath, "utf-8")}
189
13
  function debounce(fn, delay) {
190
14
  let handle
191
15
  return () => {
@@ -196,14 +20,14 @@ function debounce(fn, delay) {
196
20
  exports.performReactRefresh = debounce(exports.performReactRefresh, 16)
197
21
  export default exports
198
22
  `;
199
- var preambleCode = `
23
+ const preambleCode = `
200
24
  import RefreshRuntime from "__BASE__${runtimePublicPath.slice(1)}"
201
25
  RefreshRuntime.injectIntoGlobalHook(window)
202
26
  window.$RefreshReg$ = () => {}
203
27
  window.$RefreshSig$ = () => (type) => type
204
28
  window.__vite_plugin_react_preamble_installed__ = true
205
29
  `;
206
- var header = `
30
+ const header = `
207
31
  import RefreshRuntime from "${runtimePublicPath}";
208
32
 
209
33
  let prevRefreshReg;
@@ -224,7 +48,7 @@ if (import.meta.hot) {
224
48
  };
225
49
  window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
226
50
  }`.replace(/[\n]+/gm, "");
227
- var footer = `
51
+ const footer = `
228
52
  if (import.meta.hot) {
229
53
  window.$RefreshReg$ = prevRefreshReg;
230
54
  window.$RefreshSig$ = prevRefreshSig;
@@ -266,14 +90,13 @@ function isComponentLikeName(name) {
266
90
  return typeof name === "string" && name[0] >= "A" && name[0] <= "Z";
267
91
  }
268
92
 
269
- // src/jsx-runtime/babel-import-to-require.ts
270
93
  function babelImportToRequire({ types: t }) {
271
94
  return {
272
95
  visitor: {
273
- ImportDeclaration(path2) {
274
- const decl = path2.node;
96
+ ImportDeclaration(path) {
97
+ const decl = path.node;
275
98
  const spec = decl.specifiers[0];
276
- path2.replaceWith(t.variableDeclaration("var", [
99
+ path.replaceWith(t.variableDeclaration("var", [
277
100
  t.variableDeclarator(spec.local, t.memberExpression(t.callExpression(t.identifier("require"), [decl.source]), spec.imported))
278
101
  ]));
279
102
  }
@@ -281,10 +104,19 @@ function babelImportToRequire({ types: t }) {
281
104
  };
282
105
  }
283
106
 
284
- // src/jsx-runtime/restore-jsx.ts
285
- var babelRestoreJSX;
286
- var jsxNotFound = [null, false];
287
- async function restoreJSX(babel2, code, filename) {
107
+ let babelRestoreJSX;
108
+ const jsxNotFound = [null, false];
109
+ async function getBabelRestoreJSX() {
110
+ if (!babelRestoreJSX)
111
+ babelRestoreJSX = import('./chunks/babel-restore-jsx.mjs').then((r) => {
112
+ const fn = r.default;
113
+ if ("default" in fn)
114
+ return fn.default;
115
+ return fn;
116
+ });
117
+ return babelRestoreJSX;
118
+ }
119
+ async function restoreJSX(babel, code, filename) {
288
120
  if (filename.includes("/.vite/react-dom.js")) {
289
121
  return jsxNotFound;
290
122
  }
@@ -308,8 +140,7 @@ async function restoreJSX(babel2, code, filename) {
308
140
  if (!hasCompiledJsx) {
309
141
  return jsxNotFound;
310
142
  }
311
- babelRestoreJSX || (babelRestoreJSX = Promise.resolve().then(() => (init_babel_restore_jsx(), babel_restore_jsx_exports)));
312
- const result = await babel2.transformAsync(code, {
143
+ const result = await babel.transformAsync(code, {
313
144
  babelrc: false,
314
145
  configFile: false,
315
146
  ast: true,
@@ -318,9 +149,9 @@ async function restoreJSX(babel2, code, filename) {
318
149
  parserOpts: {
319
150
  plugins: ["jsx"]
320
151
  },
321
- plugins: [(await babelRestoreJSX).default]
152
+ plugins: [await getBabelRestoreJSX()]
322
153
  });
323
- return [result == null ? void 0 : result.ast, isCommonJS];
154
+ return [result?.ast, isCommonJS];
324
155
  }
325
156
  function parseReactAlias(code) {
326
157
  let match = code.match(/\b(var|let|const) +(\w+) *= *require\(["']react["']\)/);
@@ -334,25 +165,25 @@ function parseReactAlias(code) {
334
165
  return [void 0, false];
335
166
  }
336
167
 
337
- // src/index.ts
338
168
  function viteReact(opts = {}) {
339
169
  var _a;
340
170
  let base = "/";
341
- let filter = (0, import_pluginutils.createFilter)(opts.include, opts.exclude);
171
+ let filter = createFilter(opts.include, opts.exclude);
342
172
  let isProduction = true;
343
173
  let projectRoot = process.cwd();
344
174
  let skipFastRefresh = opts.fastRefresh === false;
345
175
  let skipReactImport = false;
346
176
  const useAutomaticRuntime = opts.jsxRuntime !== "classic";
347
- const babelOptions = __spreadValues({
177
+ const babelOptions = {
348
178
  babelrc: false,
349
- configFile: false
350
- }, opts.babel);
179
+ configFile: false,
180
+ ...opts.babel
181
+ };
351
182
  babelOptions.plugins || (babelOptions.plugins = []);
352
183
  babelOptions.presets || (babelOptions.presets = []);
353
184
  babelOptions.overrides || (babelOptions.overrides = []);
354
185
  babelOptions.parserOpts || (babelOptions.parserOpts = {});
355
- (_a = babelOptions.parserOpts).plugins || (_a.plugins = opts.parserPlugins || []);
186
+ (_a = babelOptions.parserOpts).plugins || (_a.plugins = []);
356
187
  const importReactRE = /(^|\n)import\s+(\*\s+as\s+)?React(,|\s+)/;
357
188
  const fileExtensionRE = /\.[^\/\s\?]+$/;
358
189
  const viteBabel = {
@@ -361,7 +192,7 @@ function viteReact(opts = {}) {
361
192
  configResolved(config) {
362
193
  base = config.base;
363
194
  projectRoot = config.root;
364
- filter = (0, import_pluginutils.createFilter)(opts.include, opts.exclude, {
195
+ filter = createFilter(opts.include, opts.exclude, {
365
196
  resolve: projectRoot
366
197
  });
367
198
  isProduction = config.isProduction;
@@ -372,17 +203,16 @@ function viteReact(opts = {}) {
372
203
  config.logger.warn("[@vitejs/plugin-react] This plugin imports React for you automatically, so you can stop using `esbuild.jsxInject` for that purpose.");
373
204
  }
374
205
  config.plugins.forEach((plugin) => {
375
- var _a2;
376
206
  const hasConflict = plugin.name === "react-refresh" || plugin !== viteReactJsx && plugin.name === "vite:react-jsx";
377
207
  if (hasConflict)
378
208
  return config.logger.warn(`[@vitejs/plugin-react] You should stop using "${plugin.name}" since this plugin conflicts with it.`);
379
- if ((_a2 = plugin.api) == null ? void 0 : _a2.reactBabel) {
209
+ if (plugin.api?.reactBabel) {
380
210
  plugin.api.reactBabel(babelOptions, config);
381
211
  }
382
212
  });
383
213
  },
384
214
  async transform(code, id, options) {
385
- const ssr = typeof options === "boolean" ? options : (options == null ? void 0 : options.ssr) === true;
215
+ const ssr = typeof options === "boolean" ? options : options?.ssr === true;
386
216
  const [filepath, querystring = ""] = id.split("?");
387
217
  const [extension = ""] = querystring.match(fileExtensionRE) || filepath.match(fileExtensionRE) || [];
388
218
  if (/\.(mjs|[tj]sx?)$/.test(extension)) {
@@ -445,25 +275,28 @@ function viteReact(opts = {}) {
445
275
  if (/\.tsx?$/.test(extension)) {
446
276
  parserPlugins.push("typescript");
447
277
  }
448
- const transformAsync2 = ast ? babel.transformFromAstAsync.bind(babel, ast, code) : babel.transformAsync.bind(babel, code);
278
+ const transformAsync = ast ? babel.transformFromAstAsync.bind(babel, ast, code) : babel.transformAsync.bind(babel, code);
449
279
  const isReasonReact = extension.endsWith(".bs.js");
450
- const result = await transformAsync2(__spreadProps(__spreadValues({}, babelOptions), {
280
+ const result = await transformAsync({
281
+ ...babelOptions,
451
282
  ast: !isReasonReact,
452
283
  root: projectRoot,
453
284
  filename: id,
454
285
  sourceFileName: filepath,
455
- parserOpts: __spreadProps(__spreadValues({}, babelOptions.parserOpts), {
286
+ parserOpts: {
287
+ ...babelOptions.parserOpts,
456
288
  sourceType: "module",
457
289
  allowAwaitOutsideFunction: true,
458
290
  plugins: parserPlugins
459
- }),
460
- generatorOpts: __spreadProps(__spreadValues({}, babelOptions.generatorOpts), {
291
+ },
292
+ generatorOpts: {
293
+ ...babelOptions.generatorOpts,
461
294
  decoratorsBeforeExport: true
462
- }),
295
+ },
463
296
  plugins,
464
297
  sourceMaps: true,
465
298
  inputSourceMap: false
466
- }));
299
+ });
467
300
  if (result) {
468
301
  let code2 = result.code;
469
302
  if (useFastRefresh && /\$RefreshReg\$\(/.test(code2)) {
@@ -523,7 +356,7 @@ function viteReact(opts = {}) {
523
356
  },
524
357
  load(id) {
525
358
  if (id === runtimeId) {
526
- const runtimePath = import_resolve.default.sync(runtimeId, {
359
+ const runtimePath = resolve.sync(runtimeId, {
527
360
  basedir: projectRoot
528
361
  });
529
362
  const exports = ["jsx", "jsxs", "Fragment"];
@@ -537,12 +370,8 @@ function viteReact(opts = {}) {
537
370
  return [viteBabel, viteReactRefresh, useAutomaticRuntime && viteReactJsx];
538
371
  }
539
372
  viteReact.preambleCode = preambleCode;
540
- function loadPlugin(path2) {
541
- return Promise.resolve().then(() => __toESM(require(path2))).then((module2) => module2.default || module2);
373
+ function loadPlugin(path) {
374
+ return import(path).then((module) => module.default || module);
542
375
  }
543
- // Annotate the CommonJS export names for ESM import in node:
544
- 0 && (module.exports = {});
545
- /**
546
- * https://github.com/flying-sheep/babel-plugin-transform-react-createelement-to-jsx
547
- * @license GNU General Public License v3.0
548
- */
376
+
377
+ export { viteReact as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-react",
3
- "version": "1.3.2",
3
+ "version": "2.0.0-alpha.0",
4
4
  "license": "MIT",
5
5
  "author": "Evan You",
6
6
  "contributors": [
@@ -10,18 +10,24 @@
10
10
  "dist",
11
11
  "src"
12
12
  ],
13
- "main": "dist/index.js",
14
- "types": "dist/index.d.ts",
13
+ "main": "./dist/index.cjs",
14
+ "module": "./dist/index.mjs",
15
+ "types": "./dist/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.mjs",
20
+ "require": "./dist/index.cjs"
21
+ }
22
+ },
15
23
  "scripts": {
16
- "dev": "tsc -p . -w --incremental",
17
- "build": "rimraf dist && run-s build-bundle build-types",
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 && npm run patch-dist",
19
- "patch-dist": "ts-node ../../scripts/patchEsbuildDist.ts dist/index.js viteReact",
20
- "build-types": "tsc -p . --emitDeclarationOnly --outDir temp && api-extractor run && rimraf temp",
21
- "prepublishOnly": "(cd ../vite && npm run build) && npm run build"
24
+ "dev": "unbuild --stub",
25
+ "build": "unbuild && pnpm run patch-cjs",
26
+ "patch-cjs": "ts-node ../../scripts/patchCJS.ts",
27
+ "prepublishOnly": "npm run build"
22
28
  },
23
29
  "engines": {
24
- "node": ">=12.0.0"
30
+ "node": ">=14.6.0"
25
31
  },
26
32
  "repository": {
27
33
  "type": "git",
@@ -41,5 +47,11 @@
41
47
  "@rollup/pluginutils": "^4.2.1",
42
48
  "react-refresh": "^0.13.0",
43
49
  "resolve": "^1.22.0"
50
+ },
51
+ "peerDependencies": {
52
+ "vite": "^3.0.0-alpha"
53
+ },
54
+ "devDependencies": {
55
+ "vite": "workspace:*"
44
56
  }
45
57
  }
@@ -1,6 +1,6 @@
1
- import type { types as t } from '@babel/core'
2
1
  import fs from 'fs'
3
2
  import path from 'path'
3
+ import type { types as t } from '@babel/core'
4
4
 
5
5
  export const runtimePublicPath = '/@react-refresh'
6
6
 
package/src/index.ts CHANGED
@@ -2,7 +2,7 @@ import type { ParserOptions, TransformOptions, types as t } from '@babel/core'
2
2
  import * as babel from '@babel/core'
3
3
  import { createFilter } from '@rollup/pluginutils'
4
4
  import resolve from 'resolve'
5
- import type { Plugin, PluginOption } from 'vite'
5
+ import type { Plugin, PluginOption, ResolvedConfig } from 'vite'
6
6
  import {
7
7
  addRefreshWrapper,
8
8
  isRefreshBoundary,
@@ -38,15 +38,10 @@ export interface Options {
38
38
  * @default true
39
39
  */
40
40
  jsxPure?: boolean
41
-
42
41
  /**
43
42
  * Babel configuration applied in both dev and prod.
44
43
  */
45
44
  babel?: BabelOptions
46
- /**
47
- * @deprecated Use `babel.parserOpts.plugins` instead
48
- */
49
- parserPlugins?: ParserOptions['plugins']
50
45
  }
51
46
 
52
47
  export type BabelOptions = Omit<
@@ -104,7 +99,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
104
99
  babelOptions.presets ||= []
105
100
  babelOptions.overrides ||= []
106
101
  babelOptions.parserOpts ||= {} as any
107
- babelOptions.parserOpts.plugins ||= opts.parserPlugins || []
102
+ babelOptions.parserOpts.plugins ||= []
108
103
 
109
104
  // Support patterns like:
110
105
  // - import * as React from 'react';
@@ -154,7 +149,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
154
149
  },
155
150
  async transform(code, id, options) {
156
151
  const ssr = typeof options === 'boolean' ? options : options?.ssr === true
157
- // File extension could be mocked/overriden in querystring.
152
+ // File extension could be mocked/overridden in querystring.
158
153
  const [filepath, querystring = ''] = id.split('?')
159
154
  const [extension = ''] =
160
155
  querystring.match(fileExtensionRE) ||
@@ -373,10 +368,3 @@ viteReact.preambleCode = preambleCode
373
368
  function loadPlugin(path: string): Promise<any> {
374
369
  return import(path).then((module) => module.default || module)
375
370
  }
376
-
377
- // overwrite for cjs require('...')() usage
378
- // The following lines are inserted by scripts/patchEsbuildDist.ts,
379
- // this doesn't bundle correctly after esbuild 0.14.4
380
- //
381
- // module.exports = viteReact
382
- // viteReact['default'] = viteReact
@@ -1,5 +1,5 @@
1
1
  import type * as babelCore from '@babel/core'
2
- import type { types as t, Visitor } from '@babel/core'
2
+ import type { Visitor, types as t } from '@babel/core'
3
3
 
4
4
  /**
5
5
  * Replace this:
@@ -1,5 +1,6 @@
1
- import babelRestoreJSX from './babel-restore-jsx'
2
1
  import * as babel from '@babel/core'
2
+ import { describe, expect, it } from 'vitest'
3
+ import babelRestoreJSX from './babel-restore-jsx'
3
4
 
4
5
  function jsx(code: string) {
5
6
  return babel.transform(code, {