@@ -23,7 +23,7 @@ import {
2323import { NodeJsSyncHost } from '@angular-devkit/core/node' ;
2424import * as fs from 'fs' ;
2525import * as path from 'path' ;
26- import { Observable , combineLatest , from , of , zip } from 'rxjs' ;
26+ import { from , of , zip } from 'rxjs' ;
2727import { catchError , concatMap , map , switchMap } from 'rxjs/operators' ;
2828import * as webpack from 'webpack' ;
2929import { NgBuildAnalyticsPlugin } from '../../plugins/webpack/analytics' ;
@@ -44,6 +44,7 @@ import {
4444 statsToString ,
4545 statsWarningsToString ,
4646} from '../angular-cli-files/utilities/stats' ;
47+ import { ExecutionTransformer } from '../transforms' ;
4748import { deleteOutputDir } from '../utils' ;
4849import { generateBrowserWebpackConfigFromContext } from '../utils/webpack-browser-config' ;
4950import { Schema as BrowserBuilderSchema } from './schema' ;
@@ -77,7 +78,7 @@ export function createBrowserLoggingCallback(
7778export async function buildBrowserWebpackConfigFromContext (
7879 options : BrowserBuilderSchema ,
7980 context : BuilderContext ,
80- host : virtualFs . Host < fs . Stats > ,
81+ host : virtualFs . Host < fs . Stats > = new NodeJsSyncHost ( ) ,
8182) : Promise < { workspace : experimental . workspace . Workspace , config : webpack . Configuration [ ] } > {
8283 return generateBrowserWebpackConfigFromContext (
8384 options ,
@@ -126,53 +127,48 @@ function getCompilerConfig(wco: WebpackConfigOptions): webpack.Configuration {
126127 return { } ;
127128}
128129
129- export type BrowserConfigTransformFn = (
130- workspace : experimental . workspace . Workspace ,
131- config : webpack . Configuration ,
132- ) => Observable < webpack . Configuration > ;
130+ async function initialize (
131+ options : BrowserBuilderSchema ,
132+ context : BuilderContext ,
133+ host : virtualFs . Host < fs . Stats > ,
134+ webpackConfigurationTransform ?: ExecutionTransformer < webpack . Configuration > ,
135+ ) : Promise < { workspace : experimental . workspace . Workspace , config : webpack . Configuration [ ] } > {
136+ const { config, workspace } = await buildBrowserWebpackConfigFromContext ( options , context , host ) ;
137+
138+ let transformedConfig ;
139+ if ( webpackConfigurationTransform ) {
140+ transformedConfig = [ ] ;
141+ for ( const c of config ) {
142+ transformedConfig . push ( await webpackConfigurationTransform ( c ) ) ;
143+ }
144+ }
145+
146+ if ( options . deleteOutputPath ) {
147+ await deleteOutputDir (
148+ normalize ( context . workspaceRoot ) ,
149+ normalize ( options . outputPath ) ,
150+ host ,
151+ ) . toPromise ( ) ;
152+ }
133153
154+ return { config : transformedConfig || config , workspace } ;
155+ }
134156
135157export function buildWebpackBrowser (
136158 options : BrowserBuilderSchema ,
137159 context : BuilderContext ,
138160 transforms : {
139- config ?: BrowserConfigTransformFn ,
140- output ?: ( output : BrowserBuilderOutput ) => Observable < BuilderOutput > ,
161+ webpackConfiguration ?: ExecutionTransformer < webpack . Configuration > ,
141162 logging ?: WebpackLoggingCallback ,
142163 } = { } ,
143164) {
144165 const host = new NodeJsSyncHost ( ) ;
145166 const root = normalize ( context . workspaceRoot ) ;
146167
147- const configFn = transforms . config ;
148- const outputFn = transforms . output ;
149168 const loggingFn = transforms . logging
150169 || createBrowserLoggingCallback ( ! ! options . verbose , context . logger ) ;
151170
152- // This makes a host observable into a cold one. This is because we want to wait until
153- // subscription before calling buildBrowserWebpackConfigFromContext, which can throw.
154- return of ( null ) . pipe (
155- switchMap ( ( ) => from ( buildBrowserWebpackConfigFromContext ( options , context , host ) ) ) ,
156- switchMap ( ( { workspace, config } ) => {
157- if ( configFn ) {
158- return combineLatest ( config . map ( config => configFn ( workspace , config ) ) ) . pipe (
159- map ( config => ( { workspace, config } ) ) ,
160- ) ;
161- } else {
162- return of ( { workspace, config } ) ;
163- }
164- } ) ,
165- switchMap ( ( { workspace, config } ) => {
166- if ( options . deleteOutputPath ) {
167- return deleteOutputDir (
168- root ,
169- normalize ( options . outputPath ) ,
170- host ,
171- ) . pipe ( map ( ( ) => ( { workspace, config } ) ) ) ;
172- } else {
173- return of ( { workspace, config } ) ;
174- }
175- } ) ,
171+ return from ( initialize ( options , context , host , transforms . webpackConfiguration ) ) . pipe (
176172 switchMap ( ( { workspace, config : configs } ) => {
177173 const projectName = context . target
178174 ? context . target . project : workspace . getDefaultProjectName ( ) ;
@@ -237,11 +233,9 @@ export function buildWebpackBrowser(
237233 // If we use differential loading, both configs have the same outputs
238234 outputPath : path . resolve ( context . workspaceRoot , options . outputPath ) ,
239235 } as BrowserBuilderOutput ) ) ,
240- concatMap ( output => outputFn ? outputFn ( output ) : of ( output ) ) ,
241236 ) ;
242237 } ) ,
243238 ) ;
244239}
245240
246-
247241export default createBuilder < json . JsonObject & BrowserBuilderSchema > ( buildWebpackBrowser ) ;
0 commit comments