Skip to content

Commit

Permalink
Transforms for the nextjs-commerce app
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Sep 13, 2024
1 parent 097e186 commit 61db398
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
23 changes: 23 additions & 0 deletions builder/src/build/build-worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
`,
},
});
Expand Down Expand Up @@ -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`,
}));
},
};
}
73 changes: 73 additions & 0 deletions builder/src/build/build-worker/templates/shims/node-fs.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>();
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<any> {
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) };
}

0 comments on commit 61db398

Please sign in to comment.