Skip to content

Commit 8797d50

Browse files
committed
fix(@angular/cli): add environment file to compilerHost
Fixes #4375
1 parent 26def64 commit 8797d50

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed
Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import * as fs from 'fs';
12
import * as path from 'path';
2-
import {AotPlugin} from '@ngtools/webpack';
3+
import {AotPlugin, AotPluginOptions} from '@ngtools/webpack';
34
import { WebpackConfigOptions } from '../webpack-config';
45

56

@@ -9,6 +10,28 @@ const webpackLoader: string = g['angularCliIsLocal']
910
: '@ngtools/webpack';
1011

1112

13+
function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
14+
const { appConfig, projectRoot, buildOptions } = wco;
15+
16+
// Read the environment, and set it in the compiler host.
17+
const appRoot = path.resolve(projectRoot, wco.appConfig.root);
18+
const sourcePath = wco.appConfig.environments['source'];
19+
const envFile = wco.appConfig.environments[wco.buildOptions.environment];
20+
const environmentContent = fs.readFileSync(path.join(appRoot, envFile)).toString();
21+
22+
return new AotPlugin(Object.assign({}, {
23+
tsConfigPath: path.resolve(projectRoot, appConfig.root, appConfig.tsconfig),
24+
mainPath: path.join(projectRoot, appConfig.root, appConfig.main),
25+
i18nFile: buildOptions.i18nFile,
26+
i18nFormat: buildOptions.i18nFormat,
27+
locale: buildOptions.locale,
28+
hostOverrideFileSystem: {
29+
[path.join(appRoot, sourcePath)]: environmentContent
30+
}
31+
}, options));
32+
}
33+
34+
1235
export const getNonAotConfig = function(wco: WebpackConfigOptions) {
1336
const { projectRoot, appConfig } = wco;
1437
let exclude = [ '**/*.spec.ts' ];
@@ -24,18 +47,13 @@ export const getNonAotConfig = function(wco: WebpackConfigOptions) {
2447
]
2548
},
2649
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-
}),
50+
_createAotPlugin(wco, { exclude, skipCodeGeneration: true }),
3351
]
3452
};
3553
};
3654

3755
export const getAotConfig = function(wco: WebpackConfigOptions) {
38-
const { projectRoot, buildOptions, appConfig } = wco;
56+
const { projectRoot, appConfig } = wco;
3957
let exclude = [ '**/*.spec.ts' ];
4058
if (appConfig.test) { exclude.push(path.join(projectRoot, appConfig.root, appConfig.test)); };
4159
return {
@@ -49,14 +67,7 @@ export const getAotConfig = function(wco: WebpackConfigOptions) {
4967
]
5068
},
5169
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-
})
70+
_createAotPlugin(wco, { exclude })
6071
]
6172
};
6273
};

packages/@ngtools/webpack/src/plugin.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface AotPluginOptions {
2424
mainPath?: string;
2525
typeChecking?: boolean;
2626
skipCodeGeneration?: boolean;
27+
hostOverrideFileSystem?: { [path: string]: string };
2728
i18nFile?: string;
2829
i18nFormat?: string;
2930
locale?: string;
@@ -167,6 +168,14 @@ export class AotPlugin implements Tapable {
167168
}
168169

169170
this._compilerHost = new WebpackCompilerHost(this._compilerOptions, this._basePath);
171+
172+
// Override some files in the FileSystem.
173+
if (options.hasOwnProperty('hostOverrideFileSystem')) {
174+
for (const filePath of Object.keys(options.hostOverrideFileSystem)) {
175+
this._compilerHost.writeFile(filePath, options.hostOverrideFileSystem[filePath], false);
176+
}
177+
}
178+
170179
this._program = ts.createProgram(
171180
this._rootFilePath, this._compilerOptions, this._compilerHost);
172181

0 commit comments

Comments
 (0)