@vitejs/plugin-react 1.3.0 → 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/README.md +1 -1
- package/dist/chunks/babel-restore-jsx.cjs +125 -0
- package/dist/chunks/babel-restore-jsx.mjs +123 -0
- package/dist/index.cjs +399 -0
- package/dist/index.d.ts +62 -62
- package/dist/{index.js → index.mjs} +63 -226
- package/package.json +25 -13
- package/src/fast-refresh.ts +8 -3
- package/src/index.ts +4 -16
- package/src/jsx-runtime/babel-import-to-require.ts +1 -1
- 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 +39 -20
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,189 +1,15 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
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
|
-
|
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(path) {
|
155
|
-
const node = getJSXNode(path.node);
|
156
|
-
if (node == null) {
|
157
|
-
return null;
|
158
|
-
}
|
159
|
-
path.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 runtimePublicPath = "/@react-refresh";
|
183
|
-
var runtimeFilePath = require.resolve("react-refresh/cjs/react-refresh-runtime.development.js");
|
184
|
-
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 = `
|
185
11
|
const exports = {}
|
186
|
-
${
|
12
|
+
${fs.readFileSync(runtimeFilePath, "utf-8")}
|
187
13
|
function debounce(fn, delay) {
|
188
14
|
let handle
|
189
15
|
return () => {
|
@@ -194,14 +20,14 @@ function debounce(fn, delay) {
|
|
194
20
|
exports.performReactRefresh = debounce(exports.performReactRefresh, 16)
|
195
21
|
export default exports
|
196
22
|
`;
|
197
|
-
|
23
|
+
const preambleCode = `
|
198
24
|
import RefreshRuntime from "__BASE__${runtimePublicPath.slice(1)}"
|
199
25
|
RefreshRuntime.injectIntoGlobalHook(window)
|
200
26
|
window.$RefreshReg$ = () => {}
|
201
27
|
window.$RefreshSig$ = () => (type) => type
|
202
28
|
window.__vite_plugin_react_preamble_installed__ = true
|
203
29
|
`;
|
204
|
-
|
30
|
+
const header = `
|
205
31
|
import RefreshRuntime from "${runtimePublicPath}";
|
206
32
|
|
207
33
|
let prevRefreshReg;
|
@@ -222,7 +48,7 @@ if (import.meta.hot) {
|
|
222
48
|
};
|
223
49
|
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
|
224
50
|
}`.replace(/[\n]+/gm, "");
|
225
|
-
|
51
|
+
const footer = `
|
226
52
|
if (import.meta.hot) {
|
227
53
|
window.$RefreshReg$ = prevRefreshReg;
|
228
54
|
window.$RefreshSig$ = prevRefreshSig;
|
@@ -264,7 +90,6 @@ function isComponentLikeName(name) {
|
|
264
90
|
return typeof name === "string" && name[0] >= "A" && name[0] <= "Z";
|
265
91
|
}
|
266
92
|
|
267
|
-
// src/jsx-runtime/babel-import-to-require.ts
|
268
93
|
function babelImportToRequire({ types: t }) {
|
269
94
|
return {
|
270
95
|
visitor: {
|
@@ -279,10 +104,19 @@ function babelImportToRequire({ types: t }) {
|
|
279
104
|
};
|
280
105
|
}
|
281
106
|
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
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) {
|
286
120
|
if (filename.includes("/.vite/react-dom.js")) {
|
287
121
|
return jsxNotFound;
|
288
122
|
}
|
@@ -290,18 +124,23 @@ async function restoreJSX(babel2, code, filename) {
|
|
290
124
|
if (!reactAlias) {
|
291
125
|
return jsxNotFound;
|
292
126
|
}
|
293
|
-
const reactJsxRE = new RegExp("\\b" + reactAlias + "\\.(createElement|Fragment)\\b", "g");
|
294
127
|
let hasCompiledJsx = false;
|
295
|
-
|
128
|
+
const fragmentPattern = `\\b${reactAlias}\\.Fragment\\b`;
|
129
|
+
const createElementPattern = `\\b${reactAlias}\\.createElement\\(\\s*([A-Z"'][\\w$.]*["']?)`;
|
130
|
+
code = code.replace(new RegExp(fragmentPattern, "g"), () => {
|
296
131
|
hasCompiledJsx = true;
|
297
|
-
return "React."
|
132
|
+
return "React.Fragment";
|
133
|
+
}).replace(new RegExp(createElementPattern, "g"), (original, component) => {
|
134
|
+
if (/^[a-z][\w$]*$/.test(component)) {
|
135
|
+
return original;
|
136
|
+
}
|
137
|
+
hasCompiledJsx = true;
|
138
|
+
return "React.createElement(" + (component === "Fragment" ? "React.Fragment" : component);
|
298
139
|
});
|
299
140
|
if (!hasCompiledJsx) {
|
300
141
|
return jsxNotFound;
|
301
142
|
}
|
302
|
-
|
303
|
-
babelRestoreJSX || (babelRestoreJSX = Promise.resolve().then(() => (init_babel_restore_jsx(), babel_restore_jsx_exports)));
|
304
|
-
const result = await babel2.transformAsync(code, {
|
143
|
+
const result = await babel.transformAsync(code, {
|
305
144
|
babelrc: false,
|
306
145
|
configFile: false,
|
307
146
|
ast: true,
|
@@ -310,9 +149,9 @@ async function restoreJSX(babel2, code, filename) {
|
|
310
149
|
parserOpts: {
|
311
150
|
plugins: ["jsx"]
|
312
151
|
},
|
313
|
-
plugins: [
|
152
|
+
plugins: [await getBabelRestoreJSX()]
|
314
153
|
});
|
315
|
-
return [result
|
154
|
+
return [result?.ast, isCommonJS];
|
316
155
|
}
|
317
156
|
function parseReactAlias(code) {
|
318
157
|
let match = code.match(/\b(var|let|const) +(\w+) *= *require\(["']react["']\)/);
|
@@ -326,25 +165,25 @@ function parseReactAlias(code) {
|
|
326
165
|
return [void 0, false];
|
327
166
|
}
|
328
167
|
|
329
|
-
// src/index.ts
|
330
168
|
function viteReact(opts = {}) {
|
331
169
|
var _a;
|
332
170
|
let base = "/";
|
333
|
-
let filter =
|
171
|
+
let filter = createFilter(opts.include, opts.exclude);
|
334
172
|
let isProduction = true;
|
335
173
|
let projectRoot = process.cwd();
|
336
174
|
let skipFastRefresh = opts.fastRefresh === false;
|
337
175
|
let skipReactImport = false;
|
338
176
|
const useAutomaticRuntime = opts.jsxRuntime !== "classic";
|
339
|
-
const babelOptions =
|
177
|
+
const babelOptions = {
|
340
178
|
babelrc: false,
|
341
|
-
configFile: false
|
342
|
-
|
179
|
+
configFile: false,
|
180
|
+
...opts.babel
|
181
|
+
};
|
343
182
|
babelOptions.plugins || (babelOptions.plugins = []);
|
344
183
|
babelOptions.presets || (babelOptions.presets = []);
|
345
184
|
babelOptions.overrides || (babelOptions.overrides = []);
|
346
185
|
babelOptions.parserOpts || (babelOptions.parserOpts = {});
|
347
|
-
(_a = babelOptions.parserOpts).plugins || (_a.plugins =
|
186
|
+
(_a = babelOptions.parserOpts).plugins || (_a.plugins = []);
|
348
187
|
const importReactRE = /(^|\n)import\s+(\*\s+as\s+)?React(,|\s+)/;
|
349
188
|
const fileExtensionRE = /\.[^\/\s\?]+$/;
|
350
189
|
const viteBabel = {
|
@@ -353,7 +192,7 @@ function viteReact(opts = {}) {
|
|
353
192
|
configResolved(config) {
|
354
193
|
base = config.base;
|
355
194
|
projectRoot = config.root;
|
356
|
-
filter =
|
195
|
+
filter = createFilter(opts.include, opts.exclude, {
|
357
196
|
resolve: projectRoot
|
358
197
|
});
|
359
198
|
isProduction = config.isProduction;
|
@@ -364,17 +203,16 @@ function viteReact(opts = {}) {
|
|
364
203
|
config.logger.warn("[@vitejs/plugin-react] This plugin imports React for you automatically, so you can stop using `esbuild.jsxInject` for that purpose.");
|
365
204
|
}
|
366
205
|
config.plugins.forEach((plugin) => {
|
367
|
-
var _a2;
|
368
206
|
const hasConflict = plugin.name === "react-refresh" || plugin !== viteReactJsx && plugin.name === "vite:react-jsx";
|
369
207
|
if (hasConflict)
|
370
208
|
return config.logger.warn(`[@vitejs/plugin-react] You should stop using "${plugin.name}" since this plugin conflicts with it.`);
|
371
|
-
if (
|
209
|
+
if (plugin.api?.reactBabel) {
|
372
210
|
plugin.api.reactBabel(babelOptions, config);
|
373
211
|
}
|
374
212
|
});
|
375
213
|
},
|
376
214
|
async transform(code, id, options) {
|
377
|
-
const ssr = typeof options === "boolean" ? options :
|
215
|
+
const ssr = typeof options === "boolean" ? options : options?.ssr === true;
|
378
216
|
const [filepath, querystring = ""] = id.split("?");
|
379
217
|
const [extension = ""] = querystring.match(fileExtensionRE) || filepath.match(fileExtensionRE) || [];
|
380
218
|
if (/\.(mjs|[tj]sx?)$/.test(extension)) {
|
@@ -388,7 +226,7 @@ function viteReact(opts = {}) {
|
|
388
226
|
if (isReactModule && filter(id)) {
|
389
227
|
useFastRefresh = true;
|
390
228
|
plugins.push([
|
391
|
-
await loadPlugin("react-refresh/babel
|
229
|
+
await loadPlugin("react-refresh/babel"),
|
392
230
|
{ skipEnvCheck: true }
|
393
231
|
]);
|
394
232
|
}
|
@@ -437,25 +275,28 @@ function viteReact(opts = {}) {
|
|
437
275
|
if (/\.tsx?$/.test(extension)) {
|
438
276
|
parserPlugins.push("typescript");
|
439
277
|
}
|
440
|
-
const
|
278
|
+
const transformAsync = ast ? babel.transformFromAstAsync.bind(babel, ast, code) : babel.transformAsync.bind(babel, code);
|
441
279
|
const isReasonReact = extension.endsWith(".bs.js");
|
442
|
-
const result = await
|
280
|
+
const result = await transformAsync({
|
281
|
+
...babelOptions,
|
443
282
|
ast: !isReasonReact,
|
444
283
|
root: projectRoot,
|
445
284
|
filename: id,
|
446
285
|
sourceFileName: filepath,
|
447
|
-
parserOpts:
|
286
|
+
parserOpts: {
|
287
|
+
...babelOptions.parserOpts,
|
448
288
|
sourceType: "module",
|
449
289
|
allowAwaitOutsideFunction: true,
|
450
290
|
plugins: parserPlugins
|
451
|
-
}
|
452
|
-
generatorOpts:
|
291
|
+
},
|
292
|
+
generatorOpts: {
|
293
|
+
...babelOptions.generatorOpts,
|
453
294
|
decoratorsBeforeExport: true
|
454
|
-
}
|
295
|
+
},
|
455
296
|
plugins,
|
456
297
|
sourceMaps: true,
|
457
298
|
inputSourceMap: false
|
458
|
-
})
|
299
|
+
});
|
459
300
|
if (result) {
|
460
301
|
let code2 = result.code;
|
461
302
|
if (useFastRefresh && /\$RefreshReg\$\(/.test(code2)) {
|
@@ -515,7 +356,7 @@ function viteReact(opts = {}) {
|
|
515
356
|
},
|
516
357
|
load(id) {
|
517
358
|
if (id === runtimeId) {
|
518
|
-
const runtimePath =
|
359
|
+
const runtimePath = resolve.sync(runtimeId, {
|
519
360
|
basedir: projectRoot
|
520
361
|
});
|
521
362
|
const exports = ["jsx", "jsxs", "Fragment"];
|
@@ -530,11 +371,7 @@ function viteReact(opts = {}) {
|
|
530
371
|
}
|
531
372
|
viteReact.preambleCode = preambleCode;
|
532
373
|
function loadPlugin(path) {
|
533
|
-
return
|
374
|
+
return import(path).then((module) => module.default || module);
|
534
375
|
}
|
535
|
-
|
536
|
-
|
537
|
-
/**
|
538
|
-
* https://github.com/flying-sheep/babel-plugin-transform-react-createelement-to-jsx
|
539
|
-
* @license GNU General Public License v3.0
|
540
|
-
*/
|
376
|
+
|
377
|
+
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.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.
|
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,10 +1,15 @@
|
|
1
|
-
import type { types as t } from '@babel/core'
|
2
1
|
import fs from 'fs'
|
2
|
+
import path from 'path'
|
3
|
+
import type { types as t } from '@babel/core'
|
3
4
|
|
4
5
|
export const runtimePublicPath = '/@react-refresh'
|
5
6
|
|
6
|
-
const
|
7
|
-
'react-refresh/
|
7
|
+
const reactRefreshDir = path.dirname(
|
8
|
+
require.resolve('react-refresh/package.json')
|
9
|
+
)
|
10
|
+
const runtimeFilePath = path.join(
|
11
|
+
reactRefreshDir,
|
12
|
+
'cjs/react-refresh-runtime.development.js'
|
8
13
|
)
|
9
14
|
|
10
15
|
export const runtimeCode = `
|