Skip to content

Commit a77e409

Browse files
committed
feat(@ngtools/webpack): support hostReplacementPaths
Fix angular#12197
1 parent 35a3b39 commit a77e409

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

packages/ngtools/webpack/src/angular_compiler_plugin.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,13 @@ export class AngularCompilerPlugin {
600600
private _updateForkedTypeChecker(rootNames: string[], changedCompilationFiles: string[]) {
601601
if (this._typeCheckerProcess) {
602602
if (!this._forkedTypeCheckerInitialized) {
603+
let hostReplacementPaths = {};
604+
if (this._options.hostReplacementPaths
605+
&& typeof this._options.hostReplacementPaths != 'function') {
606+
hostReplacementPaths = this._options.hostReplacementPaths;
607+
}
603608
this._typeCheckerProcess.send(new InitMessage(this._compilerOptions, this._basePath,
604-
this._JitMode, this._rootNames));
609+
this._JitMode, this._rootNames, hostReplacementPaths));
605610
this._forkedTypeCheckerInitialized = true;
606611
}
607612
this._typeCheckerProcess.send(new UpdateMessage(rootNames, changedCompilationFiles));

packages/ngtools/webpack/src/type_checker.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { logging } from '@angular-devkit/core';
8+
import { logging, normalize, resolve, virtualFs } from '@angular-devkit/core';
99
import { NodeJsSyncHost } from '@angular-devkit/core/node';
1010
import {
1111
CompilerHost,
@@ -40,6 +40,7 @@ export class InitMessage extends TypeCheckerMessage {
4040
public basePath: string,
4141
public jitMode: boolean,
4242
public rootNames: string[],
43+
public hostReplacementPaths: { [path: string]: string },
4344
) {
4445
super(MESSAGE_KIND.Init);
4546
}
@@ -68,12 +69,25 @@ export class TypeChecker {
6869
_basePath: string,
6970
private _JitMode: boolean,
7071
private _rootNames: string[],
72+
hostReplacementPaths: { [path: string]: string },
7173
) {
7274
time('TypeChecker.constructor');
75+
const host = new virtualFs.AliasHost(new NodeJsSyncHost());
76+
77+
// Add file replacements.
78+
for (const from in hostReplacementPaths) {
79+
const normalizedFrom = resolve(normalize(_basePath), normalize(from));
80+
const normalizedWith = resolve(
81+
normalize(_basePath),
82+
normalize(hostReplacementPaths[from]),
83+
);
84+
host.aliases.set(normalizedFrom, normalizedWith);
85+
}
86+
7387
const compilerHost = new WebpackCompilerHost(
7488
_compilerOptions,
7589
_basePath,
76-
new NodeJsSyncHost(),
90+
host,
7791
);
7892
// We don't set a async resource loader on the compiler host because we only support
7993
// html templates, which are the only ones that can throw errors, and those can be loaded

packages/ngtools/webpack/src/type_checker_worker.ts

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ if (process.argv.indexOf(AUTO_START_ARG) >= 0) {
3232
initMessage.basePath,
3333
initMessage.jitMode,
3434
initMessage.rootNames,
35+
initMessage.hostReplacementPaths,
3536
);
3637
break;
3738
case MESSAGE_KIND.Update:

0 commit comments

Comments
 (0)