@@ -12,7 +12,6 @@ const build_webpack_1 = require("@angular-devkit/build-webpack");
1212const core_1 = require ( "@angular-devkit/core" ) ;
1313const node_1 = require ( "@angular-devkit/core/node" ) ;
1414const fs = require ( "fs" ) ;
15- const os = require ( "os" ) ;
1615const path = require ( "path" ) ;
1716const rxjs_1 = require ( "rxjs" ) ;
1817const operators_1 = require ( "rxjs/operators" ) ;
@@ -24,15 +23,14 @@ const read_tsconfig_1 = require("../angular-cli-files/utilities/read-tsconfig");
2423const service_worker_1 = require ( "../angular-cli-files/utilities/service-worker" ) ;
2524const stats_1 = require ( "../angular-cli-files/utilities/stats" ) ;
2625const utils_1 = require ( "../utils" ) ;
26+ const action_executor_1 = require ( "../utils/action-executor" ) ;
2727const cache_path_1 = require ( "../utils/cache-path" ) ;
2828const copy_assets_1 = require ( "../utils/copy-assets" ) ;
2929const environment_options_1 = require ( "../utils/environment-options" ) ;
3030const i18n_inlining_1 = require ( "../utils/i18n-inlining" ) ;
31- const i18n_options_1 = require ( "../utils/i18n-options" ) ;
32- const load_translations_1 = require ( "../utils/load-translations" ) ;
31+ const output_paths_1 = require ( "../utils/output-paths" ) ;
3332const version_1 = require ( "../utils/version" ) ;
3433const webpack_browser_config_1 = require ( "../utils/webpack-browser-config" ) ;
35- const action_executor_1 = require ( "./action-executor" ) ;
3634const cacheDownlevelPath = environment_options_1 . cachingDisabled ? undefined : cache_path_1 . findCachePath ( 'angular-build-dl' ) ;
3735function createBrowserLoggingCallback ( verbose , logger ) {
3836 return ( stats , config ) => {
@@ -53,16 +51,20 @@ function createBrowserLoggingCallback(verbose, logger) {
5351 } ;
5452}
5553exports . createBrowserLoggingCallback = createBrowserLoggingCallback ;
56- async function buildBrowserWebpackConfigFromContext ( options , context , host = new node_1 . NodeJsSyncHost ( ) ) {
57- return webpack_browser_config_1 . generateBrowserWebpackConfigFromContext ( options , context , wco => [
54+ async function buildBrowserWebpackConfigFromContext ( options , context , host = new node_1 . NodeJsSyncHost ( ) , i18n = false ) {
55+ const webpackPartialGenerator = ( wco ) => [
5856 webpack_configs_1 . getCommonConfig ( wco ) ,
5957 webpack_configs_1 . getBrowserConfig ( wco ) ,
6058 webpack_configs_1 . getStylesConfig ( wco ) ,
6159 webpack_configs_1 . getStatsConfig ( wco ) ,
6260 getAnalyticsConfig ( wco , context ) ,
6361 getCompilerConfig ( wco ) ,
6462 wco . buildOptions . webWorkerTsConfig ? webpack_configs_1 . getWorkerConfig ( wco ) : { } ,
65- ] , host ) ;
63+ ] ;
64+ if ( i18n ) {
65+ return webpack_browser_config_1 . generateI18nBrowserWebpackConfigFromContext ( options , context , webpackPartialGenerator , host ) ;
66+ }
67+ return webpack_browser_config_1 . generateBrowserWebpackConfigFromContext ( options , context , webpackPartialGenerator , host ) ;
6668}
6769exports . buildBrowserWebpackConfigFromContext = buildBrowserWebpackConfigFromContext ;
6870function getAnalyticsConfig ( wco , context ) {
@@ -88,66 +90,14 @@ function getCompilerConfig(wco) {
8890 return { } ;
8991}
9092async function initialize ( options , context , host , webpackConfigurationTransform ) {
91- if ( ! context . target ) {
92- throw new Error ( 'The builder requires a target.' ) ;
93- }
94- const tsConfig = read_tsconfig_1 . readTsconfig ( options . tsConfig , context . workspaceRoot ) ;
95- const usingIvy = tsConfig . options . enableIvy !== false ;
96- const metadata = await context . getProjectMetadata ( context . target ) ;
97- const projectRoot = path . join ( context . workspaceRoot , metadata . root || '' ) ;
98- const i18n = i18n_options_1 . createI18nOptions ( metadata , options . localize ) ;
99- // Until 11.0, support deprecated i18n options when not using new localize option
100- // i18nFormat is automatically calculated
101- if ( options . localize === undefined && usingIvy ) {
102- i18n_options_1 . mergeDeprecatedI18nOptions ( i18n , options . i18nLocale , options . i18nFile ) ;
103- }
104- else if ( options . localize !== undefined && ! usingIvy ) {
105- options . localize = undefined ;
106- context . logger . warn ( `Option 'localize' is not supported with View Engine.` ) ;
107- }
108- if ( i18n . shouldInline ) {
109- // Load locales
110- const loader = await load_translations_1 . createTranslationLoader ( ) ;
111- const usedFormats = new Set ( ) ;
112- for ( const [ locale , desc ] of Object . entries ( i18n . locales ) ) {
113- if ( i18n . inlineLocales . has ( locale ) ) {
114- const result = loader ( path . join ( projectRoot , desc . file ) ) ;
115- usedFormats . add ( result . format ) ;
116- if ( usedFormats . size > 1 && tsConfig . options . enableI18nLegacyMessageIdFormat !== false ) {
117- // This limitation is only for legacy message id support (defaults to true as of 9.0)
118- throw new Error ( 'Localization currently only supports using one type of translation file format for the entire application.' ) ;
119- }
120- desc . format = result . format ;
121- desc . translation = result . translation ;
122- }
123- }
124- // Legacy message id's require the format of the translations
125- if ( usedFormats . size > 0 ) {
126- options . i18nFormat = [ ...usedFormats ] [ 0 ] ;
127- }
128- }
12993 const originalOutputPath = options . outputPath ;
130- // If inlining store the output in a temporary location to facilitate post-processing
131- if ( i18n . shouldInline ) {
132- options . outputPath = fs . mkdtempSync ( path . join ( fs . realpathSync ( os . tmpdir ( ) ) , 'angular-cli-' ) ) ;
133- }
134- const { config, projectSourceRoot } = await buildBrowserWebpackConfigFromContext ( options , context , host ) ;
135- if ( i18n . shouldInline ) {
136- // Remove localize "polyfill"
137- if ( ! config . resolve ) {
138- config . resolve = { } ;
139- }
140- if ( ! config . resolve . alias ) {
141- config . resolve . alias = { } ;
142- }
143- config . resolve . alias [ '@angular/localize/init' ] = require . resolve ( './empty.js' ) ;
144- }
94+ const { config, projectRoot, projectSourceRoot, i18n } = await buildBrowserWebpackConfigFromContext ( options , context , host , true ) ;
14595 let transformedConfig ;
14696 if ( webpackConfigurationTransform ) {
14797 transformedConfig = await webpackConfigurationTransform ( config ) ;
14898 }
14999 if ( options . deleteOutputPath ) {
150- await utils_1 . deleteOutputDir ( core_1 . normalize ( context . workspaceRoot ) , core_1 . normalize ( originalOutputPath ) , host ) . toPromise ( ) ;
100+ utils_1 . deleteOutputDir ( context . workspaceRoot , originalOutputPath ) ;
151101 }
152102 return { config : transformedConfig || config , projectRoot, projectSourceRoot, i18n } ;
153103}
@@ -198,14 +148,7 @@ function buildWebpackBrowser(options, context, transforms = {}) {
198148 return { success } ;
199149 }
200150 else if ( success ) {
201- const outputPaths = i18n . shouldInline && ! i18n . flatOutput
202- ? [ ...i18n . inlineLocales ] . map ( l => path . join ( baseOutputPath , l ) )
203- : [ baseOutputPath ] ;
204- for ( const outputPath of outputPaths ) {
205- if ( ! fs . existsSync ( outputPath ) ) {
206- fs . mkdirSync ( outputPath , { recursive : true } ) ;
207- }
208- }
151+ const outputPaths = output_paths_1 . ensureOutputPaths ( baseOutputPath , i18n ) ;
209152 let noModuleFiles ;
210153 let moduleFiles ;
211154 let files ;
@@ -214,7 +157,7 @@ function buildWebpackBrowser(options, context, transforms = {}) {
214157 moduleFiles = emittedFiles ;
215158 files = moduleFiles . filter ( x => x . extension === '.css' || ( x . name && scriptsEntryPointName . includes ( x . name ) ) ) ;
216159 if ( i18n . shouldInline ) {
217- const success = await i18nInlineEmittedFiles ( context , emittedFiles , i18n , baseOutputPath , outputPaths , scriptsEntryPointName ,
160+ const success = await i18n_inlining_1 . i18nInlineEmittedFiles ( context , emittedFiles , i18n , baseOutputPath , outputPaths , scriptsEntryPointName ,
218161 // tslint:disable-next-line: no-non-null-assertion
219162 webpackStats . outputPath , target <= typescript_1 . ScriptTarget . ES5 , options . i18nMissingTranslation ) ;
220163 if ( ! success ) {
@@ -417,14 +360,6 @@ function buildWebpackBrowser(options, context, transforms = {}) {
417360 }
418361 finally {
419362 executor . stop ( ) ;
420- if ( i18n . shouldInline ) {
421- try {
422- // Remove temporary directory used for i18n processing
423- // tslint:disable-next-line: no-non-null-assertion
424- await host . delete ( core_1 . normalize ( webpackStats . outputPath ) ) . toPromise ( ) ;
425- }
426- catch ( _b ) { }
427- }
428363 }
429364 // Copy assets
430365 if ( options . assets ) {
@@ -488,7 +423,7 @@ function buildWebpackBrowser(options, context, transforms = {}) {
488423 files = emittedFiles . filter ( x => x . name !== 'polyfills-es5' ) ;
489424 noModuleFiles = emittedFiles . filter ( x => x . name === 'polyfills-es5' ) ;
490425 if ( i18n . shouldInline ) {
491- const success = await i18nInlineEmittedFiles ( context , emittedFiles , i18n , baseOutputPath , outputPaths , scriptsEntryPointName ,
426+ const success = await i18n_inlining_1 . i18nInlineEmittedFiles ( context , emittedFiles , i18n , baseOutputPath , outputPaths , scriptsEntryPointName ,
492427 // tslint:disable-next-line: no-non-null-assertion
493428 webpackStats . outputPath , target <= typescript_1 . ScriptTarget . ES5 , options . i18nMissingTranslation ) ;
494429 if ( ! success ) {
@@ -542,45 +477,6 @@ function generateIndex(baseOutputPath, options, root, files, noModuleFiles, modu
542477 lang : options . i18nLocale ,
543478 } ) . toPromise ( ) ;
544479}
545- async function i18nInlineEmittedFiles ( context , emittedFiles , i18n , baseOutputPath , outputPaths , scriptsEntryPointName , emittedPath , es5 , missingTranslation ) {
546- const executor = new action_executor_1 . BundleActionExecutor ( { i18n } ) ;
547- let hasErrors = false ;
548- try {
549- const { options, originalFiles : processedFiles } = i18n_inlining_1 . emittedFilesToInlineOptions ( emittedFiles , scriptsEntryPointName , emittedPath , baseOutputPath , es5 , missingTranslation ) ;
550- for await ( const result of executor . inlineAll ( options ) ) {
551- for ( const diagnostic of result . diagnostics ) {
552- if ( diagnostic . type === 'error' ) {
553- hasErrors = true ;
554- context . logger . error ( diagnostic . message ) ;
555- }
556- else {
557- context . logger . warn ( diagnostic . message ) ;
558- }
559- }
560- }
561- // Copy any non-processed files into the output locations
562- await copy_assets_1 . copyAssets ( [
563- {
564- glob : '**/*' ,
565- input : emittedPath ,
566- output : '' ,
567- ignore : [ ...processedFiles ] . map ( f => path . relative ( emittedPath , f ) ) ,
568- } ,
569- ] , outputPaths , '' ) ;
570- }
571- catch ( err ) {
572- context . logger . error ( 'Localized bundle generation failed: ' + err . message ) ;
573- return false ;
574- }
575- finally {
576- executor . stop ( ) ;
577- }
578- context . logger . info ( `Localized bundle generation ${ hasErrors ? 'failed' : 'complete' } .` ) ;
579- if ( hasErrors ) {
580- return false ;
581- }
582- return true ;
583- }
584480function mapErrorToMessage ( error ) {
585481 if ( error instanceof Error ) {
586482 return error . message ;
0 commit comments