fix(compiler): correctly calculate the out path on windows (#19601)
Fixes #19543 PR Close #19601
This commit is contained in:
parent
d7eac7ee56
commit
d30ce19231
@ -679,31 +679,37 @@ function getNgOptionDiagnostics(options: CompilerOptions): Diagnostic[] {
|
|||||||
* TODO(tbosch): talk to the TypeScript team to expose their logic for calculating the `rootDir`
|
* TODO(tbosch): talk to the TypeScript team to expose their logic for calculating the `rootDir`
|
||||||
* if none was specified.
|
* if none was specified.
|
||||||
*
|
*
|
||||||
|
* Note: This function works on normalized paths from typescript.
|
||||||
|
*
|
||||||
* @param outDir
|
* @param outDir
|
||||||
* @param outSrcMappings
|
* @param outSrcMappings
|
||||||
*/
|
*/
|
||||||
export function createSrcToOutPathMapper(
|
export function createSrcToOutPathMapper(
|
||||||
outDir: string | undefined, sampleSrcFileName: string | undefined,
|
outDir: string | undefined, sampleSrcFileName: string | undefined,
|
||||||
sampleOutFileName: string | undefined): (srcFileName: string) => string {
|
sampleOutFileName: string | undefined, host: {
|
||||||
|
dirname: typeof path.dirname,
|
||||||
|
resolve: typeof path.resolve,
|
||||||
|
relative: typeof path.relative
|
||||||
|
} = path): (srcFileName: string) => string {
|
||||||
let srcToOutPath: (srcFileName: string) => string;
|
let srcToOutPath: (srcFileName: string) => string;
|
||||||
if (outDir) {
|
if (outDir) {
|
||||||
if (sampleSrcFileName == null || sampleOutFileName == null) {
|
if (sampleSrcFileName == null || sampleOutFileName == null) {
|
||||||
throw new Error(`Can't calculate the rootDir without a sample srcFileName / outFileName. `);
|
throw new Error(`Can't calculate the rootDir without a sample srcFileName / outFileName. `);
|
||||||
}
|
}
|
||||||
const srcFileDir = path.dirname(sampleSrcFileName);
|
const srcFileDir = host.dirname(sampleSrcFileName).replace(/\\/g, '/');
|
||||||
const outFileDir = path.dirname(sampleOutFileName);
|
const outFileDir = host.dirname(sampleOutFileName).replace(/\\/g, '/');
|
||||||
if (srcFileDir === outFileDir) {
|
if (srcFileDir === outFileDir) {
|
||||||
return (srcFileName) => srcFileName;
|
return (srcFileName) => srcFileName;
|
||||||
}
|
}
|
||||||
const srcDirParts = srcFileDir.split(path.sep);
|
const srcDirParts = srcFileDir.split('/');
|
||||||
const outDirParts = outFileDir.split(path.sep);
|
const outDirParts = outFileDir.split('/');
|
||||||
// calculate the common suffix
|
// calculate the common suffix
|
||||||
let i = 0;
|
let i = 0;
|
||||||
while (i < Math.min(srcDirParts.length, outDirParts.length) &&
|
while (i < Math.min(srcDirParts.length, outDirParts.length) &&
|
||||||
srcDirParts[srcDirParts.length - 1 - i] === outDirParts[outDirParts.length - 1 - i])
|
srcDirParts[srcDirParts.length - 1 - i] === outDirParts[outDirParts.length - 1 - i])
|
||||||
i++;
|
i++;
|
||||||
const rootDir = srcDirParts.slice(0, srcDirParts.length - i).join(path.sep);
|
const rootDir = srcDirParts.slice(0, srcDirParts.length - i).join('/');
|
||||||
srcToOutPath = (srcFileName) => path.resolve(outDir, path.relative(rootDir, srcFileName));
|
srcToOutPath = (srcFileName) => host.resolve(outDir, host.relative(rootDir, srcFileName));
|
||||||
} else {
|
} else {
|
||||||
srcToOutPath = (srcFileName) => srcFileName;
|
srcToOutPath = (srcFileName) => srcFileName;
|
||||||
}
|
}
|
||||||
|
@ -495,5 +495,17 @@ describe('ng program', () => {
|
|||||||
const mapper = createSrcToOutPathMapper('/out', '/tmp/a/x.ts', '/a/x.js');
|
const mapper = createSrcToOutPathMapper('/out', '/tmp/a/x.ts', '/a/x.js');
|
||||||
expect(mapper('/tmp/b/y.js')).toBe('/out/b/y.js');
|
expect(mapper('/tmp/b/y.js')).toBe('/out/b/y.js');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should work on windows with normalized paths', () => {
|
||||||
|
const mapper =
|
||||||
|
createSrcToOutPathMapper('c:/tmp/out', 'c:/tmp/a/x.ts', 'c:/tmp/out/a/x.js', path.win32);
|
||||||
|
expect(mapper('c:/tmp/b/y.js')).toBe('c:\\tmp\\out\\b\\y.js');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should work on windows with non-normalized paths', () => {
|
||||||
|
const mapper = createSrcToOutPathMapper(
|
||||||
|
'c:\\tmp\\out', 'c:\\tmp\\a\\x.ts', 'c:\\tmp\\out\\a\\x.js', path.win32);
|
||||||
|
expect(mapper('c:\\tmp\\b\\y.js')).toBe('c:\\tmp\\out\\b\\y.js');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user