refactor(ngcc): resolve modules based on the provided `moduleResolver` (#34494)

The `DependencyHost` implementations were duplicating the "postfix" strings
which are used to find matching paths when resolving module specifiers.
Now the hosts reuse the postfixes given to the `ModuleResolver` that is
passed to the host.

PR Close #34494
This commit is contained in:
Pete Bacon Darwin 2019-12-19 22:43:12 +00:00 committed by Alex Rickabaugh
parent e2b184515b
commit 69950e3888
3 changed files with 36 additions and 2 deletions

View File

@ -39,7 +39,7 @@ export abstract class DependencyHostBase implements DependencyHost {
collectDependencies(
entryPointPath: AbsoluteFsPath, {dependencies, missing, deepImports}: DependencyInfo): void {
const resolvedFile =
resolveFileWithPostfixes(this.fs, entryPointPath, ['', '.js', '/index.js']);
resolveFileWithPostfixes(this.fs, entryPointPath, this.moduleResolver.relativeExtensions);
if (resolvedFile !== null) {
const alreadySeen = new Set<AbsoluteFsPath>();
this.recursivelyCollectDependencies(

View File

@ -24,7 +24,7 @@ import {PathMappings, isRelativePath, resolveFileWithPostfixes} from '../utils';
export class ModuleResolver {
private pathMappings: ProcessedPathMapping[];
constructor(private fs: FileSystem, pathMappings?: PathMappings, private relativeExtensions = [
constructor(private fs: FileSystem, pathMappings?: PathMappings, readonly relativeExtensions = [
'', '.js', '/index.js'
]) {
this.pathMappings = pathMappings ? this.processPathMappings(pathMappings) : [];

View File

@ -135,6 +135,40 @@ runInEachFileSystem(() => {
expect(dependencies.has(_('/node_modules/lib-1'))).toBe(true);
expect(dependencies.has(_('/node_modules/lib-1/sub-1'))).toBe(true);
});
it('should resolve modules based on the provided `moduleResolver`', () => {
const fs = getFileSystem();
loadTestFiles([
{
name: _('/external/index.d.ts'),
contents: `import * from './internal-typings';`,
},
{
name: _('/external/internal-typings.d.ts'),
contents: `export {X} from 'lib-1';\nexport {Y} from 'lib-1/sub-1';`,
}
]);
// Default JS mode will not pick up `internal-typings.d.ts` dependency
const jsHost = new EsmDependencyHost(fs, new ModuleResolver(fs));
const jsDeps = createDependencyInfo();
jsHost.collectDependencies(_('/external/index.d.ts'), jsDeps);
expect(jsDeps.dependencies.size).toEqual(0);
expect(jsDeps.deepImports.size).toEqual(0);
expect(jsDeps.missing.size).toEqual(1);
expect(jsDeps.missing.has(relativeFrom('./internal-typings'))).toBeTruthy();
// Typings mode will pick up `internal-typings.d.ts` dependency
const dtsHost = new EsmDependencyHost(
fs, new ModuleResolver(fs, undefined, ['', '.d.ts', 'index.d.ts']));
const dtsDeps = createDependencyInfo();
dtsHost.collectDependencies(_('/external/index.d.ts'), dtsDeps);
expect(dtsDeps.dependencies.size).toEqual(2);
expect(dtsDeps.dependencies.has(_('/node_modules/lib-1'))).toBeTruthy();
expect(dtsDeps.dependencies.has(_('/node_modules/lib-1/sub-1'))).toBeTruthy();
expect(dtsDeps.deepImports.size).toEqual(0);
expect(dtsDeps.missing.size).toEqual(0);
});
});
function setupMockFileSystem(): void {