Skip to content

Commit

Permalink
fix built-in node modules static imports (#268)
Browse files Browse the repository at this point in the history
* allow any node built-in module to run correctly

* add changeset
  • Loading branch information
dario-piotrowicz authored May 22, 2023
1 parent fd51777 commit 81bfcdb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
16 changes: 16 additions & 0 deletions .changeset/lucky-toys-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@cloudflare/next-on-pages': patch
---

allow any node built-in module to be statically imported correctly

currently only static imports from "node:buffer" work correctly, other
imports, although supported by the workers runtime, aren't handled correctly
(such as "node:events" and "node:util"), fix this by making sure we handle
imports from any of the node built-in modules

> **Note**
> some node built-in modules supported by the workers runtime still cannot be
> correctly imported (like "node:path" for example), but this is because they
> seem to be not allowed by vercel/next itself (so it's something unrelated to
> next-on-pages)
37 changes: 21 additions & 16 deletions src/buildApplication/generateFunctionsMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ async function processFuncDirectory(
bundle: true,
external: ['node:*', `${relativeChunksPath}/*`, '*.wasm'],
minify: true,
plugins: [nodeBufferPlugin],
plugins: [nodeBuiltInModulesPlugin],
});
const formattedPathName = formatRoutePath(relativePath);
const normalizedFilePath = normalizePath(newFilePath);
Expand Down Expand Up @@ -478,7 +478,7 @@ async function buildWebpackChunkFiles(
bundle: true,
external: ['node:*'],
minify: true,
plugins: [nodeBufferPlugin],
plugins: [nodeBuiltInModulesPlugin],
});
const fileContents = await readFile(chunkFilePath, 'utf8');
const wasmChunkImports = Array.from(wasmIdentifiers.entries())
Expand Down Expand Up @@ -652,29 +652,34 @@ function getFunctionNestingLevel(functionPath: string): number {
return nestingLevel;
}

// Chunks can contain `require("node:buffer")`, this is not allowed and breaks at runtime
// the following fixes this by updating the require to a standard esm import from node:buffer
export const nodeBufferPlugin: Plugin = {
name: 'node:buffer',
// Chunks can contain `require("node:*")`, this is not allowed and breaks at runtime
// the following fixes this by updating the require to a standard esm import from "node:*"
export const nodeBuiltInModulesPlugin: Plugin = {
name: 'node:built-in:modules',
setup(build) {
build.onResolve({ filter: /^node:buffer$/ }, ({ kind, path }) => {
// this plugin converts `require("node:buffer")` calls, those are the only ones that
// need updating (esm imports to "node:buffer" are totally valid), so here we tag with the
build.onResolve({ filter: /^node:/ }, ({ kind, path }) => {
// this plugin converts `require("node:*")` calls, those are the only ones that
// need updating (esm imports to "node:*" are totally valid), so here we tag with the
// node-buffer namespace only imports that are require calls
return kind === 'require-call'
? {
path,
namespace: 'node-buffer',
namespace: 'node-built-in-modules',
}
: undefined;
});

// we convert the imports we tagged with the node-buffer namespace so that instead of `require("node:buffer")`
// they import from `export * from 'node:buffer;'`
build.onLoad({ filter: /.*/, namespace: 'node-buffer' }, () => ({
contents: `export * from 'node:buffer'`,
loader: 'js',
}));
// we convert the imports we tagged with the node-built-in-modules namespace so that instead of `require("node:*")`
// they import from `export * from "node:*";`
build.onLoad(
{ filter: /.*/, namespace: 'node-built-in-modules' },
({ path }) => {
return {
contents: `export * from '${path}'`,
loader: 'js',
};
}
);
},
};

Expand Down

0 comments on commit 81bfcdb

Please sign in to comment.