|
5 | 5 | * Use of this source code is governed by an MIT-style license that can be
|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 |
| -import { Path, dirname, normalize, resolve, virtualFs } from '@angular-devkit/core'; |
| 8 | +import { Path, dirname, getSystemPath, normalize, resolve, virtualFs } from '@angular-devkit/core'; |
9 | 9 | import { NodeJsSyncHost } from '@angular-devkit/core/node';
|
10 | 10 | import { ChildProcess, ForkOptions, fork } from 'child_process';
|
11 | 11 | import * as fs from 'fs';
|
@@ -77,7 +77,7 @@ export interface AngularCompilerPluginOptions {
|
77 | 77 | entryModule?: string;
|
78 | 78 | mainPath?: string;
|
79 | 79 | skipCodeGeneration?: boolean;
|
80 |
| - hostReplacementPaths?: { [path: string]: string }; |
| 80 | + hostReplacementPaths?: { [path: string]: string } | ((path: string) => string); |
81 | 81 | forkTypeChecker?: boolean;
|
82 | 82 | // TODO: remove singleFileIncludes for 2.0, this is just to support old projects that did not
|
83 | 83 | // include 'polyfills.ts' in `tsconfig.spec.json'.
|
@@ -292,11 +292,20 @@ export class AngularCompilerPlugin {
|
292 | 292 |
|
293 | 293 | let host: virtualFs.Host<fs.Stats> = options.host || new NodeJsSyncHost();
|
294 | 294 | if (options.hostReplacementPaths) {
|
295 |
| - const aliasHost = new virtualFs.AliasHost(host); |
296 |
| - for (const from in options.hostReplacementPaths) { |
297 |
| - aliasHost.aliases.set(normalize(from), normalize(options.hostReplacementPaths[from])); |
| 295 | + if (typeof options.hostReplacementPaths == 'function') { |
| 296 | + const replacementResolver = options.hostReplacementPaths; |
| 297 | + host = new class extends virtualFs.ResolverHost<fs.Stats> { |
| 298 | + _resolve(path: Path) { |
| 299 | + return normalize(replacementResolver(getSystemPath(path))); |
| 300 | + } |
| 301 | + }(host); |
| 302 | + } else { |
| 303 | + const aliasHost = new virtualFs.AliasHost(host); |
| 304 | + for (const from in options.hostReplacementPaths) { |
| 305 | + aliasHost.aliases.set(normalize(from), normalize(options.hostReplacementPaths[from])); |
| 306 | + } |
| 307 | + host = aliasHost; |
298 | 308 | }
|
299 |
| - host = aliasHost; |
300 | 309 | }
|
301 | 310 |
|
302 | 311 | // Create the webpack compiler host.
|
@@ -618,14 +627,19 @@ export class AngularCompilerPlugin {
|
618 | 627 | );
|
619 | 628 | compilerWithFileSystems.inputFileSystem = inputDecorator;
|
620 | 629 |
|
621 |
| - let replacements: Map<Path, Path> | undefined; |
| 630 | + let replacements: Map<Path, Path> | ((path: Path) => Path) | undefined; |
622 | 631 | 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 |
| - ); |
| 632 | + if (typeof this._options.hostReplacementPaths === 'function') { |
| 633 | + const replacementResolver = this._options.hostReplacementPaths; |
| 634 | + replacements = path => normalize(replacementResolver(getSystemPath(path))); |
| 635 | + } else { |
| 636 | + replacements = new Map(); |
| 637 | + for (const replace in this._options.hostReplacementPaths) { |
| 638 | + replacements.set( |
| 639 | + normalize(replace), |
| 640 | + normalize(this._options.hostReplacementPaths[replace]), |
| 641 | + ); |
| 642 | + } |
629 | 643 | }
|
630 | 644 | }
|
631 | 645 |
|
|
0 commit comments