Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions node-modules-polyfill/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ export function NodeModulesPolyfillPlugin(
args: esbuild.OnLoadArgs,
): Promise<esbuild.OnLoadResult> {
try {
const argsPath = args.path.replace(/^node:/, '')
const isCommonjs = args.namespace.endsWith('commonjs')

const resolved = polyfilledBuiltins.get(
removeEndingSlash(args.path),
removeEndingSlash(argsPath),
)
const contents = await (
await fs.promises.readFile(resolved)
Expand All @@ -62,7 +63,7 @@ export function NodeModulesPolyfillPlugin(
return {
loader: 'js',
contents: commonJsTemplate({
importPath: args.path,
importPath: argsPath,
}),
resolveDir,
}
Expand All @@ -83,12 +84,18 @@ export function NodeModulesPolyfillPlugin(
onLoad({ filter: /.*/, namespace }, loader)
onLoad({ filter: /.*/, namespace: commonjsNamespace }, loader)
const filter = new RegExp(
polyfilledBuiltinsNames.map(escapeStringRegexp).join('|'), // TODO builtins could end with slash, keep in mind in regex
[
...polyfilledBuiltinsNames,
...polyfilledBuiltinsNames.map((n) => `node:${n}`),
]
.map(escapeStringRegexp)
.join('|'), // TODO builtins could end with slash, keep in mind in regex
)
async function resolver(args: OnResolveArgs) {
const argsPath = args.path.replace(/^node:/, '')
const ignoreRequire = args.namespace === commonjsNamespace

if (!polyfilledBuiltins.has(args.path)) {
if (!polyfilledBuiltins.has(argsPath)) {
return
}

Expand All @@ -97,7 +104,7 @@ export function NodeModulesPolyfillPlugin(

return {
namespace: isCommonjs ? commonjsNamespace : namespace,
path: args.path,
path: argsPath,
}
}
onResolve({ filter }, resolver)
Expand Down