Skip to content

Commit 14925bd

Browse files
committed
fix(@ngtools/webpack): support watching file replacements
1 parent a2bab51 commit 14925bd

File tree

2 files changed

+53
-12
lines changed

2 files changed

+53
-12
lines changed

packages/ngtools/webpack/src/angular_compiler_plugin.ts

+14-1
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 { dirname, normalize, resolve, virtualFs } from '@angular-devkit/core';
8+
import { Path, dirname, normalize, resolve, virtualFs } from '@angular-devkit/core';
99
import { NodeJsSyncHost } from '@angular-devkit/core/node';
1010
import { ChildProcess, ForkOptions, fork } from 'child_process';
1111
import * as fs from 'fs';
@@ -617,8 +617,21 @@ export class AngularCompilerPlugin {
617617
this._compilerHost,
618618
);
619619
compilerWithFileSystems.inputFileSystem = inputDecorator;
620+
621+
let replacements: Map<Path, Path> | undefined;
622+
if (this._options.hostReplacementPaths) {
623+
replacements = new Map();
624+
for (const replace in this._options.hostReplacementPaths) {
625+
replacements.set(
626+
normalize(replace),
627+
normalize(this._options.hostReplacementPaths[replace]),
628+
);
629+
}
630+
}
631+
620632
compilerWithFileSystems.watchFileSystem = new VirtualWatchFileSystemDecorator(
621633
inputDecorator,
634+
replacements,
622635
);
623636
});
624637

packages/ngtools/webpack/src/virtual_file_system_decorator.ts

+39-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +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 { Path, getSystemPath, normalize } from '@angular-devkit/core';
89
import { Stats } from 'fs';
910
import { WebpackCompilerHost } from './compiler_host';
1011
import { Callback, InputFileSystem, NodeWatchFileSystemInterface } from './webpack';
@@ -105,24 +106,27 @@ export class VirtualFileSystemDecorator implements InputFileSystem {
105106
}
106107

107108
export class VirtualWatchFileSystemDecorator extends NodeWatchFileSystem {
108-
constructor(private _virtualInputFileSystem: VirtualFileSystemDecorator) {
109+
constructor(
110+
private _virtualInputFileSystem: VirtualFileSystemDecorator,
111+
private _replacements?: Map<Path, Path>,
112+
) {
109113
super(_virtualInputFileSystem);
110114
}
111115

112116
watch(
113-
files: any, // tslint:disable-line:no-any
114-
dirs: any, // tslint:disable-line:no-any
115-
missing: any, // tslint:disable-line:no-any
116-
startTime: any, // tslint:disable-line:no-any
117-
options: any, // tslint:disable-line:no-any
117+
files: string[],
118+
dirs: string[],
119+
missing: string[],
120+
startTime: number | undefined,
121+
options: {},
118122
callback: any, // tslint:disable-line:no-any
119123
callbackUndelayed: any, // tslint:disable-line:no-any
120124
) {
121125
const newCallback = (
122-
err: any, // tslint:disable-line:no-any
123-
filesModified: any, // tslint:disable-line:no-any
124-
contextModified: any, // tslint:disable-line:no-any
125-
missingModified: any, // tslint:disable-line:no-any
126+
err: Error | null,
127+
filesModified: string[],
128+
contextModified: string[],
129+
missingModified: string[],
126130
fileTimestamps: { [k: string]: number },
127131
contextTimestamps: { [k: string]: number },
128132
) => {
@@ -137,6 +141,30 @@ export class VirtualWatchFileSystemDecorator extends NodeWatchFileSystem {
137141
contextTimestamps);
138142
};
139143

140-
return super.watch(files, dirs, missing, startTime, options, newCallback, callbackUndelayed);
144+
const mapReplacements = (original: string[]): string[] => {
145+
if (!this._replacements) {
146+
return original;
147+
}
148+
const replacements = this._replacements;
149+
150+
return original.map(file => {
151+
const replacement = replacements.get(normalize(file));
152+
if (replacement) {
153+
return getSystemPath(replacement);
154+
} else {
155+
return file;
156+
}
157+
});
158+
};
159+
160+
return super.watch(
161+
mapReplacements(files),
162+
mapReplacements(dirs),
163+
mapReplacements(missing),
164+
startTime,
165+
options,
166+
newCallback,
167+
callbackUndelayed,
168+
);
141169
}
142170
}

0 commit comments

Comments
 (0)