99import { BuilderContext , BuilderOutput , createBuilder } from '@angular-devkit/architect' ;
1010import * as assert from 'assert' ;
1111import type { Message , OutputFile } from 'esbuild' ;
12- import { promises as fs } from 'fs' ;
12+ import * as fs from 'fs/promises ' ;
1313import * as path from 'path' ;
1414import { NormalizedOptimizationOptions , deleteOutputDir } from '../../utils' ;
1515import { copyAssets } from '../../utils/copy-assets' ;
1616import { assertIsError } from '../../utils/error' ;
1717import { transformSupportedBrowsersToTargets } from '../../utils/esbuild-targets' ;
1818import { FileInfo } from '../../utils/index-file/augment-index-html' ;
1919import { 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' ;
2221import { getSupportedBrowsers } from '../../utils/supported-browsers' ;
23- import { getIndexInputFile , getIndexOutputFile } from '../../utils/webpack-browser-config' ;
24- import { normalizeGlobalStyles } from '../../webpack/utils/helpers' ;
2522import { createCompilerPlugin } from './compiler-plugin' ;
2623import { bundle , logMessages } from './esbuild' ;
2724import { logExperimentalWarnings } from './experimental-warnings' ;
@@ -41,13 +38,16 @@ async function execute(
4138 projectRoot,
4239 workspaceRoot,
4340 entryPoints,
44- entryPointNameLookup,
4541 optimizationOptions,
4642 outputPath,
4743 sourcemapOptions,
4844 tsconfig,
4945 assets,
5046 outputNames,
47+ fileReplacements,
48+ globalStyles,
49+ serviceWorkerOptions,
50+ indexHtmlOptions,
5151 } = normalizedOptions ;
5252
5353 const target = transformSupportedBrowsersToTargets (
@@ -64,12 +64,14 @@ async function execute(
6464 optimizationOptions ,
6565 sourcemapOptions ,
6666 tsconfig ,
67+ fileReplacements ,
6768 target ,
6869 ) ,
6970 // Execute esbuild to bundle the global stylesheets
7071 bundleGlobalStylesheets (
7172 workspaceRoot ,
7273 outputNames ,
74+ globalStyles ,
7375 options ,
7476 optimizationOptions ,
7577 sourcemapOptions ,
@@ -102,7 +104,8 @@ async function execute(
102104 // An entryPoint value indicates an initial file
103105 initialFiles . push ( {
104106 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 ] ,
106109 extension : path . extname ( outputFile . path ) ,
107110 } ) ;
108111 }
@@ -119,16 +122,11 @@ async function execute(
119122 }
120123
121124 // 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 ) {
128126 // Create an index HTML generator that reads from the in-memory output files
129127 const indexHtmlGenerator = new IndexHtmlGenerator ( {
130- indexPath : path . join ( context . workspaceRoot , getIndexInputFile ( options . index ) ) ,
131- entrypoints,
128+ indexPath : indexHtmlOptions . input ,
129+ entrypoints : indexHtmlOptions . insertionOrder ,
132130 sri : options . subresourceIntegrity ,
133131 optimization : optimizationOptions ,
134132 crossOrigin : options . crossOrigin ,
@@ -161,7 +159,7 @@ async function execute(
161159 context . logger . warn ( warning ) ;
162160 }
163161
164- outputFiles . push ( createOutputFileFromText ( getIndexOutputFile ( options . index ) , content ) ) ;
162+ outputFiles . push ( createOutputFileFromText ( indexHtmlOptions . output , content ) ) ;
165163 }
166164
167165 // Copy assets
@@ -176,14 +174,13 @@ async function execute(
176174
177175 // Augment the application with service worker support
178176 // TODO: This should eventually operate on the in-memory files prior to writing the output files
179- if ( options . serviceWorker ) {
177+ if ( serviceWorkerOptions ) {
180178 try {
181- await augmentAppWithServiceWorker (
182- projectRoot ,
179+ await augmentAppWithServiceWorkerEsbuild (
183180 workspaceRoot ,
181+ serviceWorkerOptions ,
184182 outputPath ,
185183 options . baseHref || '/' ,
186- options . ngswConfigPath ,
187184 ) ;
188185 } catch ( error ) {
189186 context . logger . error ( error instanceof Error ? error . message : `${ error } ` ) ;
@@ -215,19 +212,9 @@ async function bundleCode(
215212 optimizationOptions : NormalizedOptimizationOptions ,
216213 sourcemapOptions : SourceMapClass ,
217214 tsconfig : string ,
215+ fileReplacements : Record < string , string > | undefined ,
218216 target : string [ ] ,
219217) {
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-
231218 return bundle ( {
232219 absWorkingDir : workspaceRoot ,
233220 bundle : true ,
@@ -295,6 +282,7 @@ async function bundleCode(
295282async function bundleGlobalStylesheets (
296283 workspaceRoot : string ,
297284 outputNames : { bundles : string ; media : string } ,
285+ globalStyles : { name : string ; files : string [ ] ; initial : boolean } [ ] ,
298286 options : BrowserBuilderOptions ,
299287 optimizationOptions : NormalizedOptimizationOptions ,
300288 sourcemapOptions : SourceMapClass ,
@@ -305,12 +293,7 @@ async function bundleGlobalStylesheets(
305293 const errors : Message [ ] = [ ] ;
306294 const warnings : Message [ ] = [ ] ;
307295
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 ) {
314297 const virtualEntryData = files
315298 . map ( ( file ) => `@import '${ file . replace ( / \\ / g, '/' ) } ';` )
316299 . join ( '\n' ) ;
@@ -321,7 +304,7 @@ async function bundleGlobalStylesheets(
321304 workspaceRoot,
322305 optimization : ! ! optimizationOptions . styles . minify ,
323306 sourcemap : ! ! sourcemapOptions . styles && ( sourcemapOptions . hidden ? 'external' : true ) ,
324- outputNames : noInjectNames . includes ( name ) ? { media : outputNames . media } : outputNames ,
307+ outputNames : initial ? outputNames : { media : outputNames . media } ,
325308 includePaths : options . stylePreprocessorOptions ?. includePaths ,
326309 preserveSymlinks : options . preserveSymlinks ,
327310 externalDependencies : options . externalDependencies ,
@@ -356,7 +339,7 @@ async function bundleGlobalStylesheets(
356339 }
357340 outputFiles . push ( createOutputFileFromText ( sheetPath , sheetContents ) ) ;
358341
359- if ( ! noInjectNames . includes ( name ) ) {
342+ if ( initial ) {
360343 initialFiles . push ( {
361344 file : sheetPath ,
362345 name,
0 commit comments