Skip to content

Commit 5d3c5dc

Browse files
committed
feat(@ngtools/webpack): add an option to redirect files
It takes multiple paths and not the content, which is cleaner in a configuration.
1 parent 2420b0b commit 5d3c5dc

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
1717
const { appConfig, projectRoot, buildOptions } = wco;
1818

1919
// Read the environment, and set it in the compiler host.
20-
let hostOverrideFileSystem: any = {};
20+
let hostReplacementPaths: any = {};
2121
// process environment file replacement
2222
if (appConfig.environments) {
2323
if (!appConfig.environmentSource) {
@@ -58,9 +58,10 @@ function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
5858
const appRoot = path.resolve(projectRoot, appConfig.root);
5959
const sourcePath = appConfig.environmentSource;
6060
const envFile = appConfig.environments[buildOptions.environment];
61-
const environmentContent = fs.readFileSync(path.join(appRoot, envFile)).toString();
6261

63-
hostOverrideFileSystem = { [path.join(appRoot, sourcePath)]: environmentContent };
62+
hostReplacementPaths = {
63+
[path.join(appRoot, sourcePath)]: path.join(appRoot, envFile)
64+
};
6465
}
6566

6667
return new AotPlugin(Object.assign({}, {
@@ -69,15 +70,18 @@ function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
6970
i18nFile: buildOptions.i18nFile,
7071
i18nFormat: buildOptions.i18nFormat,
7172
locale: buildOptions.locale,
72-
hostOverrideFileSystem
73+
hostReplacementPaths
7374
}, options));
7475
}
7576

7677

7778
export const getNonAotConfig = function(wco: WebpackConfigOptions) {
7879
const { projectRoot, appConfig } = wco;
7980
let exclude = [ '**/*.spec.ts' ];
80-
if (appConfig.test) { exclude.push(path.join(projectRoot, appConfig.root, appConfig.test)); };
81+
if (appConfig.test) {
82+
exclude.push(path.join(projectRoot, appConfig.root, appConfig.test));
83+
}
84+
8185
return {
8286
module: {
8387
rules: [

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface AotPluginOptions {
2525
typeChecking?: boolean;
2626
skipCodeGeneration?: boolean;
2727
hostOverrideFileSystem?: { [path: string]: string };
28+
hostReplacementPaths?: { [path: string]: string };
2829
i18nFile?: string;
2930
i18nFormat?: string;
3031
locale?: string;
@@ -35,6 +36,8 @@ export interface AotPluginOptions {
3536

3637

3738
export class AotPlugin implements Tapable {
39+
private _options: AotPluginOptions;
40+
3841
private _compilerOptions: ts.CompilerOptions;
3942
private _angularCompilerOptions: AngularCompilerOptions;
4043
private _program: ts.Program;
@@ -62,9 +65,11 @@ export class AotPlugin implements Tapable {
6265
private _firstRun = true;
6366

6467
constructor(options: AotPluginOptions) {
65-
this._setupOptions(options);
68+
this._options = Object.assign({}, options);
69+
this._setupOptions(this._options);
6670
}
6771

72+
get options() { return this._options; }
6873
get basePath() { return this._basePath; }
6974
get compilation() { return this._compilation; }
7075
get compilerHost() { return this._compilerHost; }
@@ -175,6 +180,14 @@ export class AotPlugin implements Tapable {
175180
this._compilerHost.writeFile(filePath, options.hostOverrideFileSystem[filePath], false);
176181
}
177182
}
183+
// Override some files in the FileSystem with paths from the actual file system.
184+
if (options.hasOwnProperty('hostReplacementPaths')) {
185+
for (const filePath of Object.keys(options.hostReplacementPaths)) {
186+
const replacementFilePath = options.hostReplacementPaths[filePath];
187+
const content = this._compilerHost.readFile(replacementFilePath);
188+
this._compilerHost.writeFile(filePath, content, false);
189+
}
190+
}
178191

179192
this._program = ts.createProgram(
180193
this._rootFilePath, this._compilerOptions, this._compilerHost);
@@ -193,8 +206,8 @@ export class AotPlugin implements Tapable {
193206

194207
// still no _entryModule? => try to resolve from mainPath
195208
if (!this._entryModule && options.mainPath) {
196-
this._entryModule = resolveEntryModuleFromMain(options.mainPath, this._compilerHost,
197-
this._program);
209+
const mainPath = path.resolve(basePath, options.mainPath);
210+
this._entryModule = resolveEntryModuleFromMain(mainPath, this._compilerHost, this._program);
198211
}
199212

200213
if (options.hasOwnProperty('i18nFile')) {

0 commit comments

Comments
 (0)