1+ import * as fs from 'fs' ;
12import * as path from 'path' ;
2- import { AotPlugin } from '@ngtools/webpack' ;
3+ import { AotPlugin , AotPluginOptions } from '@ngtools/webpack' ;
34import { WebpackConfigOptions } from '../webpack-config' ;
45
6+ const SilentError = require ( 'silent-error' ) ;
7+
58
69const g : any = global ;
710const webpackLoader : string = g [ 'angularCliIsLocal' ]
811 ? g . angularCliPackages [ '@ngtools/webpack' ] . main
912 : '@ngtools/webpack' ;
1013
1114
15+ function _createAotPlugin ( wco : WebpackConfigOptions , options : any ) {
16+ const { appConfig, projectRoot, buildOptions } = wco ;
17+
18+ // Read the environment, and set it in the compiler host.
19+ let hostOverrideFileSystem : any = { } ;
20+ // process environment file replacement
21+ if ( appConfig . environments ) {
22+ if ( ! ( 'source' in appConfig . environments ) ) {
23+ throw new SilentError ( `Environment configuration does not contain "source" entry.` ) ;
24+ }
25+ if ( ! ( buildOptions . environment in appConfig . environments ) ) {
26+ throw new SilentError ( `Environment "${ buildOptions . environment } " does not exist.` ) ;
27+ }
28+
29+ const appRoot = path . resolve ( projectRoot , appConfig . root ) ;
30+ const sourcePath = appConfig . environments [ 'source' ] ;
31+ const envFile = appConfig . environments [ buildOptions . environment ] ;
32+ const environmentContent = fs . readFileSync ( path . join ( appRoot , envFile ) ) . toString ( ) ;
33+
34+ hostOverrideFileSystem = { [ path . join ( appRoot , sourcePath ) ] : environmentContent } ;
35+ }
36+
37+ return new AotPlugin ( Object . assign ( { } , {
38+ tsConfigPath : path . resolve ( projectRoot , appConfig . root , appConfig . tsconfig ) ,
39+ mainPath : path . join ( projectRoot , appConfig . root , appConfig . main ) ,
40+ i18nFile : buildOptions . i18nFile ,
41+ i18nFormat : buildOptions . i18nFormat ,
42+ locale : buildOptions . locale ,
43+ hostOverrideFileSystem
44+ } , options ) ) ;
45+ }
46+
47+
1248export const getNonAotConfig = function ( wco : WebpackConfigOptions ) {
1349 const { projectRoot, appConfig } = wco ;
1450 let exclude = [ '**/*.spec.ts' ] ;
@@ -24,18 +60,13 @@ export const getNonAotConfig = function(wco: WebpackConfigOptions) {
2460 ]
2561 } ,
2662 plugins : [
27- new AotPlugin ( {
28- tsConfigPath : path . resolve ( projectRoot , appConfig . root , appConfig . tsconfig ) ,
29- mainPath : path . join ( projectRoot , appConfig . root , appConfig . main ) ,
30- exclude : exclude ,
31- skipCodeGeneration : true
32- } ) ,
63+ _createAotPlugin ( wco , { exclude, skipCodeGeneration : true } ) ,
3364 ]
3465 } ;
3566} ;
3667
3768export const getAotConfig = function ( wco : WebpackConfigOptions ) {
38- const { projectRoot, buildOptions , appConfig } = wco ;
69+ const { projectRoot, appConfig } = wco ;
3970 let exclude = [ '**/*.spec.ts' ] ;
4071 if ( appConfig . test ) { exclude . push ( path . join ( projectRoot , appConfig . root , appConfig . test ) ) ; } ;
4172 return {
@@ -49,14 +80,7 @@ export const getAotConfig = function(wco: WebpackConfigOptions) {
4980 ]
5081 } ,
5182 plugins : [
52- new AotPlugin ( {
53- tsConfigPath : path . resolve ( projectRoot , appConfig . root , appConfig . tsconfig ) ,
54- mainPath : path . join ( projectRoot , appConfig . root , appConfig . main ) ,
55- i18nFile : buildOptions . i18nFile ,
56- i18nFormat : buildOptions . i18nFormat ,
57- locale : buildOptions . locale ,
58- exclude : exclude
59- } )
83+ _createAotPlugin ( wco , { exclude } )
6084 ]
6185 } ;
6286} ;
0 commit comments