fix(ngcc): capture dynamic import expressions as well as declarations (#37075)

Previously we only checked for static import declaration statements.
This commit also finds import paths from dynamic import expressions.

Also this commit should speed up processing: Previously we were parsing
the source code contents into a `ts.SourceFile` and then walking the parsed
AST to find import paths.
Generating an AST is unnecessary work and it is faster and creates less
memory pressure to just scan the source code contents with the TypeScript
scanner, identifying import paths from the tokens.

PR Close #37075
This commit is contained in:
Pete Bacon Darwin
2020-06-04 08:43:04 +01:00
committed by atscott
parent 4d69da57ca
commit 07a8016118
2 changed files with 233 additions and 9 deletions

View File

@ -57,6 +57,17 @@ runInEachFileSystem(() => {
expect(dependencies.has(_('/node_modules/lib-1/sub-1'))).toBe(true);
});
it('should resolve all the external dynamic imports of the source file', () => {
const {dependencies, missing, deepImports} = createDependencyInfo();
host.collectDependencies(
_('/external/dynamic/index.js'), {dependencies, missing, deepImports});
expect(dependencies.size).toBe(2);
expect(missing.size).toBe(0);
expect(deepImports.size).toBe(0);
expect(dependencies.has(_('/node_modules/lib-1'))).toBe(true);
expect(dependencies.has(_('/node_modules/lib-1/sub-1'))).toBe(true);
});
it('should capture missing external imports', () => {
const {dependencies, missing, deepImports} = createDependencyInfo();
host.collectDependencies(
@ -184,6 +195,13 @@ runInEachFileSystem(() => {
},
{name: _('/external/imports/package.json'), contents: '{"esm2015": "./index.js"}'},
{name: _('/external/imports/index.metadata.json'), contents: 'MOCK METADATA'},
{
name: _('/external/dynamic/index.js'),
contents:
`async function foo() { await const x = import('lib-1');\n const promise = import('lib-1/sub-1'); }`
},
{name: _('/external/dynamic/package.json'), contents: '{"esm2015": "./index.js"}'},
{name: _('/external/dynamic/index.metadata.json'), contents: 'MOCK METADATA'},
{
name: _('/external/re-exports/index.js'),
contents: `export {X} from 'lib-1';\nexport {\n Y,\n Z\n} from 'lib-1/sub-1';`