6
6
* found in the LICENSE file at https://angular.dev/license
7
7
*/
8
8
9
- import type { BuildOptions , PartialMessage } from 'esbuild' ;
9
+ import type { BuildOptions , PartialMessage , Plugin } from 'esbuild' ;
10
10
import assert from 'node:assert' ;
11
11
import { createHash } from 'node:crypto' ;
12
12
import { extname , relative } from 'node:path' ;
@@ -67,27 +67,25 @@ export function createBrowserCodeBundleOptions(
67
67
entryPoints,
68
68
target,
69
69
supported : getFeatureSupport ( target , zoneless ) ,
70
- plugins : [
71
- createLoaderImportAttributePlugin ( ) ,
72
- createWasmPlugin ( { allowAsync : zoneless , cache : loadCache } ) ,
73
- createSourcemapIgnorelistPlugin ( ) ,
74
- createAngularLocalizeInitWarningPlugin ( ) ,
75
- createCompilerPlugin (
76
- // JS/TS options
77
- pluginOptions ,
78
- angularCompilation ,
79
- // Component stylesheet bundler
80
- stylesheetBundler ,
81
- ) ,
82
- ] ,
83
70
} ;
84
71
72
+ buildOptions . plugins ??= [ ] ;
73
+ buildOptions . plugins . push (
74
+ createWasmPlugin ( { allowAsync : zoneless , cache : loadCache } ) ,
75
+ createAngularLocalizeInitWarningPlugin ( ) ,
76
+ createCompilerPlugin (
77
+ // JS/TS options
78
+ pluginOptions ,
79
+ angularCompilation ,
80
+ // Component stylesheet bundler
81
+ stylesheetBundler ,
82
+ ) ,
83
+ ) ;
84
+
85
85
if ( options . plugins ) {
86
- buildOptions . plugins ? .push ( ...options . plugins ) ;
86
+ buildOptions . plugins . push ( ...options . plugins ) ;
87
87
}
88
88
89
- appendOptionsForExternalPackages ( options , buildOptions ) ;
90
-
91
89
return buildOptions ;
92
90
} ;
93
91
}
@@ -275,22 +273,21 @@ export function createServerMainCodeBundleOptions(
275
273
} ,
276
274
entryPoints,
277
275
supported : getFeatureSupport ( target , zoneless ) ,
278
- plugins : [
279
- createWasmPlugin ( { allowAsync : zoneless , cache : loadResultCache } ) ,
280
- createSourcemapIgnorelistPlugin ( ) ,
281
- createAngularLocalizeInitWarningPlugin ( ) ,
282
- createCompilerPlugin (
283
- // JS/TS options
284
- pluginOptions ,
285
- // Browser compilation handles the actual Angular code compilation
286
- new NoopCompilation ( ) ,
287
- // Component stylesheet bundler
288
- stylesheetBundler ,
289
- ) ,
290
- ] ,
291
276
} ;
292
277
293
278
buildOptions . plugins ??= [ ] ;
279
+ buildOptions . plugins . push (
280
+ createWasmPlugin ( { allowAsync : zoneless , cache : loadResultCache } ) ,
281
+ createAngularLocalizeInitWarningPlugin ( ) ,
282
+ createCompilerPlugin (
283
+ // JS/TS options
284
+ pluginOptions ,
285
+ // Browser compilation handles the actual Angular code compilation
286
+ new NoopCompilation ( ) ,
287
+ // Component stylesheet bundler
288
+ stylesheetBundler ,
289
+ ) ,
290
+ ) ;
294
291
295
292
if ( ! externalPackages ) {
296
293
buildOptions . plugins . push ( createRxjsEsmResolutionPlugin ( ) ) ;
@@ -369,8 +366,6 @@ export function createServerMainCodeBundleOptions(
369
366
buildOptions . plugins . push ( ...options . plugins ) ;
370
367
}
371
368
372
- appendOptionsForExternalPackages ( options , buildOptions ) ;
373
-
374
369
return buildOptions ;
375
370
} ;
376
371
}
@@ -418,21 +413,20 @@ export function createSsrEntryCodeBundleOptions(
418
413
'server' : ssrEntryNamespace ,
419
414
} ,
420
415
supported : getFeatureSupport ( target , true ) ,
421
- plugins : [
422
- createSourcemapIgnorelistPlugin ( ) ,
423
- createAngularLocalizeInitWarningPlugin ( ) ,
424
- createCompilerPlugin (
425
- // JS/TS options
426
- pluginOptions ,
427
- // Browser compilation handles the actual Angular code compilation
428
- new NoopCompilation ( ) ,
429
- // Component stylesheet bundler
430
- stylesheetBundler ,
431
- ) ,
432
- ] ,
433
416
} ;
434
417
435
418
buildOptions . plugins ??= [ ] ;
419
+ buildOptions . plugins . push (
420
+ createAngularLocalizeInitWarningPlugin ( ) ,
421
+ createCompilerPlugin (
422
+ // JS/TS options
423
+ pluginOptions ,
424
+ // Browser compilation handles the actual Angular code compilation
425
+ new NoopCompilation ( ) ,
426
+ // Component stylesheet bundler
427
+ stylesheetBundler ,
428
+ ) ,
429
+ ) ;
436
430
437
431
if ( ! externalPackages ) {
438
432
buildOptions . plugins . push ( createRxjsEsmResolutionPlugin ( ) ) ;
@@ -506,21 +500,19 @@ export function createSsrEntryCodeBundleOptions(
506
500
buildOptions . plugins . push ( ...options . plugins ) ;
507
501
}
508
502
509
- appendOptionsForExternalPackages ( options , buildOptions ) ;
510
-
511
503
return buildOptions ;
512
504
} ;
513
505
}
514
506
515
507
function getEsBuildServerCommonOptions ( options : NormalizedApplicationBuildOptions ) : BuildOptions {
516
508
const isNodePlatform = options . ssrOptions ?. platform !== ExperimentalPlatform . Neutral ;
517
509
518
- const commonOptons = getEsBuildCommonOptions ( options ) ;
519
- commonOptons . define ??= { } ;
520
- commonOptons . define [ 'ngServerMode' ] = 'true' ;
510
+ const commonOptions = getEsBuildCommonOptions ( options ) ;
511
+ commonOptions . define ??= { } ;
512
+ commonOptions . define [ 'ngServerMode' ] = 'true' ;
521
513
522
514
return {
523
- ...commonOptons ,
515
+ ...commonOptions ,
524
516
platform : isNodePlatform ? 'node' : 'neutral' ,
525
517
outExtension : { '.js' : '.mjs' } ,
526
518
// Note: `es2015` is needed for RxJS v6. If not specified, `module` would
@@ -585,15 +577,41 @@ function getEsBuildCommonOptions(options: NormalizedApplicationBuildOptions): Bu
585
577
conditions . push ( ...customConditions ) ;
586
578
} else {
587
579
// Include default conditions
588
- conditions . push ( 'module' ) ;
589
- conditions . push ( optimizationOptions . scripts ? 'production' : 'development' ) ;
580
+ conditions . push ( 'module' , optimizationOptions . scripts ? 'production' : 'development' ) ;
581
+ }
582
+
583
+ const plugins : Plugin [ ] = [
584
+ createLoaderImportAttributePlugin ( ) ,
585
+ createSourcemapIgnorelistPlugin ( ) ,
586
+ ] ;
587
+
588
+ let packages : BuildOptions [ 'packages' ] = 'bundle' ;
589
+ if ( options . externalPackages ) {
590
+ // Package files affected by a customized loader should not be implicitly marked as external
591
+ if (
592
+ options . loaderExtensions ||
593
+ options . plugins ||
594
+ typeof options . externalPackages === 'object'
595
+ ) {
596
+ // Plugin must be added after custom plugins to ensure any added loader options are considered
597
+ plugins . push (
598
+ createExternalPackagesPlugin (
599
+ options . externalPackages !== true ? options . externalPackages : undefined ,
600
+ ) ,
601
+ ) ;
602
+
603
+ packages = 'bundle' ;
604
+ } else {
605
+ // Safe to use the packages external option directly
606
+ packages = 'external' ;
607
+ }
590
608
}
591
609
592
610
return {
593
611
absWorkingDir : workspaceRoot ,
594
612
format : 'esm' ,
595
613
bundle : true ,
596
- packages : 'bundle' ,
614
+ packages,
597
615
assetNames : outputNames . media ,
598
616
conditions,
599
617
resolveExtensions : [ '.ts' , '.tsx' , '.mjs' , '.js' , '.cjs' ] ,
@@ -626,6 +644,7 @@ function getEsBuildCommonOptions(options: NormalizedApplicationBuildOptions): Bu
626
644
} ,
627
645
loader : loaderExtensions ,
628
646
footer,
647
+ plugins,
629
648
} ;
630
649
}
631
650
@@ -636,11 +655,10 @@ function getEsBuildCommonPolyfillsOptions(
636
655
loadResultCache : LoadResultCache | undefined ,
637
656
) : BuildOptions | undefined {
638
657
const { jit, workspaceRoot, i18nOptions } = options ;
639
- const buildOptions : BuildOptions = {
640
- ...getEsBuildCommonOptions ( options ) ,
641
- splitting : false ,
642
- plugins : [ createSourcemapIgnorelistPlugin ( ) ] ,
643
- } ;
658
+
659
+ const buildOptions = getEsBuildCommonOptions ( options ) ;
660
+ buildOptions . splitting = false ;
661
+ buildOptions . plugins ??= [ ] ;
644
662
645
663
let polyfills = options . polyfills ? [ ...options . polyfills ] : [ ] ;
646
664
@@ -668,14 +686,14 @@ function getEsBuildCommonPolyfillsOptions(
668
686
needLocaleDataPlugin = true ;
669
687
}
670
688
if ( needLocaleDataPlugin ) {
671
- buildOptions . plugins ? .push ( createAngularLocaleDataPlugin ( ) ) ;
689
+ buildOptions . plugins . push ( createAngularLocaleDataPlugin ( ) ) ;
672
690
}
673
691
674
692
if ( polyfills . length === 0 ) {
675
693
return ;
676
694
}
677
695
678
- buildOptions . plugins ? .push (
696
+ buildOptions . plugins . push (
679
697
createVirtualModulePlugin ( {
680
698
namespace,
681
699
cache : loadResultCache ,
@@ -736,29 +754,3 @@ function entryFileToWorkspaceRelative(workspaceRoot: string, entryFile: string):
736
754
. replace ( / \\ / g, '/' )
737
755
) ;
738
756
}
739
-
740
- function appendOptionsForExternalPackages (
741
- options : NormalizedApplicationBuildOptions ,
742
- buildOptions : BuildOptions ,
743
- ) : void {
744
- if ( ! options . externalPackages ) {
745
- return ;
746
- }
747
-
748
- buildOptions . plugins ??= [ ] ;
749
-
750
- // Package files affected by a customized loader should not be implicitly marked as external
751
- if ( options . loaderExtensions || options . plugins || typeof options . externalPackages === 'object' ) {
752
- // Plugin must be added after custom plugins to ensure any added loader options are considered
753
- buildOptions . plugins . push (
754
- createExternalPackagesPlugin (
755
- options . externalPackages !== true ? options . externalPackages : undefined ,
756
- ) ,
757
- ) ;
758
-
759
- buildOptions . packages = undefined ;
760
- } else {
761
- // Safe to use the packages external option directly
762
- buildOptions . packages = 'external' ;
763
- }
764
- }
0 commit comments