fix(ivy): ngcc - resolve main property paths correctly (#31509)

When determining if a `main` path points to a UMD or CommonJS
format, the contents of the file need to be loaded and parsed.

Previously, it was assumed that the path referred to the exact filename,
but did not account for normal module resolution semantics, where the
path may be missing an extension or refer to a directory containing an
`index.js` file.

// FW-1444

PR Close #31509
This commit is contained in:
Pete Bacon Darwin
2019-07-11 12:34:45 +01:00
committed by Matias Niemelä
parent fe1793844d
commit 103a5b42ec
6 changed files with 137 additions and 27 deletions

View File

@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import * as ts from 'typescript';
import {AbsoluteFsPath, FileSystem, absoluteFrom} from '../../src/ngtsc/file_system';
export function getOriginalSymbol(checker: ts.TypeChecker): (symbol: ts.Symbol) => ts.Symbol {
return function(symbol: ts.Symbol) {
@ -65,3 +66,19 @@ export type PathMappings = {
export function isRelativePath(path: string): boolean {
return /^\/|^\.\.?($|\/)/.test(path);
}
/**
* Attempt to resolve a `path` to a file by appending the provided `postFixes`
* to the `path` and checking if the file exists on disk.
* @returns An absolute path to the first matching existing file, or `null` if none exist.
*/
export function resolveFileWithPostfixes(
fs: FileSystem, path: AbsoluteFsPath, postFixes: string[]): AbsoluteFsPath|null {
for (const postFix of postFixes) {
const testPath = absoluteFrom(path + postFix);
if (fs.exists(testPath) && fs.stat(testPath).isFile()) {
return testPath;
}
}
return null;
}