diff --git a/builder/src/build/build-worker/index.ts b/builder/src/build/build-worker/index.ts index 0e6187a..be2d2f0 100644 --- a/builder/src/build/build-worker/index.ts +++ b/builder/src/build/build-worker/index.ts @@ -90,6 +90,26 @@ export async function buildWorker( */ "" } globalThis.__dirname ??= ""; + +// Do not crash on cache not supported +let isPatchedAlready = globalThis.fetch.__nextPatched; +const curFetch = globalThis.fetch; +globalThis.fetch = (input, init) => { + console.log("globalThis.fetch", input); + if (init) delete init.cache; + return curFetch(input, init); +}; +globalThis.fetch.__nextPatched = isPatchedAlready; +fetch = globalThis.fetch; +const CustomRequest = class extends globalThis.Request { + constructor(input, init) { + console.log("CustomRequest", input); + if (init) delete init.cache; + super(input, init); + } +}; +globalThis.Request = CustomRequest; +Request = globalThis.Request; `, }, }); @@ -185,6 +205,9 @@ function createFixRequiresESBuildPlugin(templateDir: string): Plugin { build.onResolve({ filter: /^\.\/require-hook$/ }, (args) => ({ path: `${templateDir}/shims/empty.ts`, })); + build.onResolve({ filter: /\.\/lib\/node-fs-methods$/ }, (args) => ({ + path: `${templateDir}/shims/node-fs.ts`, + })); }, }; } diff --git a/builder/src/build/build-worker/templates/shims/node-fs.ts b/builder/src/build/build-worker/templates/shims/node-fs.ts new file mode 100644 index 0000000..bc833ee --- /dev/null +++ b/builder/src/build/build-worker/templates/shims/node-fs.ts @@ -0,0 +1,73 @@ +// https://github.com/vercel/next.js/blob/canary/packages/next/src/server/lib/node-fs-methods.ts + +export const nodeFs = { + existsSync, + readFile, + readFileSync, + writeFile, + mkdir, + stat, +}; + +const FILES = new Map(); +const MTIME = Date.now(); + +function existsSync(path: string) { + console.log( + "existsSync", + path, + new Error().stack?.split("\n").slice(1).join("\n") + ); + return FILES.has(path); +} + +async function readFile(path: string, options: unknown): Promise { + console.log( + "readFile", + { path, options } + // new Error().stack.split("\n").slice(1).join("\n"), + ); + if (!FILES.has(path)) { + throw new Error(path + "does not exist"); + } + return FILES.get(path); +} + +function readFileSync(path: string, options: unknown) { + console.log( + "readFileSync", + { path, options } + // new Error().stack.split("\n").slice(1).join("\n"), + ); + if (!FILES.has(path)) { + throw new Error(path + "does not exist"); + } + return FILES.get(path); +} + +async function writeFile(file: string, data: unknown) { + console.log( + "writeFile", + { file, data } + // new Error().stack.split("\n").slice(1).join("\n"), + ); + FILES.set(file, data); + return true; +} + +async function mkdir(dir: string) { + console.log( + "mkdir", + dir + //new Error().stack.split("\n").slice(1).join("\n"), + ); +} + +async function stat(file: string) { + console.log( + "stat", + file + // new Error().stack.split("\n").slice(1).join("\n"), + ); + return { mtime: new Date(MTIME) }; +}