From 0b64bee5da3a43b31fcebe661150276c2b93cf52 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Mon, 11 May 2020 21:55:13 +0200 Subject: [PATCH] fix(@ngtools/webpack): getCanonicalFileName should return FS compatible paths Unlike TSC which has it's own mechanism the resolve and join paths in POSIX format, NGTSC heavily relies onNode.JS `fs` and `path` modules. This prevents `Path` usage because in Windows `path.resolve` will causes an absolute path to be resolved or joined incorrectly. Example: `/D/MyPath/MyProject` -> `D:/d/mypath/myproject`. With this change we change the `getCanonicalFileName` method to return FS compatible paths. (cherry picked from commit e8e832e5a8b8e0311725bcdd60b6990e6d6c0236) --- packages/ngtools/webpack/src/compiler_host.ts | 2 +- packages/ngtools/webpack/src/utils.ts | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/ngtools/webpack/src/compiler_host.ts b/packages/ngtools/webpack/src/compiler_host.ts index eaf9c0421c28..302670628b79 100644 --- a/packages/ngtools/webpack/src/compiler_host.ts +++ b/packages/ngtools/webpack/src/compiler_host.ts @@ -359,7 +359,7 @@ export class WebpackCompilerHost implements ts.CompilerHost { } getCanonicalFileName(fileName: string): string { - const path = this.resolve(fileName); + const path = workaroundResolve(this.resolve(fileName)); return this.useCaseSensitiveFileNames() ? path : path.toLowerCase(); } diff --git a/packages/ngtools/webpack/src/utils.ts b/packages/ngtools/webpack/src/utils.ts index db4336ada4b2..e3f4c0796c2b 100644 --- a/packages/ngtools/webpack/src/utils.ts +++ b/packages/ngtools/webpack/src/utils.ts @@ -9,9 +9,11 @@ import { Path, getSystemPath, normalize } from '@angular-devkit/core'; // `TsCompilerAotCompilerTypeCheckHostAdapter` in @angular/compiler-cli seems to resolve module // names directly via `resolveModuleName`, which prevents full Path usage. +// NSTSC also uses Node.JS `path.resolve` which will result in incorrect paths in Windows +// Example: `/D/MyPath/MyProject` -> `D:/d/mypath/myproject` // To work around this we must provide the same path format as TS internally uses in // the SourceFile paths. -export function workaroundResolve(path: Path | string) { +export function workaroundResolve(path: Path | string): string { return forwardSlashPath(getSystemPath(normalize(path))); } @@ -20,6 +22,6 @@ export function flattenArray(value: Array): T[] { } // TS represents paths internally with '/' and expects paths to be in this format. -export function forwardSlashPath(path: string) { +export function forwardSlashPath(path: string): string { return path.replace(/\\/g, '/'); }