Skip to content

Commit

Permalink
fix(@ngtools/webpack): changes in non module code are not picked up w…
Browse files Browse the repository at this point in the history
…hen using barrel files

Fixes #13975
  • Loading branch information
Alan authored and mgechev committed Mar 29, 2019
1 parent 898b436 commit 99ee7c0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
Expand Up @@ -338,4 +338,42 @@ describe('Browser Builder rebuilds', () => {
take(7),
).toPromise().then(done, done.fail);
});

it('rebuilds on changes in barrel file dependency', async () => {
host.writeMultipleFiles({
'src/index.ts': `export * from './interface'`,
'src/interface.ts': `export interface Foo { bar: boolean };`,
});
host.appendToFile('src/main.ts', `
import { Foo } from './index';
const x: Foo = { bar: true };
`);

const overrides = { watch: true, aot: false };
let buildNumber = 0;
const run = await architect.scheduleTarget(target, overrides);
await run.output.pipe(
debounceTime(1000),
tap((buildEvent) => {
buildNumber += 1;
switch (buildNumber) {
case 1:
expect(buildEvent.success).toBe(true);
host.writeMultipleFiles({
'src/interface.ts': `export interface Foo {
bar: boolean;
baz?: string;
};`,
});
break;

case 2:
expect(buildEvent.success).toBe(true);
break;
}
}),
take(2),
).toPromise();
await run.stop();
});
});
13 changes: 11 additions & 2 deletions packages/ngtools/webpack/src/angular_compiler_plugin.ts
Expand Up @@ -1047,9 +1047,18 @@ export class AngularCompilerPlugin {
const host = this._compilerHost;
const cache = this._moduleResolutionCache;

const esImports = collectDeepNodes<ts.ImportDeclaration>(sourceFile,
ts.SyntaxKind.ImportDeclaration)
const esImports = collectDeepNodes<ts.ImportDeclaration | ts.ExportDeclaration>(
sourceFile,
[
ts.SyntaxKind.ImportDeclaration,
ts.SyntaxKind.ExportDeclaration,
],
)
.map(decl => {
if (!decl.moduleSpecifier) {
return null;
}

const moduleName = (decl.moduleSpecifier as ts.StringLiteral).text;
const resolved = ts.resolveModuleName(moduleName, resolvedFileName, options, host, cache);

Expand Down
6 changes: 5 additions & 1 deletion packages/ngtools/webpack/src/loader.ts
Expand Up @@ -68,7 +68,11 @@ export function ngcLoader(this: loader.LoaderContext) {
if (sourceFileName.endsWith('.ts')) {
result.errorDependencies.forEach(dep => this.addDependency(dep));
const dependencies = plugin.getDependencies(sourceFileName);
dependencies.forEach(dep => {
dependencies
.filter(d => d.endsWith('index.ts'))
.forEach(d => dependencies.push(...plugin.getDependencies(d)));

[...new Set(dependencies)].forEach(dep => {
plugin.updateChangedFileExtensions(path.extname(dep));
this.addDependency(dep);
});
Expand Down
8 changes: 6 additions & 2 deletions packages/ngtools/webpack/src/transformers/ast_helpers.ts
Expand Up @@ -11,10 +11,14 @@ import { WebpackCompilerHost } from '../compiler_host';


// Find all nodes from the AST in the subtree of node of SyntaxKind kind.
export function collectDeepNodes<T extends ts.Node>(node: ts.Node, kind: ts.SyntaxKind): T[] {
export function collectDeepNodes<T extends ts.Node>(
node: ts.Node,
kind: ts.SyntaxKind | ts.SyntaxKind[],
): T[] {
const kinds = Array.isArray(kind) ? kind : [kind];
const nodes: T[] = [];
const helper = (child: ts.Node) => {
if (child.kind === kind) {
if (kinds.includes(child.kind)) {
nodes.push(child as T);
}
ts.forEachChild(child, helper);
Expand Down

0 comments on commit 99ee7c0

Please sign in to comment.