From e92dc20ce23dbd13bd9a4b97301098e3e7bb9df7 Mon Sep 17 00:00:00 2001 From: SilverMira <66930495+SilverMira@users.noreply.github.com> Date: Sun, 3 Jan 2021 16:52:11 +0800 Subject: [PATCH] (feat): Production file replacements --- src/compilerService.ts | 21 +++++++++++++++++++++ src/configParser.ts | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/compilerService.ts b/src/compilerService.ts index 6043725..f3d36c0 100644 --- a/src/compilerService.ts +++ b/src/compilerService.ts @@ -51,6 +51,7 @@ export class AngularCompilerService { private _typeCheckErrorListenerId = 0; private _typeCheckWorker?: TypeCheckWorker; private _lastTypeCheckResult: ng.Diagnostics = []; + private _fileReplacements = new Map(); constructor( angularJson: string, @@ -94,6 +95,26 @@ export class AngularCompilerService { const contents = await fsp.readFile(fileName, 'utf-8'); return contents; }; + const oriReadFile = host.readFile; + if (process.env.NODE_ENV === 'production') { + const replacementConfig = this.angularConfig.getProject( + this.angularProject + ).architect.build.configurations.production.fileReplacements; + replacementConfig.forEach((replacement) => { + this._fileReplacements.set( + path.resolve(replacement.replace), + path.resolve(replacement.with) + ); + }); + } + host.readFile = (fileName) => { + fileName = path.resolve(fileName); + if (this._fileReplacements.has(fileName)) { + const replaceWith = this._fileReplacements.get(fileName)!; + fileName = replaceWith; + } + return oriReadFile(fileName); + }; return host; } diff --git a/src/configParser.ts b/src/configParser.ts index 27498ef..3ef424d 100644 --- a/src/configParser.ts +++ b/src/configParser.ts @@ -24,7 +24,7 @@ export interface AngularArchitectSubset { styles: string[]; scripts: string[]; }; - configuration: { + configurations: { [configurationName: string]: AngularArchitectConfig; }; }