7
7
*/
8
8
import { BuilderContext , BuilderOutput , createBuilder } from '@angular-devkit/architect' ;
9
9
import { EmittedFiles , WebpackLoggingCallback , runWebpack } from '@angular-devkit/build-webpack' ;
10
- import { join , json , normalize , tags , virtualFs } from '@angular-devkit/core' ;
10
+ import { getSystemPath , join , json , normalize , resolve , tags , virtualFs } from '@angular-devkit/core' ;
11
11
import { NodeJsSyncHost } from '@angular-devkit/core/node' ;
12
12
import * as fs from 'fs' ;
13
13
import * as ora from 'ora' ;
@@ -50,7 +50,6 @@ import { augmentAppWithServiceWorker } from '../utils/service-worker';
50
50
import { assertCompatibleAngularVersion } from '../utils/version' ;
51
51
import {
52
52
BrowserWebpackConfigOptions ,
53
- generateBrowserWebpackConfigFromContext ,
54
53
generateI18nBrowserWebpackConfigFromContext ,
55
54
getIndexInputFile ,
56
55
getIndexOutputFile ,
@@ -97,25 +96,15 @@ interface ConfigFromContextReturn {
97
96
config : webpack . Configuration ;
98
97
projectRoot : string ;
99
98
projectSourceRoot ?: string ;
99
+ i18n : I18nOptions ;
100
100
}
101
101
102
- export async function buildBrowserWebpackConfigFromContext (
103
- options : BrowserBuilderSchema ,
104
- context : BuilderContext ,
105
- host : virtualFs . Host < fs . Stats > ,
106
- i18n : boolean ,
107
- ) : Promise < ConfigFromContextReturn & { i18n : I18nOptions } > ;
108
- export async function buildBrowserWebpackConfigFromContext (
109
- options : BrowserBuilderSchema ,
110
- context : BuilderContext ,
111
- host ?: virtualFs . Host < fs . Stats > ,
112
- ) : Promise < ConfigFromContextReturn > ;
113
102
export async function buildBrowserWebpackConfigFromContext (
114
103
options : BrowserBuilderSchema ,
115
104
context : BuilderContext ,
116
105
host : virtualFs . Host < fs . Stats > = new NodeJsSyncHost ( ) ,
117
- i18n = false ,
118
- ) : Promise < ConfigFromContextReturn & { i18n ?: I18nOptions } > {
106
+ differentialLoadingMode = false ,
107
+ ) : Promise < ConfigFromContextReturn > {
119
108
const webpackPartialGenerator = ( wco : BrowserWebpackConfigOptions ) => [
120
109
getCommonConfig ( wco ) ,
121
110
getBrowserConfig ( wco ) ,
@@ -126,16 +115,13 @@ export async function buildBrowserWebpackConfigFromContext(
126
115
wco . buildOptions . webWorkerTsConfig ? getWorkerConfig ( wco ) : { } ,
127
116
] ;
128
117
129
- if ( i18n ) {
130
- return generateI18nBrowserWebpackConfigFromContext (
131
- options ,
132
- context ,
133
- webpackPartialGenerator ,
134
- host ,
135
- ) ;
136
- }
137
-
138
- return generateBrowserWebpackConfigFromContext ( options , context , webpackPartialGenerator , host ) ;
118
+ return generateI18nBrowserWebpackConfigFromContext (
119
+ options ,
120
+ context ,
121
+ webpackPartialGenerator ,
122
+ host ,
123
+ differentialLoadingMode ,
124
+ ) ;
139
125
}
140
126
141
127
function getAnalyticsConfig (
@@ -177,6 +163,7 @@ async function initialize(
177
163
options : BrowserBuilderSchema ,
178
164
context : BuilderContext ,
179
165
host : virtualFs . Host < fs . Stats > ,
166
+ differentialLoadingMode : boolean ,
180
167
webpackConfigurationTransform ?: ExecutionTransformer < webpack . Configuration > ,
181
168
) : Promise < {
182
169
config : webpack . Configuration ;
@@ -194,7 +181,7 @@ async function initialize(
194
181
projectRoot,
195
182
projectSourceRoot,
196
183
i18n,
197
- } = await buildBrowserWebpackConfigFromContext ( adjustedOptions , context , host , true ) ;
184
+ } = await buildBrowserWebpackConfigFromContext ( adjustedOptions , context , host , differentialLoadingMode ) ;
198
185
199
186
// Validate asset option values if processed directly
200
187
if ( options . assets ?. length && ! adjustedOptions . assets ?. length ) {
@@ -235,39 +222,60 @@ export function buildWebpackBrowser(
235
222
) : Observable < BrowserBuilderOutput > {
236
223
const host = new NodeJsSyncHost ( ) ;
237
224
const root = normalize ( context . workspaceRoot ) ;
225
+
226
+ const projectName = context . target && context . target . project ;
227
+ if ( ! projectName ) {
228
+ throw new Error ( 'The builder requires a target.' ) ;
229
+ }
230
+
238
231
const baseOutputPath = path . resolve ( context . workspaceRoot , options . outputPath ) ;
239
232
let outputPaths : undefined | Map < string , string > ;
240
233
241
234
// Check Angular version.
242
235
assertCompatibleAngularVersion ( context . workspaceRoot , context . logger ) ;
243
236
244
- return from ( initialize ( options , context , host , transforms . webpackConfiguration ) ) . pipe (
245
- // tslint:disable-next-line: no-big-function
246
- switchMap ( ( { config, projectRoot, projectSourceRoot, i18n } ) => {
247
- const tsConfig = readTsconfig ( options . tsConfig , context . workspaceRoot ) ;
248
- const target = tsConfig . options . target || ScriptTarget . ES5 ;
249
- const buildBrowserFeatures = new BuildBrowserFeatures ( projectRoot , target ) ;
250
- const isDifferentialLoadingNeeded = buildBrowserFeatures . isDifferentialLoadingNeeded ( ) ;
251
-
252
- if ( target > ScriptTarget . ES2015 && isDifferentialLoadingNeeded ) {
253
- context . logger . warn ( tags . stripIndent `
237
+ return from ( context . getProjectMetadata ( projectName ) )
238
+ . pipe (
239
+ switchMap ( async projectMetadata => {
240
+ const sysProjectRoot = getSystemPath (
241
+ resolve ( normalize ( context . workspaceRoot ) ,
242
+ normalize ( ( projectMetadata . root as string ) ?? '' ) ) ,
243
+ ) ;
244
+
245
+ const { options : compilerOptions } = readTsconfig ( options . tsConfig , context . workspaceRoot ) ;
246
+ const target = compilerOptions . target || ScriptTarget . ES5 ;
247
+ const buildBrowserFeatures = new BuildBrowserFeatures ( sysProjectRoot ) ;
248
+ const isDifferentialLoadingNeeded = buildBrowserFeatures . isDifferentialLoadingNeeded ( target ) ;
249
+ const differentialLoadingMode = ! options . watch && isDifferentialLoadingNeeded ;
250
+
251
+ if ( target > ScriptTarget . ES2015 && isDifferentialLoadingNeeded ) {
252
+ context . logger . warn ( tags . stripIndent `
254
253
WARNING: Using differential loading with targets ES5 and ES2016 or higher may
255
254
cause problems. Browsers with support for ES2015 will load the ES2016+ scripts
256
255
referenced with script[type="module"] but they may not support ES2016+ syntax.
257
256
` ) ;
258
- }
259
-
260
- const useBundleDownleveling = isDifferentialLoadingNeeded && ! options . watch ;
261
- const startTime = Date . now ( ) ;
262
-
263
- return runWebpack ( config , context , {
264
- webpackFactory : require ( 'webpack' ) as typeof webpack ,
265
- logging :
266
- transforms . logging ||
267
- ( useBundleDownleveling
268
- ? ( ) => { }
269
- : createWebpackLoggingCallback ( ! ! options . verbose , context . logger ) ) ,
270
- } ) . pipe (
257
+ }
258
+
259
+ return {
260
+ ...( await initialize ( options , context , host , differentialLoadingMode , transforms . webpackConfiguration ) ) ,
261
+ buildBrowserFeatures,
262
+ isDifferentialLoadingNeeded,
263
+ target,
264
+ } ;
265
+ } ) ,
266
+ // tslint:disable-next-line: no-big-function
267
+ switchMap ( ( { config, projectRoot, projectSourceRoot, i18n, buildBrowserFeatures, isDifferentialLoadingNeeded, target } ) => {
268
+ const useBundleDownleveling = isDifferentialLoadingNeeded && ! options . watch ;
269
+ const startTime = Date . now ( ) ;
270
+
271
+ return runWebpack ( config , context , {
272
+ webpackFactory : require ( 'webpack' ) as typeof webpack ,
273
+ logging :
274
+ transforms . logging ||
275
+ ( useBundleDownleveling
276
+ ? ( ) => { }
277
+ : createWebpackLoggingCallback ( ! ! options . verbose , context . logger ) ) ,
278
+ } ) . pipe (
271
279
// tslint:disable-next-line: no-big-function
272
280
concatMap ( async buildEvent => {
273
281
const { webpackStats : webpackRawStats , success, emittedFiles = [ ] } = buildEvent ;
0 commit comments