Skip to content

Commit 61db398

Browse files
committed
Transforms for the nextjs-commerce app
1 parent 097e186 commit 61db398

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

builder/src/build/build-worker/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,26 @@ export async function buildWorker(
9090
*/ ""
9191
}
9292
globalThis.__dirname ??= "";
93+
94+
// Do not crash on cache not supported
95+
let isPatchedAlready = globalThis.fetch.__nextPatched;
96+
const curFetch = globalThis.fetch;
97+
globalThis.fetch = (input, init) => {
98+
console.log("globalThis.fetch", input);
99+
if (init) delete init.cache;
100+
return curFetch(input, init);
101+
};
102+
globalThis.fetch.__nextPatched = isPatchedAlready;
103+
fetch = globalThis.fetch;
104+
const CustomRequest = class extends globalThis.Request {
105+
constructor(input, init) {
106+
console.log("CustomRequest", input);
107+
if (init) delete init.cache;
108+
super(input, init);
109+
}
110+
};
111+
globalThis.Request = CustomRequest;
112+
Request = globalThis.Request;
93113
`,
94114
},
95115
});
@@ -185,6 +205,9 @@ function createFixRequiresESBuildPlugin(templateDir: string): Plugin {
185205
build.onResolve({ filter: /^\.\/require-hook$/ }, (args) => ({
186206
path: `${templateDir}/shims/empty.ts`,
187207
}));
208+
build.onResolve({ filter: /\.\/lib\/node-fs-methods$/ }, (args) => ({
209+
path: `${templateDir}/shims/node-fs.ts`,
210+
}));
188211
},
189212
};
190213
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// https://github.com/vercel/next.js/blob/canary/packages/next/src/server/lib/node-fs-methods.ts
2+
3+
export const nodeFs = {
4+
existsSync,
5+
readFile,
6+
readFileSync,
7+
writeFile,
8+
mkdir,
9+
stat,
10+
};
11+
12+
const FILES = new Map<string, unknown>();
13+
const MTIME = Date.now();
14+
15+
function existsSync(path: string) {
16+
console.log(
17+
"existsSync",
18+
path,
19+
new Error().stack?.split("\n").slice(1).join("\n")
20+
);
21+
return FILES.has(path);
22+
}
23+
24+
async function readFile(path: string, options: unknown): Promise<any> {
25+
console.log(
26+
"readFile",
27+
{ path, options }
28+
// new Error().stack.split("\n").slice(1).join("\n"),
29+
);
30+
if (!FILES.has(path)) {
31+
throw new Error(path + "does not exist");
32+
}
33+
return FILES.get(path);
34+
}
35+
36+
function readFileSync(path: string, options: unknown) {
37+
console.log(
38+
"readFileSync",
39+
{ path, options }
40+
// new Error().stack.split("\n").slice(1).join("\n"),
41+
);
42+
if (!FILES.has(path)) {
43+
throw new Error(path + "does not exist");
44+
}
45+
return FILES.get(path);
46+
}
47+
48+
async function writeFile(file: string, data: unknown) {
49+
console.log(
50+
"writeFile",
51+
{ file, data }
52+
// new Error().stack.split("\n").slice(1).join("\n"),
53+
);
54+
FILES.set(file, data);
55+
return true;
56+
}
57+
58+
async function mkdir(dir: string) {
59+
console.log(
60+
"mkdir",
61+
dir
62+
//new Error().stack.split("\n").slice(1).join("\n"),
63+
);
64+
}
65+
66+
async function stat(file: string) {
67+
console.log(
68+
"stat",
69+
file
70+
// new Error().stack.split("\n").slice(1).join("\n"),
71+
);
72+
return { mtime: new Date(MTIME) };
73+
}

0 commit comments

Comments
 (0)