mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2026-07-24 00:43:29 +10:00
fix: monkey patch a nitro build error (resolves #3065)
This commit is contained in:
+82
-1
@@ -16,6 +16,86 @@ const rootPackageJson = JSON.parse(readFileSync(rootPackageJsonPath, "utf-8")) a
|
||||
const appVersion = JSON.stringify(rootPackageJson.version ?? "0.0.0");
|
||||
const workspaceRoot = fileURLToPath(new URL("../..", import.meta.url));
|
||||
|
||||
const rolldownRuntimeSupport = `var __create = Object.create;
|
||||
var __defProp = Object.defineProperty;
|
||||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||
var __getProtoOf = Object.getPrototypeOf;
|
||||
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
||||
var __copyProps = (to, from, except, desc) => {
|
||||
\tif (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
||||
\t\tkey = keys[i];
|
||||
\t\tif (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
||||
\t\t\tget: ((k) => from[k]).bind(null, key),
|
||||
\t\t\tenumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
||||
\t\t});
|
||||
\t}
|
||||
\treturn to;
|
||||
};
|
||||
`;
|
||||
|
||||
const rolldownRuntimeHelpers = {
|
||||
__commonJSMin:
|
||||
"var __commonJSMin = (cb, mod) => () => (mod || (cb((mod = { exports: {} }).exports, mod), cb = null), mod.exports);\n",
|
||||
__esmMin: "var __esmMin = (fn, res) => () => (fn && (res = fn(fn = 0)), res);\n",
|
||||
__exportAll: `var __exportAll = (all, no_symbols) => {
|
||||
\tlet target = {};
|
||||
\tfor (var name in all) __defProp(target, name, {
|
||||
\t\tget: all[name],
|
||||
\t\tenumerable: true
|
||||
\t});
|
||||
\tif (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
||||
\treturn target;
|
||||
};
|
||||
`,
|
||||
__require: "var __require = __reactiveResumeCreateRequire(import.meta.url);\n",
|
||||
__toCommonJS:
|
||||
'var __toCommonJS = (mod) => __hasOwnProp.call(mod, "module.exports") ? mod["module.exports"] : __copyProps(__defProp({}, "__esModule", { value: true }), mod);\n',
|
||||
__toESM:
|
||||
'var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {\n\tvalue: mod,\n\tenumerable: true\n}) : target, mod));\n',
|
||||
};
|
||||
|
||||
const rolldownRuntimeHelperNames = Object.keys(rolldownRuntimeHelpers) as (keyof typeof rolldownRuntimeHelpers)[];
|
||||
|
||||
const hasImportedBinding = (code: string, binding: string) => {
|
||||
for (const [, specifiers] of code.matchAll(/import\s*\{([^}]*)\}/g)) {
|
||||
for (const specifier of specifiers.split(",")) {
|
||||
const [, importedBinding] = specifier.trim().split(/\s+as\s+/);
|
||||
const localBinding = importedBinding ?? specifier.trim();
|
||||
if (localBinding === binding) return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
// Work around Nitro/Rolldown emitting runtime-helper uses before helper bindings in server chunks.
|
||||
const ensureRolldownRuntimeHelpers = (): Plugin => ({
|
||||
name: "reactive-resume:ensure-rolldown-runtime-helpers",
|
||||
apply: "build",
|
||||
applyToEnvironment: (environment) => environment.name === "nitro",
|
||||
renderChunk(code) {
|
||||
const missingHelpers = rolldownRuntimeHelperNames.filter((helper) => {
|
||||
const firstUse = code.search(new RegExp(`\\b${helper}(?![\\w$])\\s*\\(`));
|
||||
if (firstUse === -1 || hasImportedBinding(code, helper)) return false;
|
||||
|
||||
const helperDefinition = code.search(new RegExp(`\\b(?:const|let|var)\\s+${helper}\\s*=`));
|
||||
return helperDefinition === -1 || helperDefinition > firstUse;
|
||||
});
|
||||
|
||||
if (missingHelpers.length === 0) return null;
|
||||
|
||||
const createRequireImport = missingHelpers.includes("__require")
|
||||
? 'import { createRequire as __reactiveResumeCreateRequire } from "node:module";\n'
|
||||
: "";
|
||||
|
||||
return {
|
||||
code: `${createRequireImport}${rolldownRuntimeSupport}${missingHelpers.map((helper) => rolldownRuntimeHelpers[helper]).join("")}${code}`,
|
||||
map: null,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
const pwa = (): PluginOption =>
|
||||
VitePWA({
|
||||
outDir: ".output/public",
|
||||
@@ -52,7 +132,7 @@ export default defineConfig({
|
||||
build: {
|
||||
chunkSizeWarningLimit: 10 * 1024, // 10 MB
|
||||
rolldownOptions: {
|
||||
external: ["bcrypt", "sharp", "@aws-sdk/client-s3"],
|
||||
external: ["bcrypt", "sharp", "@aws-sdk/client-s3", "ioredis", "linkedom"],
|
||||
},
|
||||
},
|
||||
|
||||
@@ -69,6 +149,7 @@ export default defineConfig({
|
||||
lingui(),
|
||||
babel({ presets: [linguiTransformerBabelPreset()] }),
|
||||
nitro({ plugins: ["plugins/1.migrate.ts", "plugins/2.storage.ts"] }),
|
||||
ensureRolldownRuntimeHelpers(),
|
||||
pwa(),
|
||||
],
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user