Skip to content

Commit d463166

Browse files
filipesilvaKeen Yee Liau
authored and
Keen Yee Liau
committed
fix(@ngtools/webpack): correctly determine resource dependencies
Fix #16569
1 parent 5a1b190 commit d463166

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ describe('Browser Builder rebuilds', () => {
458458
expect(content).toContain('CSS_REBUILD_STRING');
459459
// Change the component css import.
460460
host.appendToFile(
461-
'src/app/app.component.css',
461+
'src/app/imported-styles.css',
462462
'CSS_DEP_REBUILD_STRING {color: #f00;}',
463463
);
464464
break;

packages/ngtools/webpack/src/angular_compiler_plugin.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1215,12 +1215,18 @@ export class AngularCompilerPlugin {
12151215
})
12161216
.filter(x => x) as string[];
12171217

1218-
let resourceImports: string[] = [], resourceDependencies: string[] = [];
1218+
let resourceImports: string[] = [];
1219+
const resourceDependencies: string[] = [];
12191220
if (includeResources) {
12201221
resourceImports = findResources(sourceFile)
12211222
.map(resourcePath => resolve(dirname(resolvedFileName), normalize(resourcePath)));
1222-
resourceDependencies =
1223-
this.getResourceDependencies(this._compilerHost.denormalizePath(resolvedFileName));
1223+
1224+
for (const resource of resourceImports) {
1225+
for (const dep of this.getResourceDependencies(
1226+
this._compilerHost.denormalizePath(resource))) {
1227+
resourceDependencies.push(dep);
1228+
}
1229+
}
12241230
}
12251231

12261232
// These paths are meant to be used by the loader so we must denormalize them.
@@ -1233,7 +1239,7 @@ export class AngularCompilerPlugin {
12331239
return [...uniqueDependencies];
12341240
}
12351241

1236-
getResourceDependencies(fileName: string): string[] {
1242+
getResourceDependencies(fileName: string) {
12371243
if (!this._resourceLoader) {
12381244
return [];
12391245
}

packages/ngtools/webpack/src/loader.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ export function ngcLoader(this: loader.LoaderContext) {
9696
const ngStyleRe = /(?:\.shim)?\.ngstyle\.js$/;
9797
if (ngStyleRe.test(sourceFileName)) {
9898
const styleFile = sourceFileName.replace(ngStyleRe, '');
99-
const styleDependencies = plugin.getResourceDependencies(styleFile);
100-
styleDependencies.forEach(dep => this.addDependency(dep));
99+
for (const dep of plugin.getResourceDependencies(styleFile)) {
100+
this.addDependency(dep);
101+
}
101102
}
102103

103104
// Add type-only dependencies that should trigger a rebuild when they change.

packages/ngtools/webpack/src/resource_loader.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import * as path from 'path';
1212
import * as vm from 'vm';
1313
import { RawSource } from 'webpack-sources';
14+
import { forwardSlashPath } from './utils';
1415

1516
const NodeTemplatePlugin = require('webpack/lib/node/NodeTemplatePlugin');
1617
const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
@@ -26,8 +27,8 @@ interface CompilationOutput {
2627
export class WebpackResourceLoader {
2728
private _parentCompilation: any;
2829
private _context: string;
29-
private _fileDependencies = new Map<string, string[]>();
30-
private _reverseDependencies = new Map<string, string[]>();
30+
private _fileDependencies = new Map<string, Set<string>>();
31+
private _reverseDependencies = new Map<string, Set<string>>();
3132
private _cachedSources = new Map<string, string>();
3233
private _cachedEvaluatedSources = new Map<string, RawSource>();
3334

@@ -129,13 +130,14 @@ export class WebpackResourceLoader {
129130
});
130131

131132
// Save the dependencies for this resource.
132-
this._fileDependencies.set(filePath, childCompilation.fileDependencies);
133+
this._fileDependencies.set(filePath, new Set(childCompilation.fileDependencies));
133134
for (const file of childCompilation.fileDependencies) {
134-
const entry = this._reverseDependencies.get(file);
135+
const resolvedFile = forwardSlashPath(file);
136+
const entry = this._reverseDependencies.get(resolvedFile);
135137
if (entry) {
136-
entry.push(filePath);
138+
entry.add(filePath);
137139
} else {
138-
this._reverseDependencies.set(file, [filePath]);
140+
this._reverseDependencies.set(resolvedFile, new Set([filePath]));
139141
}
140142
}
141143

0 commit comments

Comments
 (0)