77 */
88import { BuilderContext , BuilderOutput , createBuilder } from '@angular-devkit/architect' ;
99import { 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' ;
1111import { NodeJsSyncHost } from '@angular-devkit/core/node' ;
1212import * as fs from 'fs' ;
1313import * as ora from 'ora' ;
@@ -50,7 +50,6 @@ import { augmentAppWithServiceWorker } from '../utils/service-worker';
5050import { assertCompatibleAngularVersion } from '../utils/version' ;
5151import {
5252 BrowserWebpackConfigOptions ,
53- generateBrowserWebpackConfigFromContext ,
5453 generateI18nBrowserWebpackConfigFromContext ,
5554 getIndexInputFile ,
5655 getIndexOutputFile ,
@@ -97,25 +96,15 @@ interface ConfigFromContextReturn {
9796 config : webpack . Configuration ;
9897 projectRoot : string ;
9998 projectSourceRoot ?: string ;
99+ i18n : I18nOptions ;
100100}
101101
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 > ;
113102export async function buildBrowserWebpackConfigFromContext (
114103 options : BrowserBuilderSchema ,
115104 context : BuilderContext ,
116105 host : virtualFs . Host < fs . Stats > = new NodeJsSyncHost ( ) ,
117- i18n = false ,
118- ) : Promise < ConfigFromContextReturn & { i18n ?: I18nOptions } > {
106+ differentialLoadingMode = false ,
107+ ) : Promise < ConfigFromContextReturn > {
119108 const webpackPartialGenerator = ( wco : BrowserWebpackConfigOptions ) => [
120109 getCommonConfig ( wco ) ,
121110 getBrowserConfig ( wco ) ,
@@ -126,16 +115,13 @@ export async function buildBrowserWebpackConfigFromContext(
126115 wco . buildOptions . webWorkerTsConfig ? getWorkerConfig ( wco ) : { } ,
127116 ] ;
128117
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+ ) ;
139125}
140126
141127function getAnalyticsConfig (
@@ -177,6 +163,7 @@ async function initialize(
177163 options : BrowserBuilderSchema ,
178164 context : BuilderContext ,
179165 host : virtualFs . Host < fs . Stats > ,
166+ differentialLoadingMode : boolean ,
180167 webpackConfigurationTransform ?: ExecutionTransformer < webpack . Configuration > ,
181168) : Promise < {
182169 config : webpack . Configuration ;
@@ -194,7 +181,7 @@ async function initialize(
194181 projectRoot,
195182 projectSourceRoot,
196183 i18n,
197- } = await buildBrowserWebpackConfigFromContext ( adjustedOptions , context , host , true ) ;
184+ } = await buildBrowserWebpackConfigFromContext ( adjustedOptions , context , host , differentialLoadingMode ) ;
198185
199186 // Validate asset option values if processed directly
200187 if ( options . assets ?. length && ! adjustedOptions . assets ?. length ) {
@@ -235,39 +222,60 @@ export function buildWebpackBrowser(
235222) : Observable < BrowserBuilderOutput > {
236223 const host = new NodeJsSyncHost ( ) ;
237224 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+
238231 const baseOutputPath = path . resolve ( context . workspaceRoot , options . outputPath ) ;
239232 let outputPaths : undefined | Map < string , string > ;
240233
241234 // Check Angular version.
242235 assertCompatibleAngularVersion ( context . workspaceRoot , context . logger ) ;
243236
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 `
254253 WARNING: Using differential loading with targets ES5 and ES2016 or higher may
255254 cause problems. Browsers with support for ES2015 will load the ES2016+ scripts
256255 referenced with script[type="module"] but they may not support ES2016+ syntax.
257256 ` ) ;
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 (
271279 // tslint:disable-next-line: no-big-function
272280 concatMap ( async buildEvent => {
273281 const { webpackStats : webpackRawStats , success, emittedFiles = [ ] } = buildEvent ;
0 commit comments