6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import type { BuildOptions } from 'esbuild' ;
10
9
import MagicString , { Bundle } from 'magic-string' ;
11
10
import assert from 'node:assert' ;
12
11
import { readFile } from 'node:fs/promises' ;
13
12
import path from 'node:path' ;
14
13
import type { NormalizedApplicationBuildOptions } from '../../builders/application/options' ;
15
14
import { 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' ;
17
17
import { createSourcemapIgnorelistPlugin } from './sourcemap-ignorelist-plugin' ;
18
18
import { createVirtualModulePlugin } from './virtual-module-plugin' ;
19
19
@@ -26,8 +26,7 @@ import { createVirtualModulePlugin } from './virtual-module-plugin';
26
26
export function createGlobalScriptsBundleOptions (
27
27
options : NormalizedApplicationBuildOptions ,
28
28
initial : boolean ,
29
- loadCache ?: LoadResultCache ,
30
- ) : BuildOptions | undefined {
29
+ ) : BundlerOptionsFactory | undefined {
31
30
const {
32
31
globalScripts,
33
32
optimizationOptions,
@@ -52,83 +51,86 @@ export function createGlobalScriptsBundleOptions(
52
51
return ;
53
52
}
54
53
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 } ]` ) ;
84
85
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
+ }
100
101
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
+ } ) ;
106
107
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 = [ ] ) ) ;
111
112
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' ) ;
116
121
}
117
122
118
- watchFiles . push ( resolveResult . path ) ;
119
- fileContent = await readFile ( resolveResult . path , 'utf-8' ) ;
123
+ bundleContent . addSource ( new MagicString ( fileContent , { filename } ) ) ;
120
124
}
121
125
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
+ } ;
133
135
} ;
134
136
}
0 commit comments