@vitejs/plugin-react 1.3.2 → 2.0.0-alpha.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/dist/chunks/babel-restore-jsx.cjs +125 -0
- package/dist/chunks/babel-restore-jsx.mjs +123 -0
- package/dist/{index.js → index.cjs} +108 -248
- package/dist/index.d.ts +69 -62
- package/dist/index.mjs +387 -0
- package/package.json +25 -13
- package/src/fast-refresh.ts +4 -2
- package/src/index.ts +77 -36
- 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 +3 -2
- package/src/jsx-runtime/restore-jsx.ts +18 -13
@@ -1,191 +1,37 @@
|
|
1
|
-
|
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
|
+
'use strict';
|
41
2
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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);
|
3
|
+
const path = require('path');
|
4
|
+
const babel = require('@babel/core');
|
5
|
+
const pluginutils = require('@rollup/pluginutils');
|
6
|
+
const vite = require('vite');
|
7
|
+
const fs = require('fs');
|
8
|
+
const module$1 = require('module');
|
9
|
+
|
10
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
|
11
|
+
|
12
|
+
function _interopNamespace(e) {
|
13
|
+
if (e && e.__esModule) return e;
|
14
|
+
const n = Object.create(null);
|
15
|
+
if (e) {
|
16
|
+
for (const k in e) {
|
17
|
+
n[k] = e[k];
|
146
18
|
}
|
147
|
-
return null;
|
148
19
|
}
|
149
|
-
|
150
|
-
|
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
|
-
};
|
20
|
+
n["default"] = e;
|
21
|
+
return n;
|
163
22
|
}
|
164
|
-
var init_babel_restore_jsx = __esm({
|
165
|
-
"src/jsx-runtime/babel-restore-jsx.ts"() {
|
166
|
-
}
|
167
|
-
});
|
168
23
|
|
169
|
-
|
170
|
-
|
171
|
-
|
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"));
|
24
|
+
const path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
25
|
+
const babel__namespace = /*#__PURE__*/_interopNamespace(babel);
|
26
|
+
const fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
179
27
|
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
var runtimeFilePath = import_path.default.join(reactRefreshDir, "cjs/react-refresh-runtime.development.js");
|
186
|
-
var runtimeCode = `
|
28
|
+
const runtimePublicPath = "/@react-refresh";
|
29
|
+
const _require = module$1.createRequire((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)));
|
30
|
+
const reactRefreshDir = path__default.dirname(_require.resolve("react-refresh/package.json"));
|
31
|
+
const runtimeFilePath = path__default.join(reactRefreshDir, "cjs/react-refresh-runtime.development.js");
|
32
|
+
const runtimeCode = `
|
187
33
|
const exports = {}
|
188
|
-
${
|
34
|
+
${fs__default.readFileSync(runtimeFilePath, "utf-8")}
|
189
35
|
function debounce(fn, delay) {
|
190
36
|
let handle
|
191
37
|
return () => {
|
@@ -196,14 +42,14 @@ function debounce(fn, delay) {
|
|
196
42
|
exports.performReactRefresh = debounce(exports.performReactRefresh, 16)
|
197
43
|
export default exports
|
198
44
|
`;
|
199
|
-
|
45
|
+
const preambleCode = `
|
200
46
|
import RefreshRuntime from "__BASE__${runtimePublicPath.slice(1)}"
|
201
47
|
RefreshRuntime.injectIntoGlobalHook(window)
|
202
48
|
window.$RefreshReg$ = () => {}
|
203
49
|
window.$RefreshSig$ = () => (type) => type
|
204
50
|
window.__vite_plugin_react_preamble_installed__ = true
|
205
51
|
`;
|
206
|
-
|
52
|
+
const header = `
|
207
53
|
import RefreshRuntime from "${runtimePublicPath}";
|
208
54
|
|
209
55
|
let prevRefreshReg;
|
@@ -224,7 +70,7 @@ if (import.meta.hot) {
|
|
224
70
|
};
|
225
71
|
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
|
226
72
|
}`.replace(/[\n]+/gm, "");
|
227
|
-
|
73
|
+
const footer = `
|
228
74
|
if (import.meta.hot) {
|
229
75
|
window.$RefreshReg$ = prevRefreshReg;
|
230
76
|
window.$RefreshSig$ = prevRefreshSig;
|
@@ -266,14 +112,13 @@ function isComponentLikeName(name) {
|
|
266
112
|
return typeof name === "string" && name[0] >= "A" && name[0] <= "Z";
|
267
113
|
}
|
268
114
|
|
269
|
-
// src/jsx-runtime/babel-import-to-require.ts
|
270
115
|
function babelImportToRequire({ types: t }) {
|
271
116
|
return {
|
272
117
|
visitor: {
|
273
|
-
ImportDeclaration(
|
274
|
-
const decl =
|
118
|
+
ImportDeclaration(path) {
|
119
|
+
const decl = path.node;
|
275
120
|
const spec = decl.specifiers[0];
|
276
|
-
|
121
|
+
path.replaceWith(t.variableDeclaration("var", [
|
277
122
|
t.variableDeclarator(spec.local, t.memberExpression(t.callExpression(t.identifier("require"), [decl.source]), spec.imported))
|
278
123
|
]));
|
279
124
|
}
|
@@ -281,13 +126,19 @@ function babelImportToRequire({ types: t }) {
|
|
281
126
|
};
|
282
127
|
}
|
283
128
|
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
129
|
+
let babelRestoreJSX;
|
130
|
+
const jsxNotFound = [null, false];
|
131
|
+
async function getBabelRestoreJSX() {
|
132
|
+
if (!babelRestoreJSX)
|
133
|
+
babelRestoreJSX = import('./chunks/babel-restore-jsx.cjs').then((r) => {
|
134
|
+
const fn = r.default;
|
135
|
+
if ("default" in fn)
|
136
|
+
return fn.default;
|
137
|
+
return fn;
|
138
|
+
});
|
139
|
+
return babelRestoreJSX;
|
140
|
+
}
|
141
|
+
async function restoreJSX(babel, code, filename) {
|
291
142
|
const [reactAlias, isCommonJS] = parseReactAlias(code);
|
292
143
|
if (!reactAlias) {
|
293
144
|
return jsxNotFound;
|
@@ -308,8 +159,7 @@ async function restoreJSX(babel2, code, filename) {
|
|
308
159
|
if (!hasCompiledJsx) {
|
309
160
|
return jsxNotFound;
|
310
161
|
}
|
311
|
-
|
312
|
-
const result = await babel2.transformAsync(code, {
|
162
|
+
const result = await babel.transformAsync(code, {
|
313
163
|
babelrc: false,
|
314
164
|
configFile: false,
|
315
165
|
ast: true,
|
@@ -318,9 +168,9 @@ async function restoreJSX(babel2, code, filename) {
|
|
318
168
|
parserOpts: {
|
319
169
|
plugins: ["jsx"]
|
320
170
|
},
|
321
|
-
plugins: [
|
171
|
+
plugins: [await getBabelRestoreJSX()]
|
322
172
|
});
|
323
|
-
return [result
|
173
|
+
return [result?.ast, isCommonJS];
|
324
174
|
}
|
325
175
|
function parseReactAlias(code) {
|
326
176
|
let match = code.match(/\b(var|let|const) +(\w+) *= *require\(["']react["']\)/);
|
@@ -334,25 +184,17 @@ function parseReactAlias(code) {
|
|
334
184
|
return [void 0, false];
|
335
185
|
}
|
336
186
|
|
337
|
-
// src/index.ts
|
338
187
|
function viteReact(opts = {}) {
|
339
|
-
var _a;
|
340
188
|
let base = "/";
|
341
|
-
let
|
189
|
+
let resolvedCacheDir;
|
190
|
+
let filter = pluginutils.createFilter(opts.include, opts.exclude);
|
342
191
|
let isProduction = true;
|
343
192
|
let projectRoot = process.cwd();
|
344
193
|
let skipFastRefresh = opts.fastRefresh === false;
|
345
194
|
let skipReactImport = false;
|
195
|
+
let runPluginOverrides = (options, context) => false;
|
196
|
+
let staticBabelOptions;
|
346
197
|
const useAutomaticRuntime = opts.jsxRuntime !== "classic";
|
347
|
-
const babelOptions = __spreadValues({
|
348
|
-
babelrc: false,
|
349
|
-
configFile: false
|
350
|
-
}, opts.babel);
|
351
|
-
babelOptions.plugins || (babelOptions.plugins = []);
|
352
|
-
babelOptions.presets || (babelOptions.presets = []);
|
353
|
-
babelOptions.overrides || (babelOptions.overrides = []);
|
354
|
-
babelOptions.parserOpts || (babelOptions.parserOpts = {});
|
355
|
-
(_a = babelOptions.parserOpts).plugins || (_a.plugins = opts.parserPlugins || []);
|
356
198
|
const importReactRE = /(^|\n)import\s+(\*\s+as\s+)?React(,|\s+)/;
|
357
199
|
const fileExtensionRE = /\.[^\/\s\?]+$/;
|
358
200
|
const viteBabel = {
|
@@ -361,7 +203,8 @@ function viteReact(opts = {}) {
|
|
361
203
|
configResolved(config) {
|
362
204
|
base = config.base;
|
363
205
|
projectRoot = config.root;
|
364
|
-
|
206
|
+
resolvedCacheDir = vite.normalizePath(path__default.resolve(config.cacheDir));
|
207
|
+
filter = pluginutils.createFilter(opts.include, opts.exclude, {
|
365
208
|
resolve: projectRoot
|
366
209
|
});
|
367
210
|
isProduction = config.isProduction;
|
@@ -372,23 +215,41 @@ function viteReact(opts = {}) {
|
|
372
215
|
config.logger.warn("[@vitejs/plugin-react] This plugin imports React for you automatically, so you can stop using `esbuild.jsxInject` for that purpose.");
|
373
216
|
}
|
374
217
|
config.plugins.forEach((plugin) => {
|
375
|
-
var _a2;
|
376
218
|
const hasConflict = plugin.name === "react-refresh" || plugin !== viteReactJsx && plugin.name === "vite:react-jsx";
|
377
219
|
if (hasConflict)
|
378
220
|
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) {
|
380
|
-
plugin.api.reactBabel(babelOptions, config);
|
381
|
-
}
|
382
221
|
});
|
222
|
+
runPluginOverrides = (babelOptions, context) => {
|
223
|
+
const hooks = config.plugins.map((plugin) => plugin.api?.reactBabel).filter(Boolean);
|
224
|
+
if (hooks.length > 0) {
|
225
|
+
return (runPluginOverrides = (babelOptions2) => {
|
226
|
+
hooks.forEach((hook) => hook(babelOptions2, context, config));
|
227
|
+
return true;
|
228
|
+
})(babelOptions);
|
229
|
+
}
|
230
|
+
runPluginOverrides = () => false;
|
231
|
+
return false;
|
232
|
+
};
|
383
233
|
},
|
384
234
|
async transform(code, id, options) {
|
385
|
-
const ssr = typeof options === "boolean" ? options :
|
235
|
+
const ssr = typeof options === "boolean" ? options : options?.ssr === true;
|
386
236
|
const [filepath, querystring = ""] = id.split("?");
|
387
237
|
const [extension = ""] = querystring.match(fileExtensionRE) || filepath.match(fileExtensionRE) || [];
|
388
238
|
if (/\.(mjs|[tj]sx?)$/.test(extension)) {
|
389
239
|
const isJSX = extension.endsWith("x");
|
390
240
|
const isNodeModules = id.includes("/node_modules/");
|
391
241
|
const isProjectFile = !isNodeModules && (id[0] === "\0" || id.startsWith(projectRoot + "/"));
|
242
|
+
let babelOptions = staticBabelOptions;
|
243
|
+
if (typeof opts.babel === "function") {
|
244
|
+
const rawOptions = opts.babel(id, { ssr });
|
245
|
+
babelOptions = createBabelOptions(rawOptions);
|
246
|
+
runPluginOverrides(babelOptions, { ssr, id });
|
247
|
+
} else if (!babelOptions) {
|
248
|
+
babelOptions = createBabelOptions(opts.babel);
|
249
|
+
if (!runPluginOverrides(babelOptions, { ssr, id })) {
|
250
|
+
staticBabelOptions = babelOptions;
|
251
|
+
}
|
252
|
+
}
|
392
253
|
const plugins = isProjectFile ? [...babelOptions.plugins] : [];
|
393
254
|
let useFastRefresh = false;
|
394
255
|
if (!skipFastRefresh && !ssr && !isNodeModules) {
|
@@ -404,7 +265,8 @@ function viteReact(opts = {}) {
|
|
404
265
|
let ast;
|
405
266
|
if (!isProjectFile || isJSX) {
|
406
267
|
if (useAutomaticRuntime) {
|
407
|
-
const
|
268
|
+
const isOptimizedReactDom = id.startsWith(resolvedCacheDir) && id.includes("/react-dom.js");
|
269
|
+
const [restoredAst, isCommonJS] = !isProjectFile && !isJSX && !isOptimizedReactDom ? await restoreJSX(babel__namespace, code, id) : [null, false];
|
408
270
|
if (isJSX || (ast = restoredAst)) {
|
409
271
|
plugins.push([
|
410
272
|
await loadPlugin("@babel/plugin-transform-react-jsx" + (isProduction ? "" : "-development")),
|
@@ -445,25 +307,28 @@ function viteReact(opts = {}) {
|
|
445
307
|
if (/\.tsx?$/.test(extension)) {
|
446
308
|
parserPlugins.push("typescript");
|
447
309
|
}
|
448
|
-
const
|
310
|
+
const transformAsync = ast ? babel__namespace.transformFromAstAsync.bind(babel__namespace, ast, code) : babel__namespace.transformAsync.bind(babel__namespace, code);
|
449
311
|
const isReasonReact = extension.endsWith(".bs.js");
|
450
|
-
const result = await
|
312
|
+
const result = await transformAsync({
|
313
|
+
...babelOptions,
|
451
314
|
ast: !isReasonReact,
|
452
315
|
root: projectRoot,
|
453
316
|
filename: id,
|
454
317
|
sourceFileName: filepath,
|
455
|
-
parserOpts:
|
318
|
+
parserOpts: {
|
319
|
+
...babelOptions.parserOpts,
|
456
320
|
sourceType: "module",
|
457
321
|
allowAwaitOutsideFunction: true,
|
458
322
|
plugins: parserPlugins
|
459
|
-
}
|
460
|
-
generatorOpts:
|
323
|
+
},
|
324
|
+
generatorOpts: {
|
325
|
+
...babelOptions.generatorOpts,
|
461
326
|
decoratorsBeforeExport: true
|
462
|
-
}
|
327
|
+
},
|
463
328
|
plugins,
|
464
329
|
sourceMaps: true,
|
465
330
|
inputSourceMap: false
|
466
|
-
})
|
331
|
+
});
|
467
332
|
if (result) {
|
468
333
|
let code2 = result.code;
|
469
334
|
if (useFastRefresh && /\$RefreshReg\$\(/.test(code2)) {
|
@@ -507,7 +372,6 @@ function viteReact(opts = {}) {
|
|
507
372
|
];
|
508
373
|
}
|
509
374
|
};
|
510
|
-
const runtimeId = "react/jsx-runtime";
|
511
375
|
const viteReactJsx = {
|
512
376
|
name: "vite:react-jsx",
|
513
377
|
enforce: "pre",
|
@@ -517,32 +381,28 @@ function viteReact(opts = {}) {
|
|
517
381
|
include: ["react/jsx-dev-runtime"]
|
518
382
|
}
|
519
383
|
};
|
520
|
-
},
|
521
|
-
resolveId(id) {
|
522
|
-
return id === runtimeId ? id : null;
|
523
|
-
},
|
524
|
-
load(id) {
|
525
|
-
if (id === runtimeId) {
|
526
|
-
const runtimePath = import_resolve.default.sync(runtimeId, {
|
527
|
-
basedir: projectRoot
|
528
|
-
});
|
529
|
-
const exports = ["jsx", "jsxs", "Fragment"];
|
530
|
-
return [
|
531
|
-
`import * as jsxRuntime from ${JSON.stringify(runtimePath)}`,
|
532
|
-
...exports.map((name) => `export const ${name} = jsxRuntime.${name}`)
|
533
|
-
].join("\n");
|
534
|
-
}
|
535
384
|
}
|
536
385
|
};
|
537
386
|
return [viteBabel, viteReactRefresh, useAutomaticRuntime && viteReactJsx];
|
538
387
|
}
|
539
388
|
viteReact.preambleCode = preambleCode;
|
540
389
|
function loadPlugin(path2) {
|
541
|
-
return
|
390
|
+
return import(path2).then((module) => module.default || module);
|
391
|
+
}
|
392
|
+
function createBabelOptions(rawOptions) {
|
393
|
+
var _a;
|
394
|
+
const babelOptions = {
|
395
|
+
babelrc: false,
|
396
|
+
configFile: false,
|
397
|
+
...rawOptions
|
398
|
+
};
|
399
|
+
babelOptions.plugins || (babelOptions.plugins = []);
|
400
|
+
babelOptions.presets || (babelOptions.presets = []);
|
401
|
+
babelOptions.overrides || (babelOptions.overrides = []);
|
402
|
+
babelOptions.parserOpts || (babelOptions.parserOpts = {});
|
403
|
+
(_a = babelOptions.parserOpts).plugins || (_a.plugins = []);
|
404
|
+
return babelOptions;
|
542
405
|
}
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
* https://github.com/flying-sheep/babel-plugin-transform-react-createelement-to-jsx
|
547
|
-
* @license GNU General Public License v3.0
|
548
|
-
*/
|
406
|
+
|
407
|
+
module.exports = viteReact;
|
408
|
+
module.exports["default"] = viteReact;
|
package/dist/index.d.ts
CHANGED
@@ -1,62 +1,69 @@
|
|
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
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
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 | ((id: string, options: {
|
33
|
+
ssr?: boolean;
|
34
|
+
}) => BabelOptions);
|
35
|
+
}
|
36
|
+
declare type BabelOptions = Omit<TransformOptions, 'ast' | 'filename' | 'root' | 'sourceFileName' | 'sourceMaps' | 'inputSourceMap'>;
|
37
|
+
/**
|
38
|
+
* The object type used by the `options` passed to plugins with
|
39
|
+
* an `api.reactBabel` method.
|
40
|
+
*/
|
41
|
+
interface ReactBabelOptions extends BabelOptions {
|
42
|
+
plugins: Extract<BabelOptions['plugins'], any[]>;
|
43
|
+
presets: Extract<BabelOptions['presets'], any[]>;
|
44
|
+
overrides: Extract<BabelOptions['overrides'], any[]>;
|
45
|
+
parserOpts: ParserOptions & {
|
46
|
+
plugins: Extract<ParserOptions['plugins'], any[]>;
|
47
|
+
};
|
48
|
+
}
|
49
|
+
declare type ReactBabelHook = (babelConfig: ReactBabelOptions, context: ReactBabelHookContext, config: ResolvedConfig) => void;
|
50
|
+
declare type ReactBabelHookContext = {
|
51
|
+
ssr: boolean;
|
52
|
+
id: string;
|
53
|
+
};
|
54
|
+
declare module 'vite' {
|
55
|
+
interface Plugin {
|
56
|
+
api?: {
|
57
|
+
/**
|
58
|
+
* Manipulate the Babel options of `@vitejs/plugin-react`
|
59
|
+
*/
|
60
|
+
reactBabel?: ReactBabelHook;
|
61
|
+
};
|
62
|
+
}
|
63
|
+
}
|
64
|
+
declare function viteReact(opts?: Options): PluginOption[];
|
65
|
+
declare namespace viteReact {
|
66
|
+
var preambleCode: string;
|
67
|
+
}
|
68
|
+
|
69
|
+
export { BabelOptions, Options, ReactBabelOptions, viteReact as default };
|