first commit
This commit is contained in:
373
node_modules/@vitejs/plugin-react/dist/index.cjs
generated
vendored
Normal file
373
node_modules/@vitejs/plugin-react/dist/index.cjs
generated
vendored
Normal file
@@ -0,0 +1,373 @@
|
||||
'use strict';
|
||||
|
||||
const path = require('node:path');
|
||||
const babel = require('@babel/core');
|
||||
const vite = require('vite');
|
||||
const MagicString = require('magic-string');
|
||||
const fs = require('node:fs');
|
||||
const node_module = require('node:module');
|
||||
|
||||
function _interopNamespaceDefault(e) {
|
||||
const n = Object.create(null);
|
||||
if (e) {
|
||||
for (const k in e) {
|
||||
n[k] = e[k];
|
||||
}
|
||||
}
|
||||
n.default = e;
|
||||
return n;
|
||||
}
|
||||
|
||||
const babel__namespace = /*#__PURE__*/_interopNamespaceDefault(babel);
|
||||
|
||||
const runtimePublicPath = "/@react-refresh";
|
||||
const _require = node_module.createRequire((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)));
|
||||
const reactRefreshDir = path.dirname(
|
||||
_require.resolve("react-refresh/package.json")
|
||||
);
|
||||
const runtimeFilePath = path.join(
|
||||
reactRefreshDir,
|
||||
"cjs/react-refresh-runtime.development.js"
|
||||
);
|
||||
const runtimeCode = `
|
||||
const exports = {}
|
||||
${fs.readFileSync(runtimeFilePath, "utf-8")}
|
||||
${fs.readFileSync(_require.resolve("./refreshUtils.js"), "utf-8")}
|
||||
export default exports
|
||||
`;
|
||||
const preambleCode = `
|
||||
import RefreshRuntime from "__BASE__${runtimePublicPath.slice(1)}"
|
||||
RefreshRuntime.injectIntoGlobalHook(window)
|
||||
window.$RefreshReg$ = () => {}
|
||||
window.$RefreshSig$ = () => (type) => type
|
||||
window.__vite_plugin_react_preamble_installed__ = true
|
||||
`;
|
||||
const header = `
|
||||
import RefreshRuntime from "${runtimePublicPath}";
|
||||
|
||||
let prevRefreshReg;
|
||||
let prevRefreshSig;
|
||||
|
||||
if (import.meta.hot) {
|
||||
if (!window.__vite_plugin_react_preamble_installed__) {
|
||||
throw new Error(
|
||||
"@vitejs/plugin-react can't detect preamble. Something is wrong. " +
|
||||
"See https://github.com/vitejs/vite-plugin-react/pull/11#discussion_r430879201"
|
||||
);
|
||||
}
|
||||
|
||||
prevRefreshReg = window.$RefreshReg$;
|
||||
prevRefreshSig = window.$RefreshSig$;
|
||||
window.$RefreshReg$ = (type, id) => {
|
||||
RefreshRuntime.register(type, __SOURCE__ + " " + id)
|
||||
};
|
||||
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
|
||||
}`.replace(/\n+/g, "");
|
||||
const footer = `
|
||||
if (import.meta.hot) {
|
||||
window.$RefreshReg$ = prevRefreshReg;
|
||||
window.$RefreshSig$ = prevRefreshSig;
|
||||
|
||||
import(/* @vite-ignore */ import.meta.url).then((currentExports) => {
|
||||
RefreshRuntime.registerExportsForReactRefresh(__SOURCE__, currentExports);
|
||||
import.meta.hot.accept((nextExports) => {
|
||||
if (!nextExports) return;
|
||||
const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(currentExports, nextExports);
|
||||
if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
|
||||
});
|
||||
});
|
||||
}`;
|
||||
function addRefreshWrapper(code, id) {
|
||||
return header.replace("__SOURCE__", JSON.stringify(id)) + code + footer.replace("__SOURCE__", JSON.stringify(id));
|
||||
}
|
||||
|
||||
const prependReactImportCode = "import React from 'react'; ";
|
||||
const refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
|
||||
function viteReact(opts = {}) {
|
||||
let devBase = "/";
|
||||
let filter = vite.createFilter(opts.include, opts.exclude);
|
||||
let needHiresSourcemap = false;
|
||||
let isProduction = true;
|
||||
let projectRoot = process.cwd();
|
||||
let skipFastRefresh = opts.fastRefresh === false;
|
||||
let skipReactImport = false;
|
||||
let runPluginOverrides = (options, context) => false;
|
||||
let staticBabelOptions;
|
||||
const useAutomaticRuntime = opts.jsxRuntime !== "classic";
|
||||
const importReactRE = /(?:^|\n)import\s+(?:\*\s+as\s+)?React(?:,|\s+)/;
|
||||
const fileExtensionRE = /\.[^/\s?]+$/;
|
||||
const viteBabel = {
|
||||
name: "vite:react-babel",
|
||||
enforce: "pre",
|
||||
config(userConfig, { mode }) {
|
||||
const resolvedRoot = vite.normalizePath(
|
||||
userConfig.root ? path.resolve(userConfig.root) : process.cwd()
|
||||
);
|
||||
const envDir = userConfig.envDir ? vite.normalizePath(path.resolve(resolvedRoot, userConfig.envDir)) : resolvedRoot;
|
||||
vite.loadEnv(mode, envDir, vite.resolveEnvPrefix(userConfig));
|
||||
const isProduction2 = (process.env.NODE_ENV || process.env.VITE_USER_NODE_ENV || mode) === "production";
|
||||
if (opts.jsxRuntime === "classic") {
|
||||
return {
|
||||
esbuild: {
|
||||
logOverride: {
|
||||
"this-is-undefined-in-esm": "silent"
|
||||
},
|
||||
jsx: "transform",
|
||||
jsxImportSource: opts.jsxImportSource,
|
||||
jsxSideEffects: opts.jsxPure === false
|
||||
}
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
esbuild: {
|
||||
jsxDev: !isProduction2,
|
||||
jsx: "automatic",
|
||||
jsxImportSource: opts.jsxImportSource,
|
||||
jsxSideEffects: opts.jsxPure === false
|
||||
}
|
||||
};
|
||||
}
|
||||
},
|
||||
configResolved(config) {
|
||||
devBase = config.base;
|
||||
projectRoot = config.root;
|
||||
filter = vite.createFilter(opts.include, opts.exclude, {
|
||||
resolve: projectRoot
|
||||
});
|
||||
needHiresSourcemap = config.command === "build" && !!config.build.sourcemap;
|
||||
isProduction = config.isProduction;
|
||||
skipFastRefresh || (skipFastRefresh = isProduction || config.command === "build");
|
||||
const jsxInject = config.esbuild && config.esbuild.jsxInject;
|
||||
if (jsxInject && importReactRE.test(jsxInject)) {
|
||||
skipReactImport = true;
|
||||
config.logger.warn(
|
||||
"[@vitejs/plugin-react] This plugin imports React for you automatically, so you can stop using `esbuild.jsxInject` for that purpose."
|
||||
);
|
||||
}
|
||||
config.plugins.forEach((plugin) => {
|
||||
const hasConflict = plugin.name === "react-refresh" || plugin !== viteReactJsx && plugin.name === "vite:react-jsx";
|
||||
if (hasConflict)
|
||||
return config.logger.warn(
|
||||
`[@vitejs/plugin-react] You should stop using "${plugin.name}" since this plugin conflicts with it.`
|
||||
);
|
||||
});
|
||||
runPluginOverrides = (babelOptions, context) => {
|
||||
const hooks = config.plugins.map((plugin) => plugin.api?.reactBabel).filter(Boolean);
|
||||
if (hooks.length > 0) {
|
||||
return (runPluginOverrides = (babelOptions2, context2) => {
|
||||
hooks.forEach((hook) => hook(babelOptions2, context2, config));
|
||||
return true;
|
||||
})(babelOptions, context);
|
||||
}
|
||||
runPluginOverrides = () => false;
|
||||
return false;
|
||||
};
|
||||
},
|
||||
async transform(code, id, options) {
|
||||
const ssr = options?.ssr === true;
|
||||
const [filepath, querystring = ""] = id.split("?");
|
||||
const [extension = ""] = querystring.match(fileExtensionRE) || filepath.match(fileExtensionRE) || [];
|
||||
if (/\.(?:mjs|[tj]sx?)$/.test(extension)) {
|
||||
const isJSX = extension.endsWith("x");
|
||||
const isNodeModules = id.includes("/node_modules/");
|
||||
const isProjectFile = !isNodeModules && (id[0] === "\0" || id.startsWith(projectRoot + "/"));
|
||||
let babelOptions = staticBabelOptions;
|
||||
if (typeof opts.babel === "function") {
|
||||
const rawOptions = opts.babel(id, { ssr });
|
||||
babelOptions = createBabelOptions(rawOptions);
|
||||
runPluginOverrides(babelOptions, { ssr, id });
|
||||
} else if (!babelOptions) {
|
||||
babelOptions = createBabelOptions(opts.babel);
|
||||
if (!runPluginOverrides(babelOptions, { ssr, id })) {
|
||||
staticBabelOptions = babelOptions;
|
||||
}
|
||||
}
|
||||
const plugins = isProjectFile ? [...babelOptions.plugins] : [];
|
||||
let useFastRefresh = false;
|
||||
if (!skipFastRefresh && !ssr && !isNodeModules) {
|
||||
const isReactModule = isJSX || importReactRE.test(code);
|
||||
if (isReactModule && filter(id)) {
|
||||
useFastRefresh = true;
|
||||
plugins.push([
|
||||
await loadPlugin("react-refresh/babel"),
|
||||
{ skipEnvCheck: true }
|
||||
]);
|
||||
}
|
||||
}
|
||||
let prependReactImport = false;
|
||||
if (!isProjectFile || isJSX) {
|
||||
if (!useAutomaticRuntime && isProjectFile) {
|
||||
if (!isProduction) {
|
||||
plugins.push(
|
||||
await loadPlugin("@babel/plugin-transform-react-jsx-self"),
|
||||
await loadPlugin("@babel/plugin-transform-react-jsx-source")
|
||||
);
|
||||
}
|
||||
if (!skipReactImport && !importReactRE.test(code)) {
|
||||
prependReactImport = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
let inputMap;
|
||||
if (prependReactImport) {
|
||||
if (needHiresSourcemap) {
|
||||
const s = new MagicString(code);
|
||||
s.prepend(prependReactImportCode);
|
||||
code = s.toString();
|
||||
inputMap = s.generateMap({ hires: true, source: id });
|
||||
} else {
|
||||
code = prependReactImportCode + code;
|
||||
}
|
||||
}
|
||||
const shouldSkip = !plugins.length && !babelOptions.configFile && !(isProjectFile && babelOptions.babelrc);
|
||||
if (shouldSkip) {
|
||||
return {
|
||||
code,
|
||||
map: inputMap ?? null
|
||||
};
|
||||
}
|
||||
const parserPlugins = [
|
||||
...babelOptions.parserOpts.plugins,
|
||||
"importMeta",
|
||||
// This plugin is applied before esbuild transforms the code,
|
||||
// so we need to enable some stage 3 syntax that is supported in
|
||||
// TypeScript and some environments already.
|
||||
"topLevelAwait",
|
||||
"classProperties",
|
||||
"classPrivateProperties",
|
||||
"classPrivateMethods"
|
||||
];
|
||||
if (!extension.endsWith(".ts")) {
|
||||
parserPlugins.push("jsx");
|
||||
}
|
||||
if (/\.tsx?$/.test(extension)) {
|
||||
parserPlugins.push("typescript");
|
||||
}
|
||||
const result = await babel__namespace.transformAsync(code, {
|
||||
...babelOptions,
|
||||
root: projectRoot,
|
||||
filename: id,
|
||||
sourceFileName: filepath,
|
||||
parserOpts: {
|
||||
...babelOptions.parserOpts,
|
||||
sourceType: "module",
|
||||
allowAwaitOutsideFunction: true,
|
||||
plugins: parserPlugins
|
||||
},
|
||||
generatorOpts: {
|
||||
...babelOptions.generatorOpts,
|
||||
decoratorsBeforeExport: true
|
||||
},
|
||||
plugins,
|
||||
sourceMaps: true,
|
||||
// Vite handles sourcemap flattening
|
||||
inputSourceMap: inputMap ?? false
|
||||
});
|
||||
if (result) {
|
||||
let code2 = result.code;
|
||||
if (useFastRefresh && refreshContentRE.test(code2)) {
|
||||
code2 = addRefreshWrapper(code2, id);
|
||||
}
|
||||
return {
|
||||
code: code2,
|
||||
map: result.map
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
const viteReactRefresh = {
|
||||
name: "vite:react-refresh",
|
||||
enforce: "pre",
|
||||
config: () => ({
|
||||
resolve: {
|
||||
dedupe: ["react", "react-dom"]
|
||||
}
|
||||
}),
|
||||
resolveId(id) {
|
||||
if (id === runtimePublicPath) {
|
||||
return id;
|
||||
}
|
||||
},
|
||||
load(id) {
|
||||
if (id === runtimePublicPath) {
|
||||
return runtimeCode;
|
||||
}
|
||||
},
|
||||
transformIndexHtml() {
|
||||
if (!skipFastRefresh)
|
||||
return [
|
||||
{
|
||||
tag: "script",
|
||||
attrs: { type: "module" },
|
||||
children: preambleCode.replace(`__BASE__`, devBase)
|
||||
}
|
||||
];
|
||||
}
|
||||
};
|
||||
const reactJsxRuntimeId = "react/jsx-runtime";
|
||||
const reactJsxDevRuntimeId = "react/jsx-dev-runtime";
|
||||
const virtualReactJsxRuntimeId = "\0" + reactJsxRuntimeId;
|
||||
const virtualReactJsxDevRuntimeId = "\0" + reactJsxDevRuntimeId;
|
||||
const viteReactJsx = {
|
||||
name: "vite:react-jsx",
|
||||
enforce: "pre",
|
||||
config() {
|
||||
return {
|
||||
optimizeDeps: {
|
||||
// We can't add `react-dom` because the dependency is `react-dom/client`
|
||||
// for React 18 while it's `react-dom` for React 17. We'd need to detect
|
||||
// what React version the user has installed.
|
||||
include: [reactJsxRuntimeId, reactJsxDevRuntimeId, "react"]
|
||||
}
|
||||
};
|
||||
},
|
||||
resolveId(id, importer) {
|
||||
if (id === reactJsxRuntimeId && importer !== virtualReactJsxRuntimeId) {
|
||||
return virtualReactJsxRuntimeId;
|
||||
}
|
||||
if (id === reactJsxDevRuntimeId && importer !== virtualReactJsxDevRuntimeId) {
|
||||
return virtualReactJsxDevRuntimeId;
|
||||
}
|
||||
},
|
||||
load(id) {
|
||||
if (id === virtualReactJsxRuntimeId) {
|
||||
return [
|
||||
`import * as jsxRuntime from ${JSON.stringify(reactJsxRuntimeId)}`,
|
||||
`export const Fragment = jsxRuntime.Fragment`,
|
||||
`export const jsx = jsxRuntime.jsx`,
|
||||
`export const jsxs = jsxRuntime.jsxs`
|
||||
].join("\n");
|
||||
}
|
||||
if (id === virtualReactJsxDevRuntimeId) {
|
||||
return [
|
||||
`import * as jsxRuntime from ${JSON.stringify(reactJsxDevRuntimeId)}`,
|
||||
`export const Fragment = jsxRuntime.Fragment`,
|
||||
`export const jsxDEV = jsxRuntime.jsxDEV`
|
||||
].join("\n");
|
||||
}
|
||||
}
|
||||
};
|
||||
return [viteBabel, viteReactRefresh, useAutomaticRuntime && viteReactJsx];
|
||||
}
|
||||
viteReact.preambleCode = preambleCode;
|
||||
function loadPlugin(path2) {
|
||||
return import(path2).then((module) => module.default || module);
|
||||
}
|
||||
function createBabelOptions(rawOptions) {
|
||||
var _a;
|
||||
const babelOptions = {
|
||||
babelrc: false,
|
||||
configFile: false,
|
||||
...rawOptions
|
||||
};
|
||||
babelOptions.plugins || (babelOptions.plugins = []);
|
||||
babelOptions.presets || (babelOptions.presets = []);
|
||||
babelOptions.overrides || (babelOptions.overrides = []);
|
||||
babelOptions.parserOpts || (babelOptions.parserOpts = {});
|
||||
(_a = babelOptions.parserOpts).plugins || (_a.plugins = []);
|
||||
return babelOptions;
|
||||
}
|
||||
|
||||
module.exports = viteReact;
|
||||
module.exports.default = viteReact;
|
||||
69
node_modules/@vitejs/plugin-react/dist/index.d.ts
generated
vendored
Normal file
69
node_modules/@vitejs/plugin-react/dist/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
import { TransformOptions, ParserOptions } from '@babel/core';
|
||||
import { ResolvedConfig, PluginOption } from 'vite';
|
||||
|
||||
interface Options {
|
||||
include?: string | RegExp | Array<string | RegExp>;
|
||||
exclude?: string | RegExp | Array<string | RegExp>;
|
||||
/**
|
||||
* Enable `react-refresh` integration. Vite disables this in prod env or build mode.
|
||||
* @default true
|
||||
*/
|
||||
fastRefresh?: boolean;
|
||||
/**
|
||||
* Set this to `"automatic"` to use [vite-react-jsx](https://github.com/alloc/vite-react-jsx).
|
||||
* @default "automatic"
|
||||
*/
|
||||
jsxRuntime?: 'classic' | 'automatic';
|
||||
/**
|
||||
* Control where the JSX factory is imported from.
|
||||
* This option is ignored when `jsxRuntime` is not `"automatic"`.
|
||||
* @default "react"
|
||||
*/
|
||||
jsxImportSource?: string;
|
||||
/**
|
||||
* Set this to `true` to annotate the JSX factory with `\/* @__PURE__ *\/`.
|
||||
* This option is ignored when `jsxRuntime` is not `"automatic"`.
|
||||
* @default true
|
||||
*/
|
||||
jsxPure?: boolean;
|
||||
/**
|
||||
* Babel configuration applied in both dev and prod.
|
||||
*/
|
||||
babel?: BabelOptions | ((id: string, options: {
|
||||
ssr?: boolean;
|
||||
}) => BabelOptions);
|
||||
}
|
||||
type BabelOptions = Omit<TransformOptions, 'ast' | 'filename' | 'root' | 'sourceFileName' | 'sourceMaps' | 'inputSourceMap'>;
|
||||
/**
|
||||
* The object type used by the `options` passed to plugins with
|
||||
* an `api.reactBabel` method.
|
||||
*/
|
||||
interface ReactBabelOptions extends BabelOptions {
|
||||
plugins: Extract<BabelOptions['plugins'], any[]>;
|
||||
presets: Extract<BabelOptions['presets'], any[]>;
|
||||
overrides: Extract<BabelOptions['overrides'], any[]>;
|
||||
parserOpts: ParserOptions & {
|
||||
plugins: Extract<ParserOptions['plugins'], any[]>;
|
||||
};
|
||||
}
|
||||
type ReactBabelHook = (babelConfig: ReactBabelOptions, context: ReactBabelHookContext, config: ResolvedConfig) => void;
|
||||
type ReactBabelHookContext = {
|
||||
ssr: boolean;
|
||||
id: string;
|
||||
};
|
||||
declare module 'vite' {
|
||||
interface Plugin {
|
||||
api?: {
|
||||
/**
|
||||
* Manipulate the Babel options of `@vitejs/plugin-react`
|
||||
*/
|
||||
reactBabel?: ReactBabelHook;
|
||||
};
|
||||
}
|
||||
}
|
||||
declare function viteReact(opts?: Options): PluginOption[];
|
||||
declare namespace viteReact {
|
||||
var preambleCode: string;
|
||||
}
|
||||
|
||||
export { BabelOptions, Options, ReactBabelOptions, viteReact as default };
|
||||
357
node_modules/@vitejs/plugin-react/dist/index.mjs
generated
vendored
Normal file
357
node_modules/@vitejs/plugin-react/dist/index.mjs
generated
vendored
Normal file
@@ -0,0 +1,357 @@
|
||||
import path from 'node:path';
|
||||
import * as babel from '@babel/core';
|
||||
import { createFilter, normalizePath, loadEnv, resolveEnvPrefix } from 'vite';
|
||||
import MagicString from 'magic-string';
|
||||
import fs from 'node:fs';
|
||||
import { createRequire } from 'node:module';
|
||||
|
||||
const runtimePublicPath = "/@react-refresh";
|
||||
const _require = createRequire(import.meta.url);
|
||||
const reactRefreshDir = path.dirname(
|
||||
_require.resolve("react-refresh/package.json")
|
||||
);
|
||||
const runtimeFilePath = path.join(
|
||||
reactRefreshDir,
|
||||
"cjs/react-refresh-runtime.development.js"
|
||||
);
|
||||
const runtimeCode = `
|
||||
const exports = {}
|
||||
${fs.readFileSync(runtimeFilePath, "utf-8")}
|
||||
${fs.readFileSync(_require.resolve("./refreshUtils.js"), "utf-8")}
|
||||
export default exports
|
||||
`;
|
||||
const preambleCode = `
|
||||
import RefreshRuntime from "__BASE__${runtimePublicPath.slice(1)}"
|
||||
RefreshRuntime.injectIntoGlobalHook(window)
|
||||
window.$RefreshReg$ = () => {}
|
||||
window.$RefreshSig$ = () => (type) => type
|
||||
window.__vite_plugin_react_preamble_installed__ = true
|
||||
`;
|
||||
const header = `
|
||||
import RefreshRuntime from "${runtimePublicPath}";
|
||||
|
||||
let prevRefreshReg;
|
||||
let prevRefreshSig;
|
||||
|
||||
if (import.meta.hot) {
|
||||
if (!window.__vite_plugin_react_preamble_installed__) {
|
||||
throw new Error(
|
||||
"@vitejs/plugin-react can't detect preamble. Something is wrong. " +
|
||||
"See https://github.com/vitejs/vite-plugin-react/pull/11#discussion_r430879201"
|
||||
);
|
||||
}
|
||||
|
||||
prevRefreshReg = window.$RefreshReg$;
|
||||
prevRefreshSig = window.$RefreshSig$;
|
||||
window.$RefreshReg$ = (type, id) => {
|
||||
RefreshRuntime.register(type, __SOURCE__ + " " + id)
|
||||
};
|
||||
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
|
||||
}`.replace(/\n+/g, "");
|
||||
const footer = `
|
||||
if (import.meta.hot) {
|
||||
window.$RefreshReg$ = prevRefreshReg;
|
||||
window.$RefreshSig$ = prevRefreshSig;
|
||||
|
||||
import(/* @vite-ignore */ import.meta.url).then((currentExports) => {
|
||||
RefreshRuntime.registerExportsForReactRefresh(__SOURCE__, currentExports);
|
||||
import.meta.hot.accept((nextExports) => {
|
||||
if (!nextExports) return;
|
||||
const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(currentExports, nextExports);
|
||||
if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
|
||||
});
|
||||
});
|
||||
}`;
|
||||
function addRefreshWrapper(code, id) {
|
||||
return header.replace("__SOURCE__", JSON.stringify(id)) + code + footer.replace("__SOURCE__", JSON.stringify(id));
|
||||
}
|
||||
|
||||
const prependReactImportCode = "import React from 'react'; ";
|
||||
const refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
|
||||
function viteReact(opts = {}) {
|
||||
let devBase = "/";
|
||||
let filter = createFilter(opts.include, opts.exclude);
|
||||
let needHiresSourcemap = false;
|
||||
let isProduction = true;
|
||||
let projectRoot = process.cwd();
|
||||
let skipFastRefresh = opts.fastRefresh === false;
|
||||
let skipReactImport = false;
|
||||
let runPluginOverrides = (options, context) => false;
|
||||
let staticBabelOptions;
|
||||
const useAutomaticRuntime = opts.jsxRuntime !== "classic";
|
||||
const importReactRE = /(?:^|\n)import\s+(?:\*\s+as\s+)?React(?:,|\s+)/;
|
||||
const fileExtensionRE = /\.[^/\s?]+$/;
|
||||
const viteBabel = {
|
||||
name: "vite:react-babel",
|
||||
enforce: "pre",
|
||||
config(userConfig, { mode }) {
|
||||
const resolvedRoot = normalizePath(
|
||||
userConfig.root ? path.resolve(userConfig.root) : process.cwd()
|
||||
);
|
||||
const envDir = userConfig.envDir ? normalizePath(path.resolve(resolvedRoot, userConfig.envDir)) : resolvedRoot;
|
||||
loadEnv(mode, envDir, resolveEnvPrefix(userConfig));
|
||||
const isProduction2 = (process.env.NODE_ENV || process.env.VITE_USER_NODE_ENV || mode) === "production";
|
||||
if (opts.jsxRuntime === "classic") {
|
||||
return {
|
||||
esbuild: {
|
||||
logOverride: {
|
||||
"this-is-undefined-in-esm": "silent"
|
||||
},
|
||||
jsx: "transform",
|
||||
jsxImportSource: opts.jsxImportSource,
|
||||
jsxSideEffects: opts.jsxPure === false
|
||||
}
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
esbuild: {
|
||||
jsxDev: !isProduction2,
|
||||
jsx: "automatic",
|
||||
jsxImportSource: opts.jsxImportSource,
|
||||
jsxSideEffects: opts.jsxPure === false
|
||||
}
|
||||
};
|
||||
}
|
||||
},
|
||||
configResolved(config) {
|
||||
devBase = config.base;
|
||||
projectRoot = config.root;
|
||||
filter = createFilter(opts.include, opts.exclude, {
|
||||
resolve: projectRoot
|
||||
});
|
||||
needHiresSourcemap = config.command === "build" && !!config.build.sourcemap;
|
||||
isProduction = config.isProduction;
|
||||
skipFastRefresh || (skipFastRefresh = isProduction || config.command === "build");
|
||||
const jsxInject = config.esbuild && config.esbuild.jsxInject;
|
||||
if (jsxInject && importReactRE.test(jsxInject)) {
|
||||
skipReactImport = true;
|
||||
config.logger.warn(
|
||||
"[@vitejs/plugin-react] This plugin imports React for you automatically, so you can stop using `esbuild.jsxInject` for that purpose."
|
||||
);
|
||||
}
|
||||
config.plugins.forEach((plugin) => {
|
||||
const hasConflict = plugin.name === "react-refresh" || plugin !== viteReactJsx && plugin.name === "vite:react-jsx";
|
||||
if (hasConflict)
|
||||
return config.logger.warn(
|
||||
`[@vitejs/plugin-react] You should stop using "${plugin.name}" since this plugin conflicts with it.`
|
||||
);
|
||||
});
|
||||
runPluginOverrides = (babelOptions, context) => {
|
||||
const hooks = config.plugins.map((plugin) => plugin.api?.reactBabel).filter(Boolean);
|
||||
if (hooks.length > 0) {
|
||||
return (runPluginOverrides = (babelOptions2, context2) => {
|
||||
hooks.forEach((hook) => hook(babelOptions2, context2, config));
|
||||
return true;
|
||||
})(babelOptions, context);
|
||||
}
|
||||
runPluginOverrides = () => false;
|
||||
return false;
|
||||
};
|
||||
},
|
||||
async transform(code, id, options) {
|
||||
const ssr = options?.ssr === true;
|
||||
const [filepath, querystring = ""] = id.split("?");
|
||||
const [extension = ""] = querystring.match(fileExtensionRE) || filepath.match(fileExtensionRE) || [];
|
||||
if (/\.(?:mjs|[tj]sx?)$/.test(extension)) {
|
||||
const isJSX = extension.endsWith("x");
|
||||
const isNodeModules = id.includes("/node_modules/");
|
||||
const isProjectFile = !isNodeModules && (id[0] === "\0" || id.startsWith(projectRoot + "/"));
|
||||
let babelOptions = staticBabelOptions;
|
||||
if (typeof opts.babel === "function") {
|
||||
const rawOptions = opts.babel(id, { ssr });
|
||||
babelOptions = createBabelOptions(rawOptions);
|
||||
runPluginOverrides(babelOptions, { ssr, id });
|
||||
} else if (!babelOptions) {
|
||||
babelOptions = createBabelOptions(opts.babel);
|
||||
if (!runPluginOverrides(babelOptions, { ssr, id })) {
|
||||
staticBabelOptions = babelOptions;
|
||||
}
|
||||
}
|
||||
const plugins = isProjectFile ? [...babelOptions.plugins] : [];
|
||||
let useFastRefresh = false;
|
||||
if (!skipFastRefresh && !ssr && !isNodeModules) {
|
||||
const isReactModule = isJSX || importReactRE.test(code);
|
||||
if (isReactModule && filter(id)) {
|
||||
useFastRefresh = true;
|
||||
plugins.push([
|
||||
await loadPlugin("react-refresh/babel"),
|
||||
{ skipEnvCheck: true }
|
||||
]);
|
||||
}
|
||||
}
|
||||
let prependReactImport = false;
|
||||
if (!isProjectFile || isJSX) {
|
||||
if (!useAutomaticRuntime && isProjectFile) {
|
||||
if (!isProduction) {
|
||||
plugins.push(
|
||||
await loadPlugin("@babel/plugin-transform-react-jsx-self"),
|
||||
await loadPlugin("@babel/plugin-transform-react-jsx-source")
|
||||
);
|
||||
}
|
||||
if (!skipReactImport && !importReactRE.test(code)) {
|
||||
prependReactImport = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
let inputMap;
|
||||
if (prependReactImport) {
|
||||
if (needHiresSourcemap) {
|
||||
const s = new MagicString(code);
|
||||
s.prepend(prependReactImportCode);
|
||||
code = s.toString();
|
||||
inputMap = s.generateMap({ hires: true, source: id });
|
||||
} else {
|
||||
code = prependReactImportCode + code;
|
||||
}
|
||||
}
|
||||
const shouldSkip = !plugins.length && !babelOptions.configFile && !(isProjectFile && babelOptions.babelrc);
|
||||
if (shouldSkip) {
|
||||
return {
|
||||
code,
|
||||
map: inputMap ?? null
|
||||
};
|
||||
}
|
||||
const parserPlugins = [
|
||||
...babelOptions.parserOpts.plugins,
|
||||
"importMeta",
|
||||
// This plugin is applied before esbuild transforms the code,
|
||||
// so we need to enable some stage 3 syntax that is supported in
|
||||
// TypeScript and some environments already.
|
||||
"topLevelAwait",
|
||||
"classProperties",
|
||||
"classPrivateProperties",
|
||||
"classPrivateMethods"
|
||||
];
|
||||
if (!extension.endsWith(".ts")) {
|
||||
parserPlugins.push("jsx");
|
||||
}
|
||||
if (/\.tsx?$/.test(extension)) {
|
||||
parserPlugins.push("typescript");
|
||||
}
|
||||
const result = await babel.transformAsync(code, {
|
||||
...babelOptions,
|
||||
root: projectRoot,
|
||||
filename: id,
|
||||
sourceFileName: filepath,
|
||||
parserOpts: {
|
||||
...babelOptions.parserOpts,
|
||||
sourceType: "module",
|
||||
allowAwaitOutsideFunction: true,
|
||||
plugins: parserPlugins
|
||||
},
|
||||
generatorOpts: {
|
||||
...babelOptions.generatorOpts,
|
||||
decoratorsBeforeExport: true
|
||||
},
|
||||
plugins,
|
||||
sourceMaps: true,
|
||||
// Vite handles sourcemap flattening
|
||||
inputSourceMap: inputMap ?? false
|
||||
});
|
||||
if (result) {
|
||||
let code2 = result.code;
|
||||
if (useFastRefresh && refreshContentRE.test(code2)) {
|
||||
code2 = addRefreshWrapper(code2, id);
|
||||
}
|
||||
return {
|
||||
code: code2,
|
||||
map: result.map
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
const viteReactRefresh = {
|
||||
name: "vite:react-refresh",
|
||||
enforce: "pre",
|
||||
config: () => ({
|
||||
resolve: {
|
||||
dedupe: ["react", "react-dom"]
|
||||
}
|
||||
}),
|
||||
resolveId(id) {
|
||||
if (id === runtimePublicPath) {
|
||||
return id;
|
||||
}
|
||||
},
|
||||
load(id) {
|
||||
if (id === runtimePublicPath) {
|
||||
return runtimeCode;
|
||||
}
|
||||
},
|
||||
transformIndexHtml() {
|
||||
if (!skipFastRefresh)
|
||||
return [
|
||||
{
|
||||
tag: "script",
|
||||
attrs: { type: "module" },
|
||||
children: preambleCode.replace(`__BASE__`, devBase)
|
||||
}
|
||||
];
|
||||
}
|
||||
};
|
||||
const reactJsxRuntimeId = "react/jsx-runtime";
|
||||
const reactJsxDevRuntimeId = "react/jsx-dev-runtime";
|
||||
const virtualReactJsxRuntimeId = "\0" + reactJsxRuntimeId;
|
||||
const virtualReactJsxDevRuntimeId = "\0" + reactJsxDevRuntimeId;
|
||||
const viteReactJsx = {
|
||||
name: "vite:react-jsx",
|
||||
enforce: "pre",
|
||||
config() {
|
||||
return {
|
||||
optimizeDeps: {
|
||||
// We can't add `react-dom` because the dependency is `react-dom/client`
|
||||
// for React 18 while it's `react-dom` for React 17. We'd need to detect
|
||||
// what React version the user has installed.
|
||||
include: [reactJsxRuntimeId, reactJsxDevRuntimeId, "react"]
|
||||
}
|
||||
};
|
||||
},
|
||||
resolveId(id, importer) {
|
||||
if (id === reactJsxRuntimeId && importer !== virtualReactJsxRuntimeId) {
|
||||
return virtualReactJsxRuntimeId;
|
||||
}
|
||||
if (id === reactJsxDevRuntimeId && importer !== virtualReactJsxDevRuntimeId) {
|
||||
return virtualReactJsxDevRuntimeId;
|
||||
}
|
||||
},
|
||||
load(id) {
|
||||
if (id === virtualReactJsxRuntimeId) {
|
||||
return [
|
||||
`import * as jsxRuntime from ${JSON.stringify(reactJsxRuntimeId)}`,
|
||||
`export const Fragment = jsxRuntime.Fragment`,
|
||||
`export const jsx = jsxRuntime.jsx`,
|
||||
`export const jsxs = jsxRuntime.jsxs`
|
||||
].join("\n");
|
||||
}
|
||||
if (id === virtualReactJsxDevRuntimeId) {
|
||||
return [
|
||||
`import * as jsxRuntime from ${JSON.stringify(reactJsxDevRuntimeId)}`,
|
||||
`export const Fragment = jsxRuntime.Fragment`,
|
||||
`export const jsxDEV = jsxRuntime.jsxDEV`
|
||||
].join("\n");
|
||||
}
|
||||
}
|
||||
};
|
||||
return [viteBabel, viteReactRefresh, useAutomaticRuntime && viteReactJsx];
|
||||
}
|
||||
viteReact.preambleCode = preambleCode;
|
||||
function loadPlugin(path2) {
|
||||
return import(path2).then((module) => module.default || module);
|
||||
}
|
||||
function createBabelOptions(rawOptions) {
|
||||
var _a;
|
||||
const babelOptions = {
|
||||
babelrc: false,
|
||||
configFile: false,
|
||||
...rawOptions
|
||||
};
|
||||
babelOptions.plugins || (babelOptions.plugins = []);
|
||||
babelOptions.presets || (babelOptions.presets = []);
|
||||
babelOptions.overrides || (babelOptions.overrides = []);
|
||||
babelOptions.parserOpts || (babelOptions.parserOpts = {});
|
||||
(_a = babelOptions.parserOpts).plugins || (_a.plugins = []);
|
||||
return babelOptions;
|
||||
}
|
||||
|
||||
export { viteReact as default };
|
||||
58
node_modules/@vitejs/plugin-react/dist/refreshUtils.js
generated
vendored
Normal file
58
node_modules/@vitejs/plugin-react/dist/refreshUtils.js
generated
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
function debounce(fn, delay) {
|
||||
let handle
|
||||
return () => {
|
||||
clearTimeout(handle)
|
||||
handle = setTimeout(fn, delay)
|
||||
}
|
||||
}
|
||||
|
||||
/* eslint-disable no-undef */
|
||||
const enqueueUpdate = debounce(exports.performReactRefresh, 16)
|
||||
|
||||
// Taken from https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/lib/runtime/RefreshUtils.js#L141
|
||||
// This allows to resister components not detected by SWC like styled component
|
||||
function registerExportsForReactRefresh(filename, moduleExports) {
|
||||
for (const key in moduleExports) {
|
||||
if (key === '__esModule') continue
|
||||
const exportValue = moduleExports[key]
|
||||
if (exports.isLikelyComponentType(exportValue)) {
|
||||
exports.register(exportValue, filename + ' ' + key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function validateRefreshBoundaryAndEnqueueUpdate(prevExports, nextExports) {
|
||||
if (!predicateOnExport(prevExports, (key) => !!nextExports[key])) {
|
||||
return 'Could not Fast Refresh (export removed)'
|
||||
}
|
||||
|
||||
let hasExports = false
|
||||
const allExportsAreComponentsOrUnchanged = predicateOnExport(
|
||||
nextExports,
|
||||
(key, value) => {
|
||||
hasExports = true
|
||||
if (exports.isLikelyComponentType(value)) return true
|
||||
if (!prevExports[key]) return false
|
||||
return prevExports[key] === nextExports[key]
|
||||
},
|
||||
)
|
||||
if (hasExports && allExportsAreComponentsOrUnchanged) {
|
||||
enqueueUpdate()
|
||||
} else {
|
||||
return 'Could not Fast Refresh. Learn more at https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react#consistent-components-exports'
|
||||
}
|
||||
}
|
||||
|
||||
function predicateOnExport(moduleExports, predicate) {
|
||||
for (const key in moduleExports) {
|
||||
if (key === '__esModule') continue
|
||||
const desc = Object.getOwnPropertyDescriptor(moduleExports, key)
|
||||
if (desc && desc.get) return false
|
||||
if (!predicate(key, moduleExports[key])) return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
exports.registerExportsForReactRefresh = registerExportsForReactRefresh
|
||||
exports.validateRefreshBoundaryAndEnqueueUpdate =
|
||||
validateRefreshBoundaryAndEnqueueUpdate
|
||||
Reference in New Issue
Block a user