Skip to content

Commit 11526ef

Browse files
committed
fix(@angular/cli): add environment file to compilerHost
Fixes #4375
1 parent 5972f1a commit 11526ef

File tree

3 files changed

+49
-36
lines changed

3 files changed

+49
-36
lines changed

packages/@angular/cli/models/webpack-configs/common.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const autoprefixer = require('autoprefixer');
1010
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
1111
const HtmlWebpackPlugin = require('html-webpack-plugin');
1212
const ExtractTextPlugin = require('extract-text-webpack-plugin');
13-
const SilentError = require('silent-error');
1413

1514
/**
1615
* Enumerate loaders and their dependencies from this file to let the dependency validator
@@ -71,25 +70,6 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
7170
}));
7271
}
7372

74-
// process environment file replacement
75-
if (appConfig.environments) {
76-
if (!('source' in appConfig.environments)) {
77-
throw new SilentError(`Environment configuration does not contain "source" entry.`);
78-
}
79-
if (!(buildOptions.environment in appConfig.environments)) {
80-
throw new SilentError(`Environment "${buildOptions.environment}" does not exist.`);
81-
}
82-
83-
extraPlugins.push(new webpack.NormalModuleReplacementPlugin(
84-
// This plugin is responsible for swapping the environment files.
85-
// Since it takes a RegExp as first parameter, we need to escape the path.
86-
// See https://webpack.github.io/docs/list-of-plugins.html#normalmodulereplacementplugin
87-
new RegExp(path.resolve(appRoot, appConfig.environments['source'])
88-
.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&')),
89-
path.resolve(appRoot, appConfig.environments[buildOptions.environment])
90-
));
91-
}
92-
9373
// process asset entries
9474
if (appConfig.assets) {
9575
extraPlugins.push(new GlobCopyWebpackPlugin({
Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,50 @@
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

6+
const SilentError = require('silent-error');
7+
58

69
const g: any = global;
710
const 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+
1248
export 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

3768
export 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
};

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)