From 0a8a7972682f69013e22b48ce46e6e324e8be172 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Mon, 9 Jul 2018 09:18:40 -0400 Subject: [PATCH] refactor(@ngtools/webpack): support file replacement without copying --- .../webpack/src/angular_compiler_plugin.ts | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/ngtools/webpack/src/angular_compiler_plugin.ts b/packages/ngtools/webpack/src/angular_compiler_plugin.ts index 0de82c0e15..6bc000985b 100644 --- a/packages/ngtools/webpack/src/angular_compiler_plugin.ts +++ b/packages/ngtools/webpack/src/angular_compiler_plugin.ts @@ -6,6 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ import { dirname, normalize, resolve, virtualFs } from '@angular-devkit/core'; +import { NodeJsSyncHost } from '@angular-devkit/core/node'; import { ChildProcess, ForkOptions, fork } from 'child_process'; import * as fs from 'fs'; import * as path from 'path'; @@ -288,11 +289,21 @@ export class AngularCompilerPlugin { this._contextElementDependencyConstructor = options.contextElementDependencyConstructor || require('webpack/lib/dependencies/ContextElementDependency'); + + let host: virtualFs.Host = options.host || new NodeJsSyncHost(); + if (options.hostReplacementPaths) { + const aliasHost = new virtualFs.AliasHost(host); + for (const from in options.hostReplacementPaths) { + aliasHost.aliases.set(normalize(from), normalize(options.hostReplacementPaths[from])); + } + host = aliasHost; + } + // Create the webpack compiler host. const webpackCompilerHost = new WebpackCompilerHost( this._compilerOptions, this._basePath, - this._options.host, + host, ); webpackCompilerHost.enableCaching(); @@ -306,17 +317,6 @@ export class AngularCompilerPlugin { tsHost: webpackCompilerHost, }) as CompilerHost & WebpackCompilerHost; - // Override some files in the FileSystem with paths from the actual file system. - if (this._options.hostReplacementPaths) { - for (const filePath of Object.keys(this._options.hostReplacementPaths)) { - const replacementFilePath = this._options.hostReplacementPaths[filePath]; - const content = this._compilerHost.readFile(replacementFilePath); - if (content) { - this._compilerHost.writeFile(filePath, content, false); - } - } - } - // Resolve mainPath if provided. if (options.mainPath) { this._mainPath = this._compilerHost.resolve(options.mainPath);