Update dependencies and clean up project structure. Add cloud.google.com/go/compute/metadata as an indirect requirement in go.mod. Remove unused files and update package.json for Vite and React plugins. Refactor Makefile for improved build process.

This commit is contained in:
2025-11-22 06:53:24 -06:00
parent c9ade39ad9
commit 30aa969433
1295 changed files with 343445 additions and 210041 deletions

View File

@@ -14,15 +14,22 @@ injectIntoGlobalHook(window);
window.$RefreshReg$ = () => {};
window.$RefreshSig$ = () => (type) => type;`;
const getPreambleCode = (base) => preambleCode.replace("__BASE__", base);
function addRefreshWrapper(code, pluginName, id, reactRefreshHost = "") {
const avoidSourceMapOption = Symbol();
function addRefreshWrapper(code, map, pluginName, id, reactRefreshHost = "") {
const hasRefresh = refreshContentRE.test(code);
const onlyReactComp = !hasRefresh && reactCompRE.test(code);
if (!hasRefresh && !onlyReactComp) return void 0;
const normalizedMap = map === avoidSourceMapOption ? null : map;
if (!hasRefresh && !onlyReactComp) return {
code,
map: normalizedMap
};
const avoidSourceMap = map === avoidSourceMapOption;
const newMap = typeof normalizedMap === "string" ? JSON.parse(normalizedMap) : normalizedMap;
let newCode = code;
newCode += `
if (hasRefresh) {
const refreshHead = removeLineBreaksIfNeeded(`let prevRefreshReg;
let prevRefreshSig;
import * as RefreshRuntime from "${reactRefreshHost}${runtimePublicPath}";
const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
if (import.meta.hot && !inWebWorker) {
if (!window.$RefreshReg$) {
throw new Error(
@@ -30,6 +37,29 @@ if (import.meta.hot && !inWebWorker) {
);
}
prevRefreshReg = window.$RefreshReg$;
prevRefreshSig = window.$RefreshSig$;
window.$RefreshReg$ = RefreshRuntime.getRefreshReg(${JSON.stringify(id)});
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
}
`, avoidSourceMap);
newCode = `${refreshHead}${newCode}
if (import.meta.hot && !inWebWorker) {
window.$RefreshReg$ = prevRefreshReg;
window.$RefreshSig$ = prevRefreshSig;
}
`;
if (newMap) newMap.mappings = ";".repeat(16) + newMap.mappings;
}
const sharedHead = removeLineBreaksIfNeeded(`import * as RefreshRuntime from "${reactRefreshHost}${runtimePublicPath}";
const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
`, avoidSourceMap);
newCode = `${sharedHead}${newCode}
if (import.meta.hot && !inWebWorker) {
RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
RefreshRuntime.registerExportsForReactRefresh(${JSON.stringify(id)}, currentExports);
import.meta.hot.accept((nextExports) => {
@@ -40,126 +70,92 @@ if (import.meta.hot && !inWebWorker) {
});
}
`;
if (hasRefresh) newCode += `function $RefreshReg$(type, id) { return RefreshRuntime.register(type, ${JSON.stringify(id)} + ' ' + id) }
function $RefreshSig$() { return RefreshRuntime.createSignatureFunctionForTransform(); }
`;
return newCode;
}
function virtualPreamblePlugin({ name, isEnabled }) {
if (newMap) newMap.mappings = ";;;" + newMap.mappings;
return {
name: "vite:react-virtual-preamble",
resolveId: {
order: "pre",
filter: { id: exactRegex(name) },
handler(source) {
if (source === name) return "\0" + source;
}
},
load: {
filter: { id: exactRegex("\0" + name) },
handler(id) {
if (id === "\0" + name) {
if (isEnabled()) return preambleCode.replace("__BASE__", "/");
return "";
}
}
}
code: newCode,
map: newMap
};
}
function removeLineBreaksIfNeeded(code, enabled) {
return enabled ? code.replace(/\n/g, "") : code;
}
//#endregion
//#region ../common/warning.ts
const silenceUseClientWarning = (userConfig) => ({ rollupOptions: { onwarn(warning, defaultHandler) {
var _userConfig$build;
if (warning.code === "MODULE_LEVEL_DIRECTIVE" && (warning.message.includes("use client") || warning.message.includes("use server"))) return;
if (warning.code === "SOURCEMAP_ERROR" && warning.message.includes("resolve original location") && warning.pos === 0) return;
if (userConfig.build?.rollupOptions?.onwarn) userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
if ((_userConfig$build = userConfig.build) === null || _userConfig$build === void 0 || (_userConfig$build = _userConfig$build.rollupOptions) === null || _userConfig$build === void 0 ? void 0 : _userConfig$build.onwarn) userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
else defaultHandler(warning);
} } });
//#endregion
//#region src/index.ts
const refreshRuntimePath = join(dirname(fileURLToPath(import.meta.url)), "refresh-runtime.js");
const _dirname = dirname(fileURLToPath(import.meta.url));
const refreshRuntimePath = join(_dirname, "refresh-runtime.js");
let babel;
async function loadBabel() {
if (!babel) babel = await import("@babel/core");
return babel;
}
const defaultIncludeRE = /\.[tj]sx?$/;
const defaultExcludeRE = /\/node_modules\//;
const tsRE = /\.tsx?$/;
const compilerAnnotationRE = /['"]use memo['"]/;
function viteReact(opts = {}) {
var _opts$babel;
const include = opts.include ?? defaultIncludeRE;
const exclude = opts.exclude ?? defaultExcludeRE;
const exclude = opts.exclude;
const filter = createFilter(include, exclude);
const jsxImportSource = opts.jsxImportSource ?? "react";
const jsxImportRuntime = `${jsxImportSource}/jsx-runtime`;
const jsxImportDevRuntime = `${jsxImportSource}/jsx-dev-runtime`;
const isRolldownVite = "rolldownVersion" in vite;
let runningInVite = false;
let isProduction = true;
let projectRoot = process.cwd();
let skipFastRefresh = true;
let base;
let isFullBundle = false;
let runPluginOverrides;
let staticBabelOptions;
const importReactRE = /\bimport\s+(?:\*\s+as\s+)?React\b/;
const viteBabel = {
name: "vite:react-babel",
enforce: "pre",
config(_userConfig, { command }) {
if ("rolldownVersion" in vite) if (opts.jsxRuntime === "classic") return { oxc: {
jsx: {
runtime: "classic",
refresh: command === "serve",
development: false
},
jsxRefreshInclude: makeIdFiltersToMatchWithQuery(include),
jsxRefreshExclude: makeIdFiltersToMatchWithQuery(exclude)
} };
else return {
oxc: {
jsx: {
runtime: "automatic",
importSource: opts.jsxImportSource,
refresh: command === "serve"
},
jsxRefreshInclude: makeIdFiltersToMatchWithQuery(include),
jsxRefreshExclude: makeIdFiltersToMatchWithQuery(exclude)
},
optimizeDeps: { rolldownOptions: { transform: { jsx: { runtime: "automatic" } } } }
};
if (opts.jsxRuntime === "classic") return { esbuild: { jsx: "transform" } };
config() {
if (opts.jsxRuntime === "classic") if ("rolldownVersion" in vite) return { oxc: { jsx: {
runtime: "classic",
development: false
} } };
else return { esbuild: { jsx: "transform" } };
else return {
esbuild: {
jsx: "automatic",
jsxImportSource: opts.jsxImportSource
},
optimizeDeps: { esbuildOptions: { jsx: "automatic" } }
optimizeDeps: "rolldownVersion" in vite ? { rollupOptions: { jsx: { mode: "automatic" } } } : { esbuildOptions: { jsx: "automatic" } }
};
},
configResolved(config) {
runningInVite = true;
base = config.base;
if (config.experimental.fullBundleMode) isFullBundle = true;
projectRoot = config.root;
isProduction = config.isProduction;
skipFastRefresh = isProduction || config.command === "build" || config.server.hmr === false;
const hooks = config.plugins.map((plugin) => plugin.api?.reactBabel).filter(defined);
if ("jsxPure" in opts) config.logger.warnOnce("[@vitejs/plugin-react] jsxPure was removed. You can configure esbuild.jsxSideEffects directly.");
const hooks = config.plugins.map((plugin) => {
var _plugin$api;
return (_plugin$api = plugin.api) === null || _plugin$api === void 0 ? void 0 : _plugin$api.reactBabel;
}).filter(defined);
if ("rolldownVersion" in vite && !opts.babel && !hooks.length && !opts.disableOxcRecommendation) config.logger.warn("[vite:react-babel] We recommend switching to `@vitejs/plugin-react-oxc` for improved performance. More information at https://vite.dev/rolldown");
if (hooks.length > 0) runPluginOverrides = (babelOptions, context) => {
hooks.forEach((hook) => hook(babelOptions, context, config));
};
else if (typeof opts.babel !== "function") {
staticBabelOptions = createBabelOptions(opts.babel);
if ((isRolldownVite || skipFastRefresh) && canSkipBabel(staticBabelOptions.plugins, staticBabelOptions) && (opts.jsxRuntime === "classic" ? isProduction : true)) delete viteBabel.transform;
if (canSkipBabel(staticBabelOptions.plugins, staticBabelOptions) && skipFastRefresh && (opts.jsxRuntime === "classic" ? isProduction : true)) delete viteBabel.transform;
}
},
options(options) {
if (!runningInVite) {
options.transform ??= {};
options.transform.jsx = {
runtime: opts.jsxRuntime,
options.jsx = {
mode: opts.jsxRuntime,
importSource: opts.jsxImportSource
};
return options;
@@ -168,33 +164,25 @@ function viteReact(opts = {}) {
transform: {
filter: { id: {
include: makeIdFiltersToMatchWithQuery(include),
exclude: makeIdFiltersToMatchWithQuery(exclude)
exclude: [...exclude ? makeIdFiltersToMatchWithQuery(ensureArray(exclude)) : [], /\/node_modules\//]
} },
async handler(code, id, options) {
if (id.includes("/node_modules/")) return;
const [filepath] = id.split("?");
if (!filter(filepath)) return;
const ssr = options?.ssr === true;
const ssr = (options === null || options === void 0 ? void 0 : options.ssr) === true;
const babelOptions = (() => {
if (staticBabelOptions) return staticBabelOptions;
const newBabelOptions = createBabelOptions(typeof opts.babel === "function" ? opts.babel(id, { ssr }) : opts.babel);
runPluginOverrides?.(newBabelOptions, {
runPluginOverrides === null || runPluginOverrides === void 0 || runPluginOverrides(newBabelOptions, {
id,
ssr
});
return newBabelOptions;
})();
const plugins = [...babelOptions.plugins];
let reactCompilerPlugin$1 = getReactCompilerPlugin(plugins);
if (reactCompilerPlugin$1 && ssr) {
plugins.splice(plugins.indexOf(reactCompilerPlugin$1), 1);
reactCompilerPlugin$1 = void 0;
}
if (Array.isArray(reactCompilerPlugin$1) && reactCompilerPlugin$1[1]?.compilationMode === "annotation" && !compilerAnnotationRE.test(code)) {
plugins.splice(plugins.indexOf(reactCompilerPlugin$1), 1);
reactCompilerPlugin$1 = void 0;
}
const isJSX = filepath.endsWith("x");
const useFastRefresh = !(isRolldownVite || skipFastRefresh) && !ssr && (isJSX || (opts.jsxRuntime === "classic" ? importReactRE.test(code) : code.includes(jsxImportDevRuntime) || code.includes(jsxImportRuntime)));
const useFastRefresh = !skipFastRefresh && !ssr && (isJSX || (opts.jsxRuntime === "classic" ? importReactRE.test(code) : code.includes(jsxImportDevRuntime) || code.includes(jsxImportRuntime)));
if (useFastRefresh) plugins.push([await loadPlugin("react-refresh/babel"), { skipEnvCheck: true }]);
if (opts.jsxRuntime === "classic" && isJSX) {
if (!isProduction) plugins.push(await loadPlugin("@babel/plugin-transform-react-jsx-self"), await loadPlugin("@babel/plugin-transform-react-jsx-source"));
@@ -203,12 +191,13 @@ function viteReact(opts = {}) {
const parserPlugins = [...babelOptions.parserOpts.plugins];
if (!filepath.endsWith(".ts")) parserPlugins.push("jsx");
if (tsRE.test(filepath)) parserPlugins.push("typescript");
const result = await (await loadBabel()).transformAsync(code, {
const babel$1 = await loadBabel();
const result = await babel$1.transformAsync(code, {
...babelOptions,
root: projectRoot,
filename: id,
sourceFileName: filepath,
retainLines: reactCompilerPlugin$1 ? false : !isProduction && isJSX && opts.jsxRuntime !== "classic",
retainLines: getReactCompilerPlugin(plugins) != null ? false : !isProduction && isJSX && opts.jsxRuntime !== "classic",
parserOpts: {
...babelOptions.parserOpts,
sourceType: "module",
@@ -228,83 +217,19 @@ function viteReact(opts = {}) {
code: result.code,
map: result.map
};
return {
code: addRefreshWrapper(result.code, "@vitejs/plugin-react", id, opts.reactRefreshHost) ?? result.code,
map: result.map
};
return addRefreshWrapper(result.code, result.map, "@vitejs/plugin-react", id, opts.reactRefreshHost);
}
}
}
};
const viteRefreshWrapper = {
name: "vite:react:refresh-wrapper",
apply: "serve",
async applyToEnvironment(env) {
if (env.config.consumer !== "client" || skipFastRefresh) return false;
let nativePlugin;
try {
nativePlugin = (await import("vite/internal")).reactRefreshWrapperPlugin;
} catch {}
if (!nativePlugin || [
"7.1.10",
"7.1.11",
"7.1.12"
].includes(vite.version)) return true;
delete viteRefreshWrapper.transform;
return nativePlugin({
cwd: process.cwd(),
include: makeIdFiltersToMatchWithQuery(include),
exclude: makeIdFiltersToMatchWithQuery(exclude),
jsxImportSource,
reactRefreshHost: opts.reactRefreshHost ?? ""
});
},
transform: {
filter: { id: {
include: makeIdFiltersToMatchWithQuery(include),
exclude: makeIdFiltersToMatchWithQuery(exclude)
} },
handler(code, id, options) {
const ssr = options?.ssr === true;
const [filepath] = id.split("?");
const isJSX = filepath.endsWith("x");
if (!(!skipFastRefresh && !ssr && (isJSX || code.includes(jsxImportDevRuntime) || code.includes(jsxImportRuntime)))) return;
const newCode = addRefreshWrapper(code, "@vitejs/plugin-react", id, opts.reactRefreshHost);
return newCode ? {
code: newCode,
map: null
} : void 0;
}
}
};
const viteConfigPost = {
name: "vite:react:config-post",
enforce: "post",
config(userConfig) {
if (userConfig.server?.hmr === false) return { oxc: { jsx: { refresh: false } } };
}
};
const viteReactRefreshFullBundleMode = {
name: "vite:react-refresh-fbm",
enforce: "pre",
transformIndexHtml: {
handler() {
if (!skipFastRefresh && isFullBundle) return [{
tag: "script",
attrs: { type: "module" },
children: getPreambleCode(base)
}];
},
order: "pre"
}
};
const dependencies = [
"react",
"react-dom",
jsxImportDevRuntime,
jsxImportRuntime
];
const reactCompilerPlugin = getReactCompilerPlugin(typeof opts.babel === "object" ? opts.babel?.plugins ?? [] : []);
const staticBabelPlugins = typeof opts.babel === "object" ? ((_opts$babel = opts.babel) === null || _opts$babel === void 0 ? void 0 : _opts$babel.plugins) ?? [] : [];
const reactCompilerPlugin = getReactCompilerPlugin(staticBabelPlugins);
if (reactCompilerPlugin != null) {
const reactCompilerRuntimeModule = getReactCompilerRuntimeModule(reactCompilerPlugin);
dependencies.push(reactCompilerRuntimeModule);
@@ -314,7 +239,8 @@ function viteReact(opts = {}) {
enforce: "pre",
config: (userConfig) => ({
build: silenceUseClientWarning(userConfig),
optimizeDeps: { include: dependencies }
optimizeDeps: { include: dependencies },
resolve: { dedupe: ["react", "react-dom"] }
}),
resolveId: {
filter: { id: exactRegex(runtimePublicPath) },
@@ -328,33 +254,17 @@ function viteReact(opts = {}) {
if (id === runtimePublicPath) return readFileSync(refreshRuntimePath, "utf-8").replace(/__README_URL__/g, "https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react");
}
},
transformIndexHtml() {
if (!skipFastRefresh && !isFullBundle) return [{
transformIndexHtml(_, config) {
if (!skipFastRefresh) return [{
tag: "script",
attrs: { type: "module" },
children: getPreambleCode(base)
children: getPreambleCode(config.server.config.base)
}];
}
};
return [
viteBabel,
...isRolldownVite ? [
viteRefreshWrapper,
viteConfigPost,
viteReactRefreshFullBundleMode
] : [],
viteReactRefresh,
virtualPreamblePlugin({
name: "@vitejs/plugin-react/preamble",
isEnabled: () => !skipFastRefresh && !isFullBundle
})
];
return [viteBabel, viteReactRefresh];
}
viteReact.preambleCode = preambleCode;
function viteReactForCjs(options) {
return viteReact.call(this, options);
}
Object.assign(viteReactForCjs, { default: viteReactForCjs });
function canSkipBabel(plugins, babelOptions) {
return !(plugins.length || babelOptions.presets.length || babelOptions.configFile || babelOptions.babelrc);
}
@@ -371,16 +281,17 @@ function loadPlugin(path) {
return promise;
}
function createBabelOptions(rawOptions) {
var _babelOptions$parserO;
const babelOptions = {
babelrc: false,
configFile: false,
...rawOptions
};
babelOptions.plugins ||= [];
babelOptions.presets ||= [];
babelOptions.overrides ||= [];
babelOptions.parserOpts ||= {};
babelOptions.parserOpts.plugins ||= [];
babelOptions.plugins || (babelOptions.plugins = []);
babelOptions.presets || (babelOptions.presets = []);
babelOptions.overrides || (babelOptions.overrides = []);
babelOptions.parserOpts || (babelOptions.parserOpts = {});
(_babelOptions$parserO = babelOptions.parserOpts).plugins || (_babelOptions$parserO.plugins = []);
return babelOptions;
}
function defined(value) {
@@ -392,10 +303,18 @@ function getReactCompilerPlugin(plugins) {
function getReactCompilerRuntimeModule(plugin) {
let moduleName = "react/compiler-runtime";
if (Array.isArray(plugin)) {
if (plugin[1]?.target === "17" || plugin[1]?.target === "18") moduleName = "react-compiler-runtime";
var _plugin$, _plugin$2, _plugin$3;
if (((_plugin$ = plugin[1]) === null || _plugin$ === void 0 ? void 0 : _plugin$.target) === "17" || ((_plugin$2 = plugin[1]) === null || _plugin$2 === void 0 ? void 0 : _plugin$2.target) === "18") moduleName = "react-compiler-runtime";
else if (typeof ((_plugin$3 = plugin[1]) === null || _plugin$3 === void 0 ? void 0 : _plugin$3.runtimeModule) === "string") {
var _plugin$4;
moduleName = (_plugin$4 = plugin[1]) === null || _plugin$4 === void 0 ? void 0 : _plugin$4.runtimeModule;
}
}
return moduleName;
}
function ensureArray(value) {
return Array.isArray(value) ? value : [value];
}
//#endregion
export { viteReact as default, viteReactForCjs as "module.exports" };
export { viteReact as default };