Skip to content

Commit 1128bdd

Browse files
committedApr 8, 2024·
fix(@angular-devkit/build-angular): ensure esbuild-based builders exclusively produce ESM output
Previously, there were instances where the ESbuilder might incorrectly generate CJS code, leading to runtime errors. Fixes #27421 (cherry picked from commit afa76bb)
1 parent 1f47a10 commit 1128bdd

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed
 

‎packages/angular_devkit/build_angular/src/tools/esbuild/angular/compiler-plugin.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -463,18 +463,18 @@ function createCompilerOptionsTransformer(
463463
): Parameters<AngularCompilation['initialize']>[2] {
464464
return (compilerOptions) => {
465465
// target of 9 is ES2022 (using the number avoids an expensive import of typescript just for an enum)
466-
if (compilerOptions.target === undefined || compilerOptions.target < 9) {
466+
if (compilerOptions.target === undefined || compilerOptions.target < 9 /** ES2022 */) {
467467
// If 'useDefineForClassFields' is already defined in the users project leave the value as is.
468468
// Otherwise fallback to false due to https://github.com/microsoft/TypeScript/issues/45995
469469
// which breaks the deprecated `@Effects` NGRX decorator and potentially other existing code as well.
470-
compilerOptions.target = 9;
470+
compilerOptions.target = 9 /** ES2022 */;
471471
compilerOptions.useDefineForClassFields ??= false;
472472

473473
// Only add the warning on the initial build
474474
setupWarnings?.push({
475475
text:
476-
'TypeScript compiler options "target" and "useDefineForClassFields" are set to "ES2022" and ' +
477-
'"false" respectively by the Angular CLI.',
476+
`TypeScript compiler options 'target' and 'useDefineForClassFields' are set to 'ES2022' and ` +
477+
`'false' respectively by the Angular CLI.`,
478478
location: { file: pluginOptions.tsconfig },
479479
notes: [
480480
{
@@ -507,6 +507,15 @@ function createCompilerOptionsTransformer(
507507
compilerOptions.incremental = false;
508508
}
509509

510+
if (compilerOptions.module === undefined || compilerOptions.module < 5 /** ES2015 */) {
511+
compilerOptions.module = 7; /** ES2022 */
512+
setupWarnings?.push({
513+
text: `TypeScript compiler options 'module' values 'CommonJS', 'UMD', 'System' and 'AMD' are not supported.`,
514+
location: null,
515+
notes: [{ text: `The 'module' option will be set to 'ES2022' instead.` }],
516+
});
517+
}
518+
510519
return {
511520
...compilerOptions,
512521
noEmitOnError: false,

0 commit comments

Comments
 (0)
Please sign in to comment.