@@ -6,7 +6,7 @@ import { cp, readFile, writeFile } from "node:fs/promises";
6
6
import { globSync } from "glob" ;
7
7
import { resolve } from "node:path" ;
8
8
9
- let fixRequires : Plugin = {
9
+ const fixRequiresESBuildPlugin : Plugin = {
10
10
name : "replaceRelative" ,
11
11
setup ( build ) {
12
12
// Note: we (empty) shim require-hook modules as they generate problematic code that uses requires
@@ -44,7 +44,7 @@ export async function buildWorker(
44
44
format : "esm" ,
45
45
target : "esnext" ,
46
46
minify : false ,
47
- plugins : [ fixRequires ] ,
47
+ plugins : [ fixRequiresESBuildPlugin ] ,
48
48
alias : {
49
49
// Note: we apply an empty shim to next/dist/compiled/ws because it generates two `eval`s:
50
50
// eval("require")("bufferutil");
@@ -125,18 +125,22 @@ async function updateWorkerBundledCode(
125
125
nextjsAppPaths : NextjsAppPaths
126
126
) : Promise < void > {
127
127
console . log ( { workerOutputFile } ) ;
128
- const workerContents = await readFile ( workerOutputFile , "utf8" ) ;
128
+ const originalCode = await readFile ( workerOutputFile , "utf8" ) ;
129
129
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
132
136
. replace ( / _ _ r e q u i r e \d ? \( / g, "require(" )
133
137
. replace ( / _ _ r e q u i r e \d ? \. / g, "require." ) ;
134
138
135
139
// The next-server code gets the buildId from the filesystem, resulting in a `[unenv] fs.readFileSync is not implemented yet!` error
136
140
// so we add an early return to the `getBuildId` function so that the `readyFileSync` is never encountered
137
141
// (source: https://github.com/vercel/next.js/blob/15aeb92efb34c09a36/packages/next/src/server/next-server.ts#L438-L451)
138
142
// Note: we could/should probably just patch readFileSync here or something!
139
- updatedWorkerContents = updatedWorkerContents . replace (
143
+ patchedCode = patchedCode . replace (
140
144
"getBuildId() {" ,
141
145
`getBuildId() {
142
146
return ${ JSON . stringify (
@@ -154,7 +158,7 @@ async function updateWorkerBundledCode(
154
158
const manifestJsons = globSync (
155
159
`${ nextjsAppPaths . standaloneAppDotNextDir } /**/*-manifest.json`
156
160
) . map ( ( file ) => file . replace ( nextjsAppPaths . standaloneAppDir + "/" , "" ) ) ;
157
- updatedWorkerContents = updatedWorkerContents . replace (
161
+ patchedCode = patchedCode . replace (
158
162
/ f u n c t i o n l o a d M a n i f e s t \( ( .+ ?) , .+ ?\) { / ,
159
163
`$&
160
164
${ manifestJsons
@@ -177,7 +181,7 @@ async function updateWorkerBundledCode(
177
181
// VERY IMPORTANT: this required the following dependency to be part of the application!!!! (this is very bad!!!)
178
182
// "node-url": "npm:url@^0.11.4"
179
183
// 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 (
181
185
/ ( [ a - z A - Z 0 - 9 _ ] + ) = r e q u i r e \( " u r l " \) ; / g,
182
186
` $1 = require("url");
183
187
const nodeUrl = require("node-url");
@@ -210,7 +214,7 @@ async function updateWorkerBundledCode(
210
214
const htmlPages = allManifestFiles . filter ( ( file ) => file . endsWith ( ".html" ) ) ;
211
215
const pageModules = allManifestFiles . filter ( ( file ) => file . endsWith ( ".js" ) ) ;
212
216
213
- updatedWorkerContents = updatedWorkerContents . replace (
217
+ patchedCode = patchedCode . replace (
214
218
/ c o n s t p a g e P a t h = g e t P a g e P a t h \( .+ ?\) ; / ,
215
219
`$&
216
220
${ htmlPages
@@ -244,7 +248,7 @@ async function updateWorkerBundledCode(
244
248
// (source: https://github.com/vercel/next.js/blob/ba995993/packages/next/src/lib/find-pages-dir.ts#L4-L13)
245
249
// (usage source: https://github.com/vercel/next.js/blob/ba995993/packages/next/src/server/next-server.ts#L450-L451)
246
250
// 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 (
248
252
"function findDir(dir, name) {" ,
249
253
`function findDir(dir, name) {
250
254
if (dir.endsWith(".next/server")) {
@@ -267,7 +271,7 @@ async function updateWorkerBundledCode(
267
271
const manifestJss = globSync (
268
272
`${ nextjsAppPaths . standaloneAppDotNextDir } /**/*_client-reference-manifest.js`
269
273
) . map ( ( file ) => file . replace ( `${ nextjsAppPaths . standaloneAppDir } /` , "" ) ) ;
270
- updatedWorkerContents = updatedWorkerContents . replace (
274
+ patchedCode = patchedCode . replace (
271
275
/ f u n c t i o n e v a l M a n i f e s t \( ( .+ ?) , .+ ?\) { / ,
272
276
`$&
273
277
${ manifestJss
@@ -295,7 +299,7 @@ async function updateWorkerBundledCode(
295
299
`
296
300
) ;
297
301
298
- await writeFile ( workerOutputFile , updatedWorkerContents ) ;
302
+ await writeFile ( workerOutputFile , patchedCode ) ;
299
303
}
300
304
301
305
/**
0 commit comments