Skip to content

Commit 855699a

Browse files
committed
Minor updates
1 parent 64e48f8 commit 855699a

File tree

1 file changed

+16
-12
lines changed
  • nextjs-worker-builder/src/build/build-worker

1 file changed

+16
-12
lines changed

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { cp, readFile, writeFile } from "node:fs/promises";
66
import { globSync } from "glob";
77
import { resolve } from "node:path";
88

9-
let fixRequires: Plugin = {
9+
const fixRequiresESBuildPlugin: Plugin = {
1010
name: "replaceRelative",
1111
setup(build) {
1212
// Note: we (empty) shim require-hook modules as they generate problematic code that uses requires
@@ -44,7 +44,7 @@ export async function buildWorker(
4444
format: "esm",
4545
target: "esnext",
4646
minify: false,
47-
plugins: [fixRequires],
47+
plugins: [fixRequiresESBuildPlugin],
4848
alias: {
4949
// Note: we apply an empty shim to next/dist/compiled/ws because it generates two `eval`s:
5050
// eval("require")("bufferutil");
@@ -125,18 +125,22 @@ async function updateWorkerBundledCode(
125125
nextjsAppPaths: NextjsAppPaths
126126
): Promise<void> {
127127
console.log({ workerOutputFile });
128-
const workerContents = await readFile(workerOutputFile, "utf8");
128+
const originalCode = await readFile(workerOutputFile, "utf8");
129129

130-
// ultra hack (don't remember/know why it's needed)
131-
let updatedWorkerContents = workerContents
130+
let patchedCode = originalCode;
131+
132+
// ESBuild does not support CJS format
133+
// See https://github.com/evanw/esbuild/issues/1921 and linked issues
134+
// Some of the solutions are based on `module.createRequire()` not implemented in workerd.
135+
patchedCode = patchedCode
132136
.replace(/__require\d?\(/g, "require(")
133137
.replace(/__require\d?\./g, "require.");
134138

135139
// The next-server code gets the buildId from the filesystem, resulting in a `[unenv] fs.readFileSync is not implemented yet!` error
136140
// so we add an early return to the `getBuildId` function so that the `readyFileSync` is never encountered
137141
// (source: https://github.com/vercel/next.js/blob/15aeb92efb34c09a36/packages/next/src/server/next-server.ts#L438-L451)
138142
// Note: we could/should probably just patch readFileSync here or something!
139-
updatedWorkerContents = updatedWorkerContents.replace(
143+
patchedCode = patchedCode.replace(
140144
"getBuildId() {",
141145
`getBuildId() {
142146
return ${JSON.stringify(
@@ -154,7 +158,7 @@ async function updateWorkerBundledCode(
154158
const manifestJsons = globSync(
155159
`${nextjsAppPaths.standaloneAppDotNextDir}/**/*-manifest.json`
156160
).map((file) => file.replace(nextjsAppPaths.standaloneAppDir + "/", ""));
157-
updatedWorkerContents = updatedWorkerContents.replace(
161+
patchedCode = patchedCode.replace(
158162
/function loadManifest\((.+?), .+?\) {/,
159163
`$&
160164
${manifestJsons
@@ -177,7 +181,7 @@ async function updateWorkerBundledCode(
177181
// VERY IMPORTANT: this required the following dependency to be part of the application!!!! (this is very bad!!!)
178182
// "node-url": "npm:url@^0.11.4"
179183
// Hopefully this should not be necessary after this unenv PR lands: https://github.com/unjs/unenv/pull/292
180-
updatedWorkerContents = updatedWorkerContents.replace(
184+
patchedCode = patchedCode.replace(
181185
/ ([a-zA-Z0-9_]+) = require\("url"\);/g,
182186
` $1 = require("url");
183187
const nodeUrl = require("node-url");
@@ -210,7 +214,7 @@ async function updateWorkerBundledCode(
210214
const htmlPages = allManifestFiles.filter((file) => file.endsWith(".html"));
211215
const pageModules = allManifestFiles.filter((file) => file.endsWith(".js"));
212216

213-
updatedWorkerContents = updatedWorkerContents.replace(
217+
patchedCode = patchedCode.replace(
214218
/const pagePath = getPagePath\(.+?\);/,
215219
`$&
216220
${htmlPages
@@ -244,7 +248,7 @@ async function updateWorkerBundledCode(
244248
// (source: https://github.com/vercel/next.js/blob/ba995993/packages/next/src/lib/find-pages-dir.ts#L4-L13)
245249
// (usage source: https://github.com/vercel/next.js/blob/ba995993/packages/next/src/server/next-server.ts#L450-L451)
246250
// Note: `findDir` uses `fs.existsSync` under the hood, so patching that should be enough to make this work
247-
updatedWorkerContents = updatedWorkerContents.replace(
251+
patchedCode = patchedCode.replace(
248252
"function findDir(dir, name) {",
249253
`function findDir(dir, name) {
250254
if (dir.endsWith(".next/server")) {
@@ -267,7 +271,7 @@ async function updateWorkerBundledCode(
267271
const manifestJss = globSync(
268272
`${nextjsAppPaths.standaloneAppDotNextDir}/**/*_client-reference-manifest.js`
269273
).map((file) => file.replace(`${nextjsAppPaths.standaloneAppDir}/`, ""));
270-
updatedWorkerContents = updatedWorkerContents.replace(
274+
patchedCode = patchedCode.replace(
271275
/function evalManifest\((.+?), .+?\) {/,
272276
`$&
273277
${manifestJss
@@ -295,7 +299,7 @@ async function updateWorkerBundledCode(
295299
`
296300
);
297301

298-
await writeFile(workerOutputFile, updatedWorkerContents);
302+
await writeFile(workerOutputFile, patchedCode);
299303
}
300304

301305
/**

0 commit comments

Comments
 (0)