@@ -108,6 +108,9 @@ async function execute(
108
108
const target = transformSupportedBrowsersToTargets ( browsers ) ;
109
109
110
110
// Reuse rebuild state or create new bundle contexts for code and global stylesheets
111
+ const bundlerContexts = [ ] ;
112
+
113
+ // Application code
111
114
const codeBundleCache = options . watch
112
115
? rebuildState ?. codeBundleCache ?? new SourceFileCache ( )
113
116
: undefined ;
@@ -118,37 +121,38 @@ async function execute(
118
121
! ! options . watch ,
119
122
createCodeBundleOptions ( options , target , browsers , codeBundleCache ) ,
120
123
) ;
121
- const globalStylesBundleContext =
122
- rebuildState ?. globalStylesRebuild ??
123
- new BundlerContext (
124
+ bundlerContexts . push ( codeBundleContext ) ;
125
+ // Global Stylesheets
126
+ let globalStylesBundleContext ;
127
+ if ( options . globalStyles . length > 0 ) {
128
+ globalStylesBundleContext =
129
+ rebuildState ?. globalStylesRebuild ??
130
+ new BundlerContext (
131
+ workspaceRoot ,
132
+ ! ! options . watch ,
133
+ createGlobalStylesBundleOptions (
134
+ options ,
135
+ target ,
136
+ browsers ,
137
+ codeBundleCache ?. loadResultCache ,
138
+ ) ,
139
+ ) ;
140
+ bundlerContexts . push ( globalStylesBundleContext ) ;
141
+ }
142
+ // Global Scripts
143
+ if ( options . globalScripts . length > 0 ) {
144
+ const globalScriptsBundleContext = new BundlerContext (
124
145
workspaceRoot ,
125
146
! ! options . watch ,
126
- createGlobalStylesBundleOptions ( options , target , browsers , codeBundleCache ?. loadResultCache ) ,
147
+ createGlobalScriptsBundleOptions ( options ) ,
127
148
) ;
149
+ bundlerContexts . push ( globalScriptsBundleContext ) ;
150
+ }
128
151
129
- const globalScriptsBundleContext = new BundlerContext (
130
- workspaceRoot ,
131
- ! ! options . watch ,
132
- createGlobalScriptsBundleOptions ( options ) ,
133
- ) ;
134
-
135
- const [ codeResults , styleResults , scriptResults ] = await Promise . all ( [
136
- // Execute esbuild to bundle the application code
137
- codeBundleContext . bundle ( ) ,
138
- // Execute esbuild to bundle the global stylesheets
139
- globalStylesBundleContext . bundle ( ) ,
140
- globalScriptsBundleContext . bundle ( ) ,
141
- ] ) ;
152
+ const bundlingResult = await BundlerContext . bundleAll ( bundlerContexts ) ;
142
153
143
154
// Log all warnings and errors generated during bundling
144
- await logMessages ( context , {
145
- errors : [
146
- ...( codeResults . errors || [ ] ) ,
147
- ...( styleResults . errors || [ ] ) ,
148
- ...( scriptResults . errors || [ ] ) ,
149
- ] ,
150
- warnings : [ ...codeResults . warnings , ...styleResults . warnings , ...scriptResults . warnings ] ,
151
- } ) ;
155
+ await logMessages ( context , bundlingResult ) ;
152
156
153
157
const executionResult = new ExecutionResult (
154
158
codeBundleContext ,
@@ -157,40 +161,22 @@ async function execute(
157
161
) ;
158
162
159
163
// Return if the bundling has errors
160
- if ( codeResults . errors || styleResults . errors || scriptResults . errors ) {
164
+ if ( bundlingResult . errors ) {
161
165
return executionResult ;
162
166
}
163
167
164
- // Filter global stylesheet initial files
165
- styleResults . initialFiles = styleResults . initialFiles . filter (
166
- ( { name } ) => options . globalStyles . find ( ( style ) => style . name === name ) ?. initial ,
167
- ) ;
168
+ // Filter global stylesheet initial files. Currently all initial CSS files are from the global styles option.
169
+ if ( options . globalScripts . length > 0 ) {
170
+ bundlingResult . initialFiles = bundlingResult . initialFiles . filter (
171
+ ( { file, name } ) =>
172
+ ! file . endsWith ( '.css' ) ||
173
+ options . globalStyles . find ( ( style ) => style . name === name ) ?. initial ,
174
+ ) ;
175
+ }
168
176
169
- // Combine the bundling output files
170
- const initialFiles : FileInfo [ ] = [
171
- ...codeResults . initialFiles ,
172
- ...styleResults . initialFiles ,
173
- ...scriptResults . initialFiles ,
174
- ] ;
175
- executionResult . outputFiles . push (
176
- ...codeResults . outputFiles ,
177
- ...styleResults . outputFiles ,
178
- ...scriptResults . outputFiles ,
179
- ) ;
177
+ const { metafile, initialFiles, outputFiles } = bundlingResult ;
180
178
181
- // Combine metafiles used for the stats option as well as bundle budgets and console output
182
- const metafile = {
183
- inputs : {
184
- ...codeResults . metafile ?. inputs ,
185
- ...styleResults . metafile ?. inputs ,
186
- ...scriptResults . metafile ?. inputs ,
187
- } ,
188
- outputs : {
189
- ...codeResults . metafile ?. outputs ,
190
- ...styleResults . metafile ?. outputs ,
191
- ...scriptResults . metafile ?. outputs ,
192
- } ,
193
- } ;
179
+ executionResult . outputFiles . push ( ...outputFiles ) ;
194
180
195
181
// Check metafile for CommonJS module usage if optimizing scripts
196
182
if ( optimizationOptions . scripts ) {
0 commit comments