Skip to content

Commit 99ee7c0

Browse files
Alanmgechev
authored andcommitted
fix(@ngtools/webpack): changes in non module code are not picked up when using barrel files
Fixes #13975
1 parent 898b436 commit 99ee7c0

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

packages/angular_devkit/build_angular/test/browser/rebuild_spec_large.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,4 +338,42 @@ describe('Browser Builder rebuilds', () => {
338338
take(7),
339339
).toPromise().then(done, done.fail);
340340
});
341+
342+
it('rebuilds on changes in barrel file dependency', async () => {
343+
host.writeMultipleFiles({
344+
'src/index.ts': `export * from './interface'`,
345+
'src/interface.ts': `export interface Foo { bar: boolean };`,
346+
});
347+
host.appendToFile('src/main.ts', `
348+
import { Foo } from './index';
349+
const x: Foo = { bar: true };
350+
`);
351+
352+
const overrides = { watch: true, aot: false };
353+
let buildNumber = 0;
354+
const run = await architect.scheduleTarget(target, overrides);
355+
await run.output.pipe(
356+
debounceTime(1000),
357+
tap((buildEvent) => {
358+
buildNumber += 1;
359+
switch (buildNumber) {
360+
case 1:
361+
expect(buildEvent.success).toBe(true);
362+
host.writeMultipleFiles({
363+
'src/interface.ts': `export interface Foo {
364+
bar: boolean;
365+
baz?: string;
366+
};`,
367+
});
368+
break;
369+
370+
case 2:
371+
expect(buildEvent.success).toBe(true);
372+
break;
373+
}
374+
}),
375+
take(2),
376+
).toPromise();
377+
await run.stop();
378+
});
341379
});

packages/ngtools/webpack/src/angular_compiler_plugin.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,9 +1047,18 @@ export class AngularCompilerPlugin {
10471047
const host = this._compilerHost;
10481048
const cache = this._moduleResolutionCache;
10491049

1050-
const esImports = collectDeepNodes<ts.ImportDeclaration>(sourceFile,
1051-
ts.SyntaxKind.ImportDeclaration)
1050+
const esImports = collectDeepNodes<ts.ImportDeclaration | ts.ExportDeclaration>(
1051+
sourceFile,
1052+
[
1053+
ts.SyntaxKind.ImportDeclaration,
1054+
ts.SyntaxKind.ExportDeclaration,
1055+
],
1056+
)
10521057
.map(decl => {
1058+
if (!decl.moduleSpecifier) {
1059+
return null;
1060+
}
1061+
10531062
const moduleName = (decl.moduleSpecifier as ts.StringLiteral).text;
10541063
const resolved = ts.resolveModuleName(moduleName, resolvedFileName, options, host, cache);
10551064

packages/ngtools/webpack/src/loader.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ export function ngcLoader(this: loader.LoaderContext) {
6868
if (sourceFileName.endsWith('.ts')) {
6969
result.errorDependencies.forEach(dep => this.addDependency(dep));
7070
const dependencies = plugin.getDependencies(sourceFileName);
71-
dependencies.forEach(dep => {
71+
dependencies
72+
.filter(d => d.endsWith('index.ts'))
73+
.forEach(d => dependencies.push(...plugin.getDependencies(d)));
74+
75+
[...new Set(dependencies)].forEach(dep => {
7276
plugin.updateChangedFileExtensions(path.extname(dep));
7377
this.addDependency(dep);
7478
});

packages/ngtools/webpack/src/transformers/ast_helpers.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ import { WebpackCompilerHost } from '../compiler_host';
1111

1212

1313
// Find all nodes from the AST in the subtree of node of SyntaxKind kind.
14-
export function collectDeepNodes<T extends ts.Node>(node: ts.Node, kind: ts.SyntaxKind): T[] {
14+
export function collectDeepNodes<T extends ts.Node>(
15+
node: ts.Node,
16+
kind: ts.SyntaxKind | ts.SyntaxKind[],
17+
): T[] {
18+
const kinds = Array.isArray(kind) ? kind : [kind];
1519
const nodes: T[] = [];
1620
const helper = (child: ts.Node) => {
17-
if (child.kind === kind) {
21+
if (kinds.includes(child.kind)) {
1822
nodes.push(child as T);
1923
}
2024
ts.forEachChild(child, helper);

0 commit comments

Comments
 (0)