Skip to content

Commit 861a1b1

Browse files
committed
fix(@angular/build): alias createRequire banner import to avoid duplicate binding
ESBuild processes banner content as raw text outside of its module graph, so it cannot deduplicate or rename banner imports the way it does for user imports. If user code already imports `createRequire` from `node:module`, the injected banner produces a duplicate binding that causes a runtime error. Alias the banner import to `__ngCreateRequire` to avoid colliding with any `createRequire` binding that esbuild may emit from bundled user code.
1 parent 853130f commit 861a1b1

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

packages/angular/build/src/tools/esbuild/application-code-bundle.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,14 @@ export function createServerPolyfillBundleOptions(
196196
if (isNodePlatform) {
197197
// Note: Needed as esbuild does not provide require shims / proxy from ESModules.
198198
// See: https://github.com/evanw/esbuild/issues/1921.
199+
// Use an alias to avoid colliding with any `createRequire` import that may
200+
// already exist in the bundled user code. ESBuild processes banner content
201+
// as raw text outside of its module graph, so it cannot deduplicate or
202+
// rename banner imports the way it does for user imports. Without the alias,
203+
// a duplicate `import { createRequire }` binding would cause a runtime error.
199204
jsBanner.push(
200-
`import { createRequire } from 'node:module';`,
201-
`globalThis['require'] ??= createRequire(import.meta.url);`,
205+
`import { createRequire as __ngCreateRequire } from 'node:module';`,
206+
`globalThis['require'] ??= __ngCreateRequire(import.meta.url);`,
202207
);
203208
}
204209

@@ -397,9 +402,14 @@ export function createSsrEntryCodeBundleOptions(
397402
if (isNodePlatform) {
398403
// Note: Needed as esbuild does not provide require shims / proxy from ESModules.
399404
// See: https://github.com/evanw/esbuild/issues/1921.
405+
// Use an alias to avoid colliding with any `createRequire` import that may
406+
// already exist in the bundled user code. ESBuild processes banner content
407+
// as raw text outside of its module graph, so it cannot deduplicate or
408+
// rename banner imports the way it does for user imports. Without the alias,
409+
// a duplicate `import { createRequire }` binding would cause a runtime error.
400410
jsBanner.push(
401-
`import { createRequire } from 'node:module';`,
402-
`globalThis['require'] ??= createRequire(import.meta.url);`,
411+
`import { createRequire as __ngCreateRequire } from 'node:module';`,
412+
`globalThis['require'] ??= __ngCreateRequire(import.meta.url);`,
403413
);
404414
}
405415

0 commit comments

Comments
 (0)