Skip to content

Commit

Permalink
fix(@ngtools/webpack): correctly determine resource dependencies
Browse files Browse the repository at this point in the history
Fix #16569
  • Loading branch information
filipesilva authored and Keen Yee Liau committed Jan 24, 2020
1 parent d5c21eb commit 0587153
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ describe('Browser Builder rebuilds', () => {
expect(content).toContain('CSS_REBUILD_STRING');
// Change the component css import.
host.appendToFile(
'src/app/app.component.css',
'src/app/imported-styles.css',
'CSS_DEP_REBUILD_STRING {color: #f00;}',
);
break;
Expand Down
14 changes: 10 additions & 4 deletions packages/ngtools/webpack/src/angular_compiler_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1215,12 +1215,18 @@ export class AngularCompilerPlugin {
})
.filter(x => x) as string[];

let resourceImports: string[] = [], resourceDependencies: string[] = [];
let resourceImports: string[] = [];
const resourceDependencies: string[] = [];
if (includeResources) {
resourceImports = findResources(sourceFile)
.map(resourcePath => resolve(dirname(resolvedFileName), normalize(resourcePath)));
resourceDependencies =
this.getResourceDependencies(this._compilerHost.denormalizePath(resolvedFileName));

for (const resource of resourceImports) {
for (const dep of this.getResourceDependencies(
this._compilerHost.denormalizePath(resource))) {
resourceDependencies.push(dep);
}
}
}

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

getResourceDependencies(fileName: string): string[] {
getResourceDependencies(fileName: string) {
if (!this._resourceLoader) {
return [];
}
Expand Down
5 changes: 3 additions & 2 deletions packages/ngtools/webpack/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ export function ngcLoader(this: loader.LoaderContext) {
const ngStyleRe = /(?:\.shim)?\.ngstyle\.js$/;
if (ngStyleRe.test(sourceFileName)) {
const styleFile = sourceFileName.replace(ngStyleRe, '');
const styleDependencies = plugin.getResourceDependencies(styleFile);
styleDependencies.forEach(dep => this.addDependency(dep));
for (const dep of plugin.getResourceDependencies(styleFile)) {
this.addDependency(dep);
}
}

// Add type-only dependencies that should trigger a rebuild when they change.
Expand Down
14 changes: 8 additions & 6 deletions packages/ngtools/webpack/src/resource_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import * as path from 'path';
import * as vm from 'vm';
import { RawSource } from 'webpack-sources';
import { forwardSlashPath } from './utils';

const NodeTemplatePlugin = require('webpack/lib/node/NodeTemplatePlugin');
const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
Expand All @@ -26,8 +27,8 @@ interface CompilationOutput {
export class WebpackResourceLoader {
private _parentCompilation: any;
private _context: string;
private _fileDependencies = new Map<string, string[]>();
private _reverseDependencies = new Map<string, string[]>();
private _fileDependencies = new Map<string, Set<string>>();
private _reverseDependencies = new Map<string, Set<string>>();
private _cachedSources = new Map<string, string>();
private _cachedEvaluatedSources = new Map<string, RawSource>();

Expand Down Expand Up @@ -129,13 +130,14 @@ export class WebpackResourceLoader {
});

// Save the dependencies for this resource.
this._fileDependencies.set(filePath, childCompilation.fileDependencies);
this._fileDependencies.set(filePath, new Set(childCompilation.fileDependencies));
for (const file of childCompilation.fileDependencies) {
const entry = this._reverseDependencies.get(file);
const resolvedFile = forwardSlashPath(file);
const entry = this._reverseDependencies.get(resolvedFile);
if (entry) {
entry.push(filePath);
entry.add(filePath);
} else {
this._reverseDependencies.set(file, [filePath]);
this._reverseDependencies.set(resolvedFile, new Set([filePath]));
}
}

Expand Down

0 comments on commit 0587153

Please sign in to comment.