From 1d76983df599cb99aabf3ac85dc3778426a32782 Mon Sep 17 00:00:00 2001 From: James Date: Wed, 27 Nov 2024 19:59:55 +0000 Subject: [PATCH 1/2] fix: mark `async_hooks` as external --- .changeset/stupid-dragons-brake.md | 5 +++++ .../next-on-pages/src/buildApplication/buildWorkerFile.ts | 7 ++++++- .../src/buildApplication/processVercelFunctions/build.ts | 8 +++++++- 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 .changeset/stupid-dragons-brake.md diff --git a/.changeset/stupid-dragons-brake.md b/.changeset/stupid-dragons-brake.md new file mode 100644 index 000000000..36386ee73 --- /dev/null +++ b/.changeset/stupid-dragons-brake.md @@ -0,0 +1,5 @@ +--- +'@cloudflare/next-on-pages': patch +--- + +Mark `async_hooks` as external. diff --git a/packages/next-on-pages/src/buildApplication/buildWorkerFile.ts b/packages/next-on-pages/src/buildApplication/buildWorkerFile.ts index 2d01843cd..a5559d951 100644 --- a/packages/next-on-pages/src/buildApplication/buildWorkerFile.ts +++ b/packages/next-on-pages/src/buildApplication/buildWorkerFile.ts @@ -81,7 +81,12 @@ export async function buildWorkerFile( banner: { js: generateGlobalJs() }, bundle: true, inject: [functionsFile], - external: ['node:*', './__next-on-pages-dist__/*', 'cloudflare:*'], + external: [ + 'node:*', + 'async_hooks', + './__next-on-pages-dist__/*', + 'cloudflare:*', + ], define: { __CONFIG__: JSON.stringify(vercelConfig), __NODE_ENV__: JSON.stringify(getNodeEnv()), diff --git a/packages/next-on-pages/src/buildApplication/processVercelFunctions/build.ts b/packages/next-on-pages/src/buildApplication/processVercelFunctions/build.ts index c920ae390..4c79cd4ef 100644 --- a/packages/next-on-pages/src/buildApplication/processVercelFunctions/build.ts +++ b/packages/next-on-pages/src/buildApplication/processVercelFunctions/build.ts @@ -36,7 +36,13 @@ export async function buildFile( platform: 'neutral', outfile: filePath, bundle: true, - external: ['node:*', `${relativeNopDistPath}/*`, '*.wasm', 'cloudflare:*'], + external: [ + 'node:*', + 'async_hooks', + `${relativeNopDistPath}/*`, + '*.wasm', + 'cloudflare:*', + ], minify: true, plugins: [builtInModulesPlugin], define: { From 7c8766c32ea55fd63f3de279181102fa773c44ff Mon Sep 17 00:00:00 2001 From: James Date: Wed, 27 Nov 2024 20:24:49 +0000 Subject: [PATCH 2/2] add to dynamic require esbuild plugin --- .../processVercelFunctions/build.ts | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/next-on-pages/src/buildApplication/processVercelFunctions/build.ts b/packages/next-on-pages/src/buildApplication/processVercelFunctions/build.ts index 4c79cd4ef..d37a4bb20 100644 --- a/packages/next-on-pages/src/buildApplication/processVercelFunctions/build.ts +++ b/packages/next-on-pages/src/buildApplication/processVercelFunctions/build.ts @@ -101,21 +101,24 @@ type RelativePathOpts = { * breaks at runtime. The following fixes this by updating the dynamic require to a standard esm * import from the built-in module. * - * This applies to `require("node:*")` and `require("cloudflare:*")`. + * This applies to `require("node:*")`, `require("cloudflare:*")`, and `require("async_hooks")`. */ export const builtInModulesPlugin: Plugin = { name: 'built-in:modules', setup(build) { - build.onResolve({ filter: /^(node|cloudflare):/ }, ({ kind, path }) => { - /** - * This plugin converts `require(":*")` calls, those are the only ones that need - * updating (esm imports to ":*" are totally valid), so here we tag with the - * built-in-modules namespace only imports that are require calls. - */ - return kind === 'require-call' - ? { path, namespace: 'built-in-modules' } - : undefined; - }); + build.onResolve( + { filter: /^(node:|cloudflare:|async_hooks)/ }, + ({ kind, path }) => { + /** + * This plugin converts `require(":*")` calls, those are the only ones that need + * updating (esm imports to ":*" are totally valid), so here we tag with the + * built-in-modules namespace only imports that are require calls. + */ + return kind === 'require-call' + ? { path, namespace: 'built-in-modules' } + : undefined; + }, + ); /** * We convert the imports we tagged with the built-in-modules namespace so that instead of