66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import type { BuildOptions } from 'esbuild' ;
109import MagicString , { Bundle } from 'magic-string' ;
1110import assert from 'node:assert' ;
1211import { readFile } from 'node:fs/promises' ;
1312import path from 'node:path' ;
1413import type { NormalizedApplicationBuildOptions } from '../../builders/application/options' ;
1514import { assertIsError } from '../../utils/error' ;
16- import { LoadResultCache , createCachedLoad } from './load-result-cache' ;
15+ import { BundlerOptionsFactory } from './bundler-context' ;
16+ import { createCachedLoad } from './load-result-cache' ;
1717import { createSourcemapIgnorelistPlugin } from './sourcemap-ignorelist-plugin' ;
1818import { createVirtualModulePlugin } from './virtual-module-plugin' ;
1919
@@ -26,8 +26,7 @@ import { createVirtualModulePlugin } from './virtual-module-plugin';
2626export function createGlobalScriptsBundleOptions (
2727 options : NormalizedApplicationBuildOptions ,
2828 initial : boolean ,
29- loadCache ?: LoadResultCache ,
30- ) : BuildOptions | undefined {
29+ ) : BundlerOptionsFactory | undefined {
3130 const {
3231 globalScripts,
3332 optimizationOptions,
@@ -52,83 +51,86 @@ export function createGlobalScriptsBundleOptions(
5251 return ;
5352 }
5453
55- return {
56- absWorkingDir : workspaceRoot ,
57- bundle : false ,
58- splitting : false ,
59- entryPoints,
60- entryNames : initial ? outputNames . bundles : '[name]' ,
61- assetNames : outputNames . media ,
62- mainFields : [ 'script' , 'browser' , 'main' ] ,
63- conditions : [ 'script' ] ,
64- resolveExtensions : [ '.mjs' , '.js' ] ,
65- logLevel : options . verbose ? 'debug' : 'silent' ,
66- metafile : true ,
67- minify : optimizationOptions . scripts ,
68- outdir : workspaceRoot ,
69- sourcemap : sourcemapOptions . scripts && ( sourcemapOptions . hidden ? 'external' : true ) ,
70- write : false ,
71- platform : 'neutral' ,
72- preserveSymlinks,
73- plugins : [
74- createSourcemapIgnorelistPlugin ( ) ,
75- createVirtualModulePlugin ( {
76- namespace,
77- external : true ,
78- // Add the `js` extension here so that esbuild generates an output file with the extension
79- transformPath : ( path ) => path . slice ( namespace . length + 1 ) + '.js' ,
80- loadContent : ( args , build ) =>
81- createCachedLoad ( loadCache , async ( args ) => {
82- const files = globalScripts . find ( ( { name } ) => name === args . path . slice ( 0 , - 3 ) ) ?. files ;
83- assert ( files , `Invalid operation: global scripts name not found [${ args . path } ]` ) ;
54+ return ( loadCache ) => {
55+ return {
56+ absWorkingDir : workspaceRoot ,
57+ bundle : false ,
58+ splitting : false ,
59+ entryPoints,
60+ entryNames : initial ? outputNames . bundles : '[name]' ,
61+ assetNames : outputNames . media ,
62+ mainFields : [ 'script' , 'browser' , 'main' ] ,
63+ conditions : [ 'script' ] ,
64+ resolveExtensions : [ '.mjs' , '.js' ] ,
65+ logLevel : options . verbose ? 'debug' : 'silent' ,
66+ metafile : true ,
67+ minify : optimizationOptions . scripts ,
68+ outdir : workspaceRoot ,
69+ sourcemap : sourcemapOptions . scripts && ( sourcemapOptions . hidden ? 'external' : true ) ,
70+ write : false ,
71+ platform : 'neutral' ,
72+ preserveSymlinks,
73+ plugins : [
74+ createSourcemapIgnorelistPlugin ( ) ,
75+ createVirtualModulePlugin ( {
76+ namespace,
77+ external : true ,
78+ // Add the `js` extension here so that esbuild generates an output file with the extension
79+ transformPath : ( path ) => path . slice ( namespace . length + 1 ) + '.js' ,
80+ loadContent : ( args , build ) =>
81+ createCachedLoad ( loadCache , async ( args ) => {
82+ const files = globalScripts . find ( ( { name } ) => name === args . path . slice ( 0 , - 3 ) )
83+ ?. files ;
84+ assert ( files , `Invalid operation: global scripts name not found [${ args . path } ]` ) ;
8485
85- // Global scripts are concatenated using magic-string instead of bundled via esbuild.
86- const bundleContent = new Bundle ( ) ;
87- const watchFiles = [ ] ;
88- for ( const filename of files ) {
89- let fileContent ;
90- try {
91- // Attempt to read as a relative path from the workspace root
92- const fullPath = path . join ( workspaceRoot , filename ) ;
93- fileContent = await readFile ( fullPath , 'utf-8' ) ;
94- watchFiles . push ( fullPath ) ;
95- } catch ( e ) {
96- assertIsError ( e ) ;
97- if ( e . code !== 'ENOENT' ) {
98- throw e ;
99- }
86+ // Global scripts are concatenated using magic-string instead of bundled via esbuild.
87+ const bundleContent = new Bundle ( ) ;
88+ const watchFiles = [ ] ;
89+ for ( const filename of files ) {
90+ let fileContent ;
91+ try {
92+ // Attempt to read as a relative path from the workspace root
93+ const fullPath = path . join ( workspaceRoot , filename ) ;
94+ fileContent = await readFile ( fullPath , 'utf-8' ) ;
95+ watchFiles . push ( fullPath ) ;
96+ } catch ( e ) {
97+ assertIsError ( e ) ;
98+ if ( e . code !== 'ENOENT' ) {
99+ throw e ;
100+ }
100101
101- // If not found, attempt to resolve as a module specifier
102- const resolveResult = await build . resolve ( filename , {
103- kind : 'entry-point' ,
104- resolveDir : workspaceRoot ,
105- } ) ;
102+ // If not found, attempt to resolve as a module specifier
103+ const resolveResult = await build . resolve ( filename , {
104+ kind : 'entry-point' ,
105+ resolveDir : workspaceRoot ,
106+ } ) ;
106107
107- if ( resolveResult . errors . length ) {
108- // Remove resolution failure notes about marking as external since it doesn't apply
109- // to global scripts.
110- resolveResult . errors . forEach ( ( error ) => ( error . notes = [ ] ) ) ;
108+ if ( resolveResult . errors . length ) {
109+ // Remove resolution failure notes about marking as external since it doesn't apply
110+ // to global scripts.
111+ resolveResult . errors . forEach ( ( error ) => ( error . notes = [ ] ) ) ;
111112
112- return {
113- errors : resolveResult . errors ,
114- warnings : resolveResult . warnings ,
115- } ;
113+ return {
114+ errors : resolveResult . errors ,
115+ warnings : resolveResult . warnings ,
116+ } ;
117+ }
118+
119+ watchFiles . push ( resolveResult . path ) ;
120+ fileContent = await readFile ( resolveResult . path , 'utf-8' ) ;
116121 }
117122
118- watchFiles . push ( resolveResult . path ) ;
119- fileContent = await readFile ( resolveResult . path , 'utf-8' ) ;
123+ bundleContent . addSource ( new MagicString ( fileContent , { filename } ) ) ;
120124 }
121125
122- bundleContent . addSource ( new MagicString ( fileContent , { filename } ) ) ;
123- }
124-
125- return {
126- contents : bundleContent . toString ( ) ,
127- loader : 'js' ,
128- watchFiles,
129- } ;
130- } ) . call ( build , args ) ,
131- } ) ,
132- ] ,
126+ return {
127+ contents : bundleContent . toString ( ) ,
128+ loader : 'js' ,
129+ watchFiles,
130+ } ;
131+ } ) . call ( build , args ) ,
132+ } ) ,
133+ ] ,
134+ } ;
133135 } ;
134136}
0 commit comments