Skip to content

Commit

Permalink
fix: mark async_hooks as external (#909)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-elicx authored Nov 28, 2024
1 parent 96c331d commit d879acd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-dragons-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cloudflare/next-on-pages': patch
---

Mark `async_hooks` as external.
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -95,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("<PREFIX>:*")` calls, those are the only ones that need
* updating (esm imports to "<PREFIX>:*" 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("<PREFIX>:*")` calls, those are the only ones that need
* updating (esm imports to "<PREFIX>:*" 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
Expand Down

0 comments on commit d879acd

Please sign in to comment.