@vitejs/plugin-react 1.3.1 → 2.0.0-alpha.1
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/chunks/babel-restore-jsx.cjs +125 -0
- package/dist/chunks/babel-restore-jsx.mjs +123 -0
- package/dist/index.cjs +401 -0
- package/dist/index.d.ts +62 -62
- package/dist/{index.js → index.mjs} +68 -231
- package/package.json +25 -13
- package/src/fast-refresh.ts +4 -2
- package/src/index.ts +3 -15
- package/src/jsx-runtime/babel-import-to-require.ts +2 -3
- package/src/jsx-runtime/babel-restore-jsx.spec.ts +2 -1
- package/src/jsx-runtime/restore-jsx.spec.ts +56 -0
- package/src/jsx-runtime/restore-jsx.ts +44 -23
package/dist/index.d.ts
CHANGED
@@ -1,62 +1,62 @@
|
|
1
|
-
import
|
2
|
-
import
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
declare
|
58
|
-
|
59
|
-
|
60
|
-
|
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,17 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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';
|
6
|
+
import { createRequire } from 'module';
|
41
7
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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 = `
|
8
|
+
const runtimePublicPath = "/@react-refresh";
|
9
|
+
const _require = createRequire(import.meta.url);
|
10
|
+
const reactRefreshDir = path.dirname(_require.resolve("react-refresh/package.json"));
|
11
|
+
const runtimeFilePath = path.join(reactRefreshDir, "cjs/react-refresh-runtime.development.js");
|
12
|
+
const runtimeCode = `
|
187
13
|
const exports = {}
|
188
|
-
${
|
14
|
+
${fs.readFileSync(runtimeFilePath, "utf-8")}
|
189
15
|
function debounce(fn, delay) {
|
190
16
|
let handle
|
191
17
|
return () => {
|
@@ -196,14 +22,14 @@ function debounce(fn, delay) {
|
|
196
22
|
exports.performReactRefresh = debounce(exports.performReactRefresh, 16)
|
197
23
|
export default exports
|
198
24
|
`;
|
199
|
-
|
25
|
+
const preambleCode = `
|
200
26
|
import RefreshRuntime from "__BASE__${runtimePublicPath.slice(1)}"
|
201
27
|
RefreshRuntime.injectIntoGlobalHook(window)
|
202
28
|
window.$RefreshReg$ = () => {}
|
203
29
|
window.$RefreshSig$ = () => (type) => type
|
204
30
|
window.__vite_plugin_react_preamble_installed__ = true
|
205
31
|
`;
|
206
|
-
|
32
|
+
const header = `
|
207
33
|
import RefreshRuntime from "${runtimePublicPath}";
|
208
34
|
|
209
35
|
let prevRefreshReg;
|
@@ -224,7 +50,7 @@ if (import.meta.hot) {
|
|
224
50
|
};
|
225
51
|
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
|
226
52
|
}`.replace(/[\n]+/gm, "");
|
227
|
-
|
53
|
+
const footer = `
|
228
54
|
if (import.meta.hot) {
|
229
55
|
window.$RefreshReg$ = prevRefreshReg;
|
230
56
|
window.$RefreshSig$ = prevRefreshSig;
|
@@ -266,14 +92,13 @@ function isComponentLikeName(name) {
|
|
266
92
|
return typeof name === "string" && name[0] >= "A" && name[0] <= "Z";
|
267
93
|
}
|
268
94
|
|
269
|
-
// src/jsx-runtime/babel-import-to-require.ts
|
270
95
|
function babelImportToRequire({ types: t }) {
|
271
96
|
return {
|
272
97
|
visitor: {
|
273
|
-
ImportDeclaration(
|
274
|
-
const decl =
|
98
|
+
ImportDeclaration(path) {
|
99
|
+
const decl = path.node;
|
275
100
|
const spec = decl.specifiers[0];
|
276
|
-
|
101
|
+
path.replaceWith(t.variableDeclaration("var", [
|
277
102
|
t.variableDeclarator(spec.local, t.memberExpression(t.callExpression(t.identifier("require"), [decl.source]), spec.imported))
|
278
103
|
]));
|
279
104
|
}
|
@@ -281,10 +106,19 @@ function babelImportToRequire({ types: t }) {
|
|
281
106
|
};
|
282
107
|
}
|
283
108
|
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
109
|
+
let babelRestoreJSX;
|
110
|
+
const jsxNotFound = [null, false];
|
111
|
+
async function getBabelRestoreJSX() {
|
112
|
+
if (!babelRestoreJSX)
|
113
|
+
babelRestoreJSX = import('./chunks/babel-restore-jsx.mjs').then((r) => {
|
114
|
+
const fn = r.default;
|
115
|
+
if ("default" in fn)
|
116
|
+
return fn.default;
|
117
|
+
return fn;
|
118
|
+
});
|
119
|
+
return babelRestoreJSX;
|
120
|
+
}
|
121
|
+
async function restoreJSX(babel, code, filename) {
|
288
122
|
if (filename.includes("/.vite/react-dom.js")) {
|
289
123
|
return jsxNotFound;
|
290
124
|
}
|
@@ -292,18 +126,23 @@ async function restoreJSX(babel2, code, filename) {
|
|
292
126
|
if (!reactAlias) {
|
293
127
|
return jsxNotFound;
|
294
128
|
}
|
295
|
-
const reactJsxRE = new RegExp("\\b" + reactAlias + "\\.(createElement|Fragment)\\b", "g");
|
296
129
|
let hasCompiledJsx = false;
|
297
|
-
|
130
|
+
const fragmentPattern = `\\b${reactAlias}\\.Fragment\\b`;
|
131
|
+
const createElementPattern = `\\b${reactAlias}\\.createElement\\(\\s*([A-Z"'][\\w$.]*["']?)`;
|
132
|
+
code = code.replace(new RegExp(fragmentPattern, "g"), () => {
|
298
133
|
hasCompiledJsx = true;
|
299
|
-
return "React."
|
134
|
+
return "React.Fragment";
|
135
|
+
}).replace(new RegExp(createElementPattern, "g"), (original, component) => {
|
136
|
+
if (/^[a-z][\w$]*$/.test(component)) {
|
137
|
+
return original;
|
138
|
+
}
|
139
|
+
hasCompiledJsx = true;
|
140
|
+
return "React.createElement(" + (component === "Fragment" ? "React.Fragment" : component);
|
300
141
|
});
|
301
142
|
if (!hasCompiledJsx) {
|
302
143
|
return jsxNotFound;
|
303
144
|
}
|
304
|
-
|
305
|
-
babelRestoreJSX || (babelRestoreJSX = Promise.resolve().then(() => (init_babel_restore_jsx(), babel_restore_jsx_exports)));
|
306
|
-
const result = await babel2.transformAsync(code, {
|
145
|
+
const result = await babel.transformAsync(code, {
|
307
146
|
babelrc: false,
|
308
147
|
configFile: false,
|
309
148
|
ast: true,
|
@@ -312,9 +151,9 @@ async function restoreJSX(babel2, code, filename) {
|
|
312
151
|
parserOpts: {
|
313
152
|
plugins: ["jsx"]
|
314
153
|
},
|
315
|
-
plugins: [
|
154
|
+
plugins: [await getBabelRestoreJSX()]
|
316
155
|
});
|
317
|
-
return [result
|
156
|
+
return [result?.ast, isCommonJS];
|
318
157
|
}
|
319
158
|
function parseReactAlias(code) {
|
320
159
|
let match = code.match(/\b(var|let|const) +(\w+) *= *require\(["']react["']\)/);
|
@@ -328,25 +167,25 @@ function parseReactAlias(code) {
|
|
328
167
|
return [void 0, false];
|
329
168
|
}
|
330
169
|
|
331
|
-
// src/index.ts
|
332
170
|
function viteReact(opts = {}) {
|
333
171
|
var _a;
|
334
172
|
let base = "/";
|
335
|
-
let filter =
|
173
|
+
let filter = createFilter(opts.include, opts.exclude);
|
336
174
|
let isProduction = true;
|
337
175
|
let projectRoot = process.cwd();
|
338
176
|
let skipFastRefresh = opts.fastRefresh === false;
|
339
177
|
let skipReactImport = false;
|
340
178
|
const useAutomaticRuntime = opts.jsxRuntime !== "classic";
|
341
|
-
const babelOptions =
|
179
|
+
const babelOptions = {
|
342
180
|
babelrc: false,
|
343
|
-
configFile: false
|
344
|
-
|
181
|
+
configFile: false,
|
182
|
+
...opts.babel
|
183
|
+
};
|
345
184
|
babelOptions.plugins || (babelOptions.plugins = []);
|
346
185
|
babelOptions.presets || (babelOptions.presets = []);
|
347
186
|
babelOptions.overrides || (babelOptions.overrides = []);
|
348
187
|
babelOptions.parserOpts || (babelOptions.parserOpts = {});
|
349
|
-
(_a = babelOptions.parserOpts).plugins || (_a.plugins =
|
188
|
+
(_a = babelOptions.parserOpts).plugins || (_a.plugins = []);
|
350
189
|
const importReactRE = /(^|\n)import\s+(\*\s+as\s+)?React(,|\s+)/;
|
351
190
|
const fileExtensionRE = /\.[^\/\s\?]+$/;
|
352
191
|
const viteBabel = {
|
@@ -355,7 +194,7 @@ function viteReact(opts = {}) {
|
|
355
194
|
configResolved(config) {
|
356
195
|
base = config.base;
|
357
196
|
projectRoot = config.root;
|
358
|
-
filter =
|
197
|
+
filter = createFilter(opts.include, opts.exclude, {
|
359
198
|
resolve: projectRoot
|
360
199
|
});
|
361
200
|
isProduction = config.isProduction;
|
@@ -366,17 +205,16 @@ function viteReact(opts = {}) {
|
|
366
205
|
config.logger.warn("[@vitejs/plugin-react] This plugin imports React for you automatically, so you can stop using `esbuild.jsxInject` for that purpose.");
|
367
206
|
}
|
368
207
|
config.plugins.forEach((plugin) => {
|
369
|
-
var _a2;
|
370
208
|
const hasConflict = plugin.name === "react-refresh" || plugin !== viteReactJsx && plugin.name === "vite:react-jsx";
|
371
209
|
if (hasConflict)
|
372
210
|
return config.logger.warn(`[@vitejs/plugin-react] You should stop using "${plugin.name}" since this plugin conflicts with it.`);
|
373
|
-
if (
|
211
|
+
if (plugin.api?.reactBabel) {
|
374
212
|
plugin.api.reactBabel(babelOptions, config);
|
375
213
|
}
|
376
214
|
});
|
377
215
|
},
|
378
216
|
async transform(code, id, options) {
|
379
|
-
const ssr = typeof options === "boolean" ? options :
|
217
|
+
const ssr = typeof options === "boolean" ? options : options?.ssr === true;
|
380
218
|
const [filepath, querystring = ""] = id.split("?");
|
381
219
|
const [extension = ""] = querystring.match(fileExtensionRE) || filepath.match(fileExtensionRE) || [];
|
382
220
|
if (/\.(mjs|[tj]sx?)$/.test(extension)) {
|
@@ -439,25 +277,28 @@ function viteReact(opts = {}) {
|
|
439
277
|
if (/\.tsx?$/.test(extension)) {
|
440
278
|
parserPlugins.push("typescript");
|
441
279
|
}
|
442
|
-
const
|
280
|
+
const transformAsync = ast ? babel.transformFromAstAsync.bind(babel, ast, code) : babel.transformAsync.bind(babel, code);
|
443
281
|
const isReasonReact = extension.endsWith(".bs.js");
|
444
|
-
const result = await
|
282
|
+
const result = await transformAsync({
|
283
|
+
...babelOptions,
|
445
284
|
ast: !isReasonReact,
|
446
285
|
root: projectRoot,
|
447
286
|
filename: id,
|
448
287
|
sourceFileName: filepath,
|
449
|
-
parserOpts:
|
288
|
+
parserOpts: {
|
289
|
+
...babelOptions.parserOpts,
|
450
290
|
sourceType: "module",
|
451
291
|
allowAwaitOutsideFunction: true,
|
452
292
|
plugins: parserPlugins
|
453
|
-
}
|
454
|
-
generatorOpts:
|
293
|
+
},
|
294
|
+
generatorOpts: {
|
295
|
+
...babelOptions.generatorOpts,
|
455
296
|
decoratorsBeforeExport: true
|
456
|
-
}
|
297
|
+
},
|
457
298
|
plugins,
|
458
299
|
sourceMaps: true,
|
459
300
|
inputSourceMap: false
|
460
|
-
})
|
301
|
+
});
|
461
302
|
if (result) {
|
462
303
|
let code2 = result.code;
|
463
304
|
if (useFastRefresh && /\$RefreshReg\$\(/.test(code2)) {
|
@@ -517,7 +358,7 @@ function viteReact(opts = {}) {
|
|
517
358
|
},
|
518
359
|
load(id) {
|
519
360
|
if (id === runtimeId) {
|
520
|
-
const runtimePath =
|
361
|
+
const runtimePath = resolve.sync(runtimeId, {
|
521
362
|
basedir: projectRoot
|
522
363
|
});
|
523
364
|
const exports = ["jsx", "jsxs", "Fragment"];
|
@@ -531,12 +372,8 @@ function viteReact(opts = {}) {
|
|
531
372
|
return [viteBabel, viteReactRefresh, useAutomaticRuntime && viteReactJsx];
|
532
373
|
}
|
533
374
|
viteReact.preambleCode = preambleCode;
|
534
|
-
function loadPlugin(
|
535
|
-
return
|
375
|
+
function loadPlugin(path) {
|
376
|
+
return import(path).then((module) => module.default || module);
|
536
377
|
}
|
537
|
-
|
538
|
-
|
539
|
-
/**
|
540
|
-
* https://github.com/flying-sheep/babel-plugin-transform-react-createelement-to-jsx
|
541
|
-
* @license GNU General Public License v3.0
|
542
|
-
*/
|
378
|
+
|
379
|
+
export { viteReact as default };
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@vitejs/plugin-react",
|
3
|
-
"version": "
|
3
|
+
"version": "2.0.0-alpha.1",
|
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.
|
14
|
-
"
|
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": "
|
17
|
-
"build": "
|
18
|
-
"
|
19
|
-
"
|
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": ">=
|
30
|
+
"node": ">=14.6.0"
|
25
31
|
},
|
26
32
|
"repository": {
|
27
33
|
"type": "git",
|
@@ -33,13 +39,19 @@
|
|
33
39
|
},
|
34
40
|
"homepage": "https://github.com/vitejs/vite/tree/main/packages/plugin-react#readme",
|
35
41
|
"dependencies": {
|
36
|
-
"@babel/core": "^7.17.
|
42
|
+
"@babel/core": "^7.17.10",
|
37
43
|
"@babel/plugin-transform-react-jsx": "^7.17.3",
|
38
44
|
"@babel/plugin-transform-react-jsx-development": "^7.16.7",
|
39
45
|
"@babel/plugin-transform-react-jsx-self": "^7.16.7",
|
40
46
|
"@babel/plugin-transform-react-jsx-source": "^7.16.7",
|
41
|
-
"@rollup/pluginutils": "^4.2.
|
42
|
-
"react-refresh": "^0.
|
47
|
+
"@rollup/pluginutils": "^4.2.1",
|
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
|
}
|
package/src/fast-refresh.ts
CHANGED
@@ -1,11 +1,13 @@
|
|
1
|
-
import type { types as t } from '@babel/core'
|
2
1
|
import fs from 'fs'
|
3
2
|
import path from 'path'
|
3
|
+
import { createRequire } from 'module'
|
4
|
+
import type { types as t } from '@babel/core'
|
4
5
|
|
5
6
|
export const runtimePublicPath = '/@react-refresh'
|
6
7
|
|
8
|
+
const _require = createRequire(import.meta.url)
|
7
9
|
const reactRefreshDir = path.dirname(
|
8
|
-
|
10
|
+
_require.resolve('react-refresh/package.json')
|
9
11
|
)
|
10
12
|
const runtimeFilePath = path.join(
|
11
13
|
reactRefreshDir,
|