5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
- import {
9
- BuilderContext ,
10
- BuilderOutput ,
11
- createBuilder ,
12
- } from '@angular-devkit/architect' ;
8
+ import { BuilderContext , BuilderOutput , createBuilder } from '@angular-devkit/architect' ;
13
9
import {
14
10
BuildResult ,
15
11
EmittedFiles ,
16
12
WebpackLoggingCallback ,
17
13
runWebpack ,
18
- } from '@angular-devkit/build-webpack' ;
14
+ } from '@angular-devkit/build-webpack' ;
19
15
import {
20
16
experimental ,
21
17
getSystemPath ,
@@ -62,9 +58,10 @@ import { assertCompatibleAngularVersion } from '../utils/version';
62
58
import { generateBrowserWebpackConfigFromContext } from '../utils/webpack-browser-config' ;
63
59
import { Schema as BrowserBuilderSchema } from './schema' ;
64
60
65
- export type BrowserBuilderOutput = json . JsonObject & BuilderOutput & {
66
- outputPath : string ;
67
- } ;
61
+ export type BrowserBuilderOutput = json . JsonObject &
62
+ BuilderOutput & {
63
+ outputPath : string ;
64
+ } ;
68
65
69
66
export function createBrowserLoggingCallback (
70
67
verbose : boolean ,
@@ -92,7 +89,7 @@ export async function buildBrowserWebpackConfigFromContext(
92
89
options : BrowserBuilderSchema ,
93
90
context : BuilderContext ,
94
91
host : virtualFs . Host < fs . Stats > = new NodeJsSyncHost ( ) ,
95
- ) : Promise < { workspace : experimental . workspace . Workspace , config : webpack . Configuration [ ] } > {
92
+ ) : Promise < { workspace : experimental . workspace . Workspace ; config : webpack . Configuration [ ] } > {
96
93
return generateBrowserWebpackConfigFromContext (
97
94
options ,
98
95
context ,
@@ -125,9 +122,7 @@ function getAnalyticsConfig(
125
122
126
123
// The category is the builder name if it's an angular builder.
127
124
return {
128
- plugins : [
129
- new NgBuildAnalyticsPlugin ( wco . projectRoot , context . analytics , category ) ,
130
- ] ,
125
+ plugins : [ new NgBuildAnalyticsPlugin ( wco . projectRoot , context . analytics , category ) ] ,
131
126
} ;
132
127
}
133
128
@@ -147,7 +142,7 @@ async function initialize(
147
142
context : BuilderContext ,
148
143
host : virtualFs . Host < fs . Stats > ,
149
144
webpackConfigurationTransform ?: ExecutionTransformer < webpack . Configuration > ,
150
- ) : Promise < { workspace : experimental . workspace . Workspace , config : webpack . Configuration [ ] } > {
145
+ ) : Promise < { workspace : experimental . workspace . Workspace ; config : webpack . Configuration [ ] } > {
151
146
const { config, workspace } = await buildBrowserWebpackConfigFromContext ( options , context , host ) ;
152
147
153
148
let transformedConfig ;
@@ -173,9 +168,9 @@ export function buildWebpackBrowser(
173
168
options : BrowserBuilderSchema ,
174
169
context : BuilderContext ,
175
170
transforms : {
176
- webpackConfiguration ?: ExecutionTransformer < webpack . Configuration > ,
177
- logging ?: WebpackLoggingCallback ,
178
- indexHtml ?: IndexHtmlTransform ,
171
+ webpackConfiguration ?: ExecutionTransformer < webpack . Configuration > ;
172
+ logging ?: WebpackLoggingCallback ;
173
+ indexHtml ?: IndexHtmlTransform ;
179
174
} = { } ,
180
175
) {
181
176
const host = new NodeJsSyncHost ( ) ;
@@ -184,13 +179,14 @@ export function buildWebpackBrowser(
184
179
// Check Angular version.
185
180
assertCompatibleAngularVersion ( context . workspaceRoot , context . logger ) ;
186
181
187
- const loggingFn = transforms . logging
188
- || createBrowserLoggingCallback ( ! ! options . verbose , context . logger ) ;
182
+ const loggingFn =
183
+ transforms . logging || createBrowserLoggingCallback ( ! ! options . verbose , context . logger ) ;
189
184
190
185
return from ( initialize ( options , context , host , transforms . webpackConfiguration ) ) . pipe (
191
186
switchMap ( ( { workspace, config : configs } ) => {
192
187
const projectName = context . target
193
- ? context . target . project : workspace . getDefaultProjectName ( ) ;
188
+ ? context . target . project
189
+ : workspace . getDefaultProjectName ( ) ;
194
190
195
191
if ( ! projectName ) {
196
192
throw new Error ( 'Must either have a target from the context or a default project.' ) ;
@@ -203,12 +199,11 @@ export function buildWebpackBrowser(
203
199
204
200
const tsConfig = readTsconfig ( options . tsConfig , context . workspaceRoot ) ;
205
201
const target = tsConfig . options . target || ScriptTarget . ES5 ;
206
- const buildBrowserFeatures = new BuildBrowserFeatures (
207
- getSystemPath ( projectRoot ) ,
208
- target ,
209
- ) ;
202
+ const buildBrowserFeatures = new BuildBrowserFeatures ( getSystemPath ( projectRoot ) , target ) ;
210
203
211
- if ( target > ScriptTarget . ES2015 && buildBrowserFeatures . isDifferentialLoadingNeeded ( ) ) {
204
+ const isDifferentialLoadingNeeded = buildBrowserFeatures . isDifferentialLoadingNeeded ( ) ;
205
+
206
+ if ( target > ScriptTarget . ES2015 && isDifferentialLoadingNeeded ) {
212
207
context . logger . warn ( tags . stripIndent `
213
208
WARNING: Using differential loading with targets ES5 and ES2016 or higher may
214
209
cause problems. Browsers with support for ES2015 will load the ES2016+ scripts
@@ -219,14 +214,18 @@ export function buildWebpackBrowser(
219
214
return from ( configs ) . pipe (
220
215
// the concurrency parameter (3rd parameter of mergeScan) is deliberately
221
216
// set to 1 to make sure the build steps are executed in sequence.
222
- mergeScan ( ( lastResult , config ) => {
223
- // Make sure to only run the 2nd build step, if 1st one succeeded
224
- if ( lastResult . success ) {
225
- return runWebpack ( config , context , { logging : loggingFn } ) ;
226
- } else {
227
- return of ( ) ;
228
- }
229
- } , { success : true } as BuildResult , 1 ) ,
217
+ mergeScan (
218
+ ( lastResult , config ) => {
219
+ // Make sure to only run the 2nd build step, if 1st one succeeded
220
+ if ( lastResult . success ) {
221
+ return runWebpack ( config , context , { logging : loggingFn } ) ;
222
+ } else {
223
+ return of ( ) ;
224
+ }
225
+ } ,
226
+ { success : true } as BuildResult ,
227
+ 1 ,
228
+ ) ,
230
229
bufferCount ( configs . length ) ,
231
230
switchMap ( buildEvents => {
232
231
const success = buildEvents . every ( r => r . success ) ;
@@ -235,14 +234,19 @@ export function buildWebpackBrowser(
235
234
let moduleFiles : EmittedFiles [ ] | undefined ;
236
235
let files : EmittedFiles [ ] | undefined ;
237
236
238
- const [ ES5Result , ES2015Result ] = buildEvents ;
237
+ const [ firstBuild , secondBuild ] = buildEvents ;
239
238
240
239
if ( buildEvents . length === 2 ) {
241
- noModuleFiles = ES5Result . emittedFiles ;
242
- moduleFiles = ES2015Result . emittedFiles || [ ] ;
240
+ noModuleFiles = firstBuild . emittedFiles ;
241
+ moduleFiles = secondBuild . emittedFiles || [ ] ;
242
+ files = moduleFiles . filter ( x => x . extension === '.css' ) ;
243
+ } else if ( options . watch && isDifferentialLoadingNeeded ) {
244
+ // differential loading is not enabled in watch mode
245
+ // but we still want to use module type tags
246
+ moduleFiles = firstBuild . emittedFiles || [ ] ;
243
247
files = moduleFiles . filter ( x => x . extension === '.css' ) ;
244
248
} else {
245
- const { emittedFiles = [ ] } = ES5Result ;
249
+ const { emittedFiles = [ ] } = firstBuild ;
246
250
files = emittedFiles . filter ( x => x . name !== 'polyfills-es5' ) ;
247
251
noModuleFiles = emittedFiles . filter ( x => x . name === 'polyfills-es5' ) ;
248
252
}
@@ -260,8 +264,7 @@ export function buildWebpackBrowser(
260
264
scripts : options . scripts ,
261
265
styles : options . styles ,
262
266
postTransform : transforms . indexHtml ,
263
- } )
264
- . pipe (
267
+ } ) . pipe (
265
268
map ( ( ) => ( { success : true } ) ) ,
266
269
catchError ( error => of ( { success : false , error : mapErrorToMessage ( error ) } ) ) ,
267
270
) ;
@@ -271,26 +274,31 @@ export function buildWebpackBrowser(
271
274
} ) ,
272
275
concatMap ( buildEvent => {
273
276
if ( buildEvent . success && ! options . watch && options . serviceWorker ) {
274
- return from ( augmentAppWithServiceWorker (
275
- host ,
276
- root ,
277
- projectRoot ,
278
- resolve ( root , normalize ( options . outputPath ) ) ,
279
- options . baseHref || '/' ,
280
- options . ngswConfigPath ,
281
- ) . then (
282
- ( ) => ( { success : true } ) ,
283
- error => ( { success : false , error : mapErrorToMessage ( error ) } ) ,
284
- ) ) ;
277
+ return from (
278
+ augmentAppWithServiceWorker (
279
+ host ,
280
+ root ,
281
+ projectRoot ,
282
+ resolve ( root , normalize ( options . outputPath ) ) ,
283
+ options . baseHref || '/' ,
284
+ options . ngswConfigPath ,
285
+ ) . then (
286
+ ( ) => ( { success : true } ) ,
287
+ error => ( { success : false , error : mapErrorToMessage ( error ) } ) ,
288
+ ) ,
289
+ ) ;
285
290
} else {
286
291
return of ( buildEvent ) ;
287
292
}
288
293
} ) ,
289
- map ( event => ( {
290
- ...event ,
291
- // If we use differential loading, both configs have the same outputs
292
- outputPath : path . resolve ( context . workspaceRoot , options . outputPath ) ,
293
- } as BrowserBuilderOutput ) ) ,
294
+ map (
295
+ event =>
296
+ ( {
297
+ ...event ,
298
+ // If we use differential loading, both configs have the same outputs
299
+ outputPath : path . resolve ( context . workspaceRoot , options . outputPath ) ,
300
+ } as BrowserBuilderOutput ) ,
301
+ ) ,
294
302
) ;
295
303
} ) ,
296
304
) ;
0 commit comments