Skip to content

Commit

Permalink
add to dynamic require esbuild plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
james-elicx committed Nov 27, 2024
1 parent 1d76983 commit e834348
Showing 1 changed file with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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("<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 e834348

Please sign in to comment.