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