9
9
import { BuilderContext , BuilderOutput , createBuilder } from '@angular-devkit/architect' ;
10
10
import * as assert from 'assert' ;
11
11
import type { Message , OutputFile } from 'esbuild' ;
12
- import { promises as fs } from 'fs' ;
12
+ import * as fs from 'fs/promises ' ;
13
13
import * as path from 'path' ;
14
14
import { NormalizedOptimizationOptions , deleteOutputDir } from '../../utils' ;
15
15
import { copyAssets } from '../../utils/copy-assets' ;
16
16
import { assertIsError } from '../../utils/error' ;
17
17
import { transformSupportedBrowsersToTargets } from '../../utils/esbuild-targets' ;
18
18
import { FileInfo } from '../../utils/index-file/augment-index-html' ;
19
19
import { IndexHtmlGenerator } from '../../utils/index-file/index-html-generator' ;
20
- import { generateEntryPoints } from '../../utils/package-chunk-sort' ;
21
- import { augmentAppWithServiceWorker } from '../../utils/service-worker' ;
20
+ import { augmentAppWithServiceWorkerEsbuild } from '../../utils/service-worker' ;
22
21
import { getSupportedBrowsers } from '../../utils/supported-browsers' ;
23
- import { getIndexInputFile , getIndexOutputFile } from '../../utils/webpack-browser-config' ;
24
- import { normalizeGlobalStyles } from '../../webpack/utils/helpers' ;
25
22
import { createCompilerPlugin } from './compiler-plugin' ;
26
23
import { bundle , logMessages } from './esbuild' ;
27
24
import { logExperimentalWarnings } from './experimental-warnings' ;
@@ -41,13 +38,16 @@ async function execute(
41
38
projectRoot,
42
39
workspaceRoot,
43
40
entryPoints,
44
- entryPointNameLookup,
45
41
optimizationOptions,
46
42
outputPath,
47
43
sourcemapOptions,
48
44
tsconfig,
49
45
assets,
50
46
outputNames,
47
+ fileReplacements,
48
+ globalStyles,
49
+ serviceWorkerOptions,
50
+ indexHtmlOptions,
51
51
} = normalizedOptions ;
52
52
53
53
const target = transformSupportedBrowsersToTargets (
@@ -64,12 +64,14 @@ async function execute(
64
64
optimizationOptions ,
65
65
sourcemapOptions ,
66
66
tsconfig ,
67
+ fileReplacements ,
67
68
target ,
68
69
) ,
69
70
// Execute esbuild to bundle the global stylesheets
70
71
bundleGlobalStylesheets (
71
72
workspaceRoot ,
72
73
outputNames ,
74
+ globalStyles ,
73
75
options ,
74
76
optimizationOptions ,
75
77
sourcemapOptions ,
@@ -102,7 +104,8 @@ async function execute(
102
104
// An entryPoint value indicates an initial file
103
105
initialFiles . push ( {
104
106
file : outputFile . path ,
105
- name : entryPointNameLookup . get ( entryPoint ) ?? '' ,
107
+ // The first part of the filename is the name of file (e.g., "polyfills" for "polyfills.7S5G3MDY.js")
108
+ name : path . basename ( outputFile . path ) . split ( '.' ) [ 0 ] ,
106
109
extension : path . extname ( outputFile . path ) ,
107
110
} ) ;
108
111
}
@@ -119,16 +122,11 @@ async function execute(
119
122
}
120
123
121
124
// Generate index HTML file
122
- if ( options . index ) {
123
- const entrypoints = generateEntryPoints ( {
124
- scripts : options . scripts ?? [ ] ,
125
- styles : options . styles ?? [ ] ,
126
- } ) ;
127
-
125
+ if ( indexHtmlOptions ) {
128
126
// Create an index HTML generator that reads from the in-memory output files
129
127
const indexHtmlGenerator = new IndexHtmlGenerator ( {
130
- indexPath : path . join ( context . workspaceRoot , getIndexInputFile ( options . index ) ) ,
131
- entrypoints,
128
+ indexPath : indexHtmlOptions . input ,
129
+ entrypoints : indexHtmlOptions . insertionOrder ,
132
130
sri : options . subresourceIntegrity ,
133
131
optimization : optimizationOptions ,
134
132
crossOrigin : options . crossOrigin ,
@@ -161,7 +159,7 @@ async function execute(
161
159
context . logger . warn ( warning ) ;
162
160
}
163
161
164
- outputFiles . push ( createOutputFileFromText ( getIndexOutputFile ( options . index ) , content ) ) ;
162
+ outputFiles . push ( createOutputFileFromText ( indexHtmlOptions . output , content ) ) ;
165
163
}
166
164
167
165
// Copy assets
@@ -176,14 +174,13 @@ async function execute(
176
174
177
175
// Augment the application with service worker support
178
176
// TODO: This should eventually operate on the in-memory files prior to writing the output files
179
- if ( options . serviceWorker ) {
177
+ if ( serviceWorkerOptions ) {
180
178
try {
181
- await augmentAppWithServiceWorker (
182
- projectRoot ,
179
+ await augmentAppWithServiceWorkerEsbuild (
183
180
workspaceRoot ,
181
+ serviceWorkerOptions ,
184
182
outputPath ,
185
183
options . baseHref || '/' ,
186
- options . ngswConfigPath ,
187
184
) ;
188
185
} catch ( error ) {
189
186
context . logger . error ( error instanceof Error ? error . message : `${ error } ` ) ;
@@ -215,19 +212,9 @@ async function bundleCode(
215
212
optimizationOptions : NormalizedOptimizationOptions ,
216
213
sourcemapOptions : SourceMapClass ,
217
214
tsconfig : string ,
215
+ fileReplacements : Record < string , string > | undefined ,
218
216
target : string [ ] ,
219
217
) {
220
- let fileReplacements : Record < string , string > | undefined ;
221
- if ( options . fileReplacements ) {
222
- for ( const replacement of options . fileReplacements ) {
223
- fileReplacements ??= { } ;
224
- fileReplacements [ path . join ( workspaceRoot , replacement . replace ) ] = path . join (
225
- workspaceRoot ,
226
- replacement . with ,
227
- ) ;
228
- }
229
- }
230
-
231
218
return bundle ( {
232
219
absWorkingDir : workspaceRoot ,
233
220
bundle : true ,
@@ -295,6 +282,7 @@ async function bundleCode(
295
282
async function bundleGlobalStylesheets (
296
283
workspaceRoot : string ,
297
284
outputNames : { bundles : string ; media : string } ,
285
+ globalStyles : { name : string ; files : string [ ] ; initial : boolean } [ ] ,
298
286
options : BrowserBuilderOptions ,
299
287
optimizationOptions : NormalizedOptimizationOptions ,
300
288
sourcemapOptions : SourceMapClass ,
@@ -305,12 +293,7 @@ async function bundleGlobalStylesheets(
305
293
const errors : Message [ ] = [ ] ;
306
294
const warnings : Message [ ] = [ ] ;
307
295
308
- // resolveGlobalStyles is temporarily reused from the Webpack builder code
309
- const { entryPoints : stylesheetEntrypoints , noInjectNames } = normalizeGlobalStyles (
310
- options . styles || [ ] ,
311
- ) ;
312
-
313
- for ( const [ name , files ] of Object . entries ( stylesheetEntrypoints ) ) {
296
+ for ( const { name, files, initial } of globalStyles ) {
314
297
const virtualEntryData = files
315
298
. map ( ( file ) => `@import '${ file . replace ( / \\ / g, '/' ) } ';` )
316
299
. join ( '\n' ) ;
@@ -321,7 +304,7 @@ async function bundleGlobalStylesheets(
321
304
workspaceRoot,
322
305
optimization : ! ! optimizationOptions . styles . minify ,
323
306
sourcemap : ! ! sourcemapOptions . styles && ( sourcemapOptions . hidden ? 'external' : true ) ,
324
- outputNames : noInjectNames . includes ( name ) ? { media : outputNames . media } : outputNames ,
307
+ outputNames : initial ? outputNames : { media : outputNames . media } ,
325
308
includePaths : options . stylePreprocessorOptions ?. includePaths ,
326
309
preserveSymlinks : options . preserveSymlinks ,
327
310
externalDependencies : options . externalDependencies ,
@@ -356,7 +339,7 @@ async function bundleGlobalStylesheets(
356
339
}
357
340
outputFiles . push ( createOutputFileFromText ( sheetPath , sheetContents ) ) ;
358
341
359
- if ( ! noInjectNames . includes ( name ) ) {
342
+ if ( initial ) {
360
343
initialFiles . push ( {
361
344
file : sheetPath ,
362
345
name,
0 commit comments