@@ -23,7 +23,7 @@ import {
23
23
import { NodeJsSyncHost } from '@angular-devkit/core/node' ;
24
24
import * as fs from 'fs' ;
25
25
import * as path from 'path' ;
26
- import { Observable , combineLatest , from , of , zip } from 'rxjs' ;
26
+ import { from , of , zip } from 'rxjs' ;
27
27
import { catchError , concatMap , map , switchMap } from 'rxjs/operators' ;
28
28
import * as webpack from 'webpack' ;
29
29
import { NgBuildAnalyticsPlugin } from '../../plugins/webpack/analytics' ;
@@ -44,6 +44,7 @@ import {
44
44
statsToString ,
45
45
statsWarningsToString ,
46
46
} from '../angular-cli-files/utilities/stats' ;
47
+ import { ExecutionTransformer } from '../transforms' ;
47
48
import { deleteOutputDir } from '../utils' ;
48
49
import { generateBrowserWebpackConfigFromContext } from '../utils/webpack-browser-config' ;
49
50
import { Schema as BrowserBuilderSchema } from './schema' ;
@@ -77,7 +78,7 @@ export function createBrowserLoggingCallback(
77
78
export async function buildBrowserWebpackConfigFromContext (
78
79
options : BrowserBuilderSchema ,
79
80
context : BuilderContext ,
80
- host : virtualFs . Host < fs . Stats > ,
81
+ host : virtualFs . Host < fs . Stats > = new NodeJsSyncHost ( ) ,
81
82
) : Promise < { workspace : experimental . workspace . Workspace , config : webpack . Configuration [ ] } > {
82
83
return generateBrowserWebpackConfigFromContext (
83
84
options ,
@@ -126,53 +127,48 @@ function getCompilerConfig(wco: WebpackConfigOptions): webpack.Configuration {
126
127
return { } ;
127
128
}
128
129
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
+ }
133
153
154
+ return { config : transformedConfig || config , workspace } ;
155
+ }
134
156
135
157
export function buildWebpackBrowser (
136
158
options : BrowserBuilderSchema ,
137
159
context : BuilderContext ,
138
160
transforms : {
139
- config ?: BrowserConfigTransformFn ,
140
- output ?: ( output : BrowserBuilderOutput ) => Observable < BuilderOutput > ,
161
+ webpackConfiguration ?: ExecutionTransformer < webpack . Configuration > ,
141
162
logging ?: WebpackLoggingCallback ,
142
163
} = { } ,
143
164
) {
144
165
const host = new NodeJsSyncHost ( ) ;
145
166
const root = normalize ( context . workspaceRoot ) ;
146
167
147
- const configFn = transforms . config ;
148
- const outputFn = transforms . output ;
149
168
const loggingFn = transforms . logging
150
169
|| createBrowserLoggingCallback ( ! ! options . verbose , context . logger ) ;
151
170
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 (
176
172
switchMap ( ( { workspace, config : configs } ) => {
177
173
const projectName = context . target
178
174
? context . target . project : workspace . getDefaultProjectName ( ) ;
@@ -237,11 +233,9 @@ export function buildWebpackBrowser(
237
233
// If we use differential loading, both configs have the same outputs
238
234
outputPath : path . resolve ( context . workspaceRoot , options . outputPath ) ,
239
235
} as BrowserBuilderOutput ) ) ,
240
- concatMap ( output => outputFn ? outputFn ( output ) : of ( output ) ) ,
241
236
) ;
242
237
} ) ,
243
238
) ;
244
239
}
245
240
246
-
247
241
export default createBuilder < json . JsonObject & BrowserBuilderSchema > ( buildWebpackBrowser ) ;
0 commit comments