66 * found in the LICENSE file at https://angular.dev/license
77 */
88
9- import type { BuildOptions , PartialMessage } from 'esbuild' ;
9+ import type { BuildOptions , PartialMessage , Plugin } from 'esbuild' ;
1010import assert from 'node:assert' ;
1111import { createHash } from 'node:crypto' ;
1212import { extname , relative } from 'node:path' ;
@@ -67,27 +67,25 @@ export function createBrowserCodeBundleOptions(
6767 entryPoints,
6868 target,
6969 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- ] ,
8370 } ;
8471
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+
8585 if ( options . plugins ) {
86- buildOptions . plugins ? .push ( ...options . plugins ) ;
86+ buildOptions . plugins . push ( ...options . plugins ) ;
8787 }
8888
89- appendOptionsForExternalPackages ( options , buildOptions ) ;
90-
9189 return buildOptions ;
9290 } ;
9391}
@@ -275,22 +273,21 @@ export function createServerMainCodeBundleOptions(
275273 } ,
276274 entryPoints,
277275 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- ] ,
291276 } ;
292277
293278 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+ ) ;
294291
295292 if ( ! externalPackages ) {
296293 buildOptions . plugins . push ( createRxjsEsmResolutionPlugin ( ) ) ;
@@ -369,8 +366,6 @@ export function createServerMainCodeBundleOptions(
369366 buildOptions . plugins . push ( ...options . plugins ) ;
370367 }
371368
372- appendOptionsForExternalPackages ( options , buildOptions ) ;
373-
374369 return buildOptions ;
375370 } ;
376371}
@@ -418,21 +413,20 @@ export function createSsrEntryCodeBundleOptions(
418413 'server' : ssrEntryNamespace ,
419414 } ,
420415 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- ] ,
433416 } ;
434417
435418 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+ ) ;
436430
437431 if ( ! externalPackages ) {
438432 buildOptions . plugins . push ( createRxjsEsmResolutionPlugin ( ) ) ;
@@ -506,8 +500,6 @@ export function createSsrEntryCodeBundleOptions(
506500 buildOptions . plugins . push ( ...options . plugins ) ;
507501 }
508502
509- appendOptionsForExternalPackages ( options , buildOptions ) ;
510-
511503 return buildOptions ;
512504 } ;
513505}
@@ -589,11 +581,38 @@ function getEsBuildCommonOptions(options: NormalizedApplicationBuildOptions): Bu
589581 conditions . push ( optimizationOptions . scripts ? 'production' : 'development' ) ;
590582 }
591583
584+ const plugins : Plugin [ ] = [
585+ createLoaderImportAttributePlugin ( ) ,
586+ createSourcemapIgnorelistPlugin ( ) ,
587+ ] ;
588+
589+ let packages : BuildOptions [ 'packages' ] = 'bundle' ;
590+ if ( options . externalPackages ) {
591+ // Package files affected by a customized loader should not be implicitly marked as external
592+ if (
593+ options . loaderExtensions ||
594+ options . plugins ||
595+ typeof options . externalPackages === 'object'
596+ ) {
597+ // Plugin must be added after custom plugins to ensure any added loader options are considered
598+ plugins . push (
599+ createExternalPackagesPlugin (
600+ options . externalPackages !== true ? options . externalPackages : undefined ,
601+ ) ,
602+ ) ;
603+
604+ packages = 'bundle' ;
605+ } else {
606+ // Safe to use the packages external option directly
607+ packages = 'external' ;
608+ }
609+ }
610+
592611 return {
593612 absWorkingDir : workspaceRoot ,
594613 format : 'esm' ,
595614 bundle : true ,
596- packages : 'bundle' ,
615+ packages,
597616 assetNames : outputNames . media ,
598617 conditions,
599618 resolveExtensions : [ '.ts' , '.tsx' , '.mjs' , '.js' , '.cjs' ] ,
@@ -626,6 +645,7 @@ function getEsBuildCommonOptions(options: NormalizedApplicationBuildOptions): Bu
626645 } ,
627646 loader : loaderExtensions ,
628647 footer,
648+ plugins,
629649 } ;
630650}
631651
@@ -636,11 +656,10 @@ function getEsBuildCommonPolyfillsOptions(
636656 loadResultCache : LoadResultCache | undefined ,
637657) : BuildOptions | undefined {
638658 const { jit, workspaceRoot, i18nOptions } = options ;
639- const buildOptions : BuildOptions = {
640- ...getEsBuildCommonOptions ( options ) ,
641- splitting : false ,
642- plugins : [ createSourcemapIgnorelistPlugin ( ) ] ,
643- } ;
659+
660+ const buildOptions = getEsBuildCommonOptions ( options ) ;
661+ buildOptions . splitting = false ;
662+ buildOptions . plugins ??= [ ] ;
644663
645664 let polyfills = options . polyfills ? [ ...options . polyfills ] : [ ] ;
646665
@@ -668,14 +687,14 @@ function getEsBuildCommonPolyfillsOptions(
668687 needLocaleDataPlugin = true ;
669688 }
670689 if ( needLocaleDataPlugin ) {
671- buildOptions . plugins ? .push ( createAngularLocaleDataPlugin ( ) ) ;
690+ buildOptions . plugins . push ( createAngularLocaleDataPlugin ( ) ) ;
672691 }
673692
674693 if ( polyfills . length === 0 ) {
675694 return ;
676695 }
677696
678- buildOptions . plugins ? .push (
697+ buildOptions . plugins . push (
679698 createVirtualModulePlugin ( {
680699 namespace,
681700 cache : loadResultCache ,
@@ -736,29 +755,3 @@ function entryFileToWorkspaceRelative(workspaceRoot: string, entryFile: string):
736755 . replace ( / \\ / g, '/' )
737756 ) ;
738757}
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