Skip to content

Commit 6e9514b

Browse files
alan-agius4Keen Yee Liau
authored and
Keen Yee Liau
committed
fix(@ngtools/webpack): display unused file warning once per file
1 parent 284de6c commit 6e9514b

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

packages/angular_devkit/build_angular/test/browser/unused-files-warning_spec_large.ts

+53-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { debounceTime, take, tap } from 'rxjs/operators';
1111
import { BrowserBuilderOutput } from '../../src/browser/index';
1212
import { createArchitect, host, ivyEnabled } from '../utils';
1313

14-
14+
// tslint:disable-next-line:no-big-function
1515
describe('Browser Builder unused files warnings', () => {
1616
const warningMessageSuffix = `is part of the TypeScript compilation but it's unused`;
1717
const targetSpec = { project: 'app', target: 'build' };
@@ -162,4 +162,56 @@ describe('Browser Builder unused files warnings', () => {
162162
await run.stop();
163163
});
164164

165+
it('should only show warning once per file', async () => {
166+
if (!ivyEnabled) {
167+
// TODO: https://github.com/angular/angular-cli/issues/15056
168+
pending('Only supported in Ivy.');
169+
170+
return;
171+
}
172+
173+
host.replaceInFile(
174+
'src/tsconfig.app.json',
175+
'"**/*.d.ts"',
176+
'"**/*.d.ts", "testing/**/*.ts"',
177+
);
178+
179+
// Write a used file
180+
host.writeMultipleFiles({
181+
'src/testing/type.ts': 'export type MyType = number;',
182+
});
183+
184+
const logger = new TestLogger('unused-files-warnings');
185+
let buildNumber = 0;
186+
const run = await architect.scheduleTarget(targetSpec, { watch: true }, { logger });
187+
188+
await run.output
189+
.pipe(
190+
debounceTime(1000),
191+
tap(buildEvent => {
192+
expect(buildEvent.success).toBe(true);
193+
194+
buildNumber ++;
195+
switch (buildNumber) {
196+
case 1:
197+
// The first should have type.ts as unused.
198+
expect(logger.includes(`type.ts ${warningMessageSuffix}`)).toBe(true, `Case ${buildNumber} failed.`);
199+
200+
// touch a file to trigger a rebuild
201+
host.appendToFile('src/main.ts', '');
202+
break;
203+
case 2:
204+
// The second should should have type.ts as unused but shouldn't warn.
205+
expect(logger.includes(warningMessageSuffix)).toBe(false, `Case ${buildNumber} failed.`);
206+
break;
207+
}
208+
209+
logger.clear();
210+
}),
211+
take(2),
212+
)
213+
.toPromise();
214+
await run.stop();
215+
});
216+
165217
});

packages/ngtools/webpack/src/angular_compiler_plugin.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export class AngularCompilerPlugin {
111111
// This is needed because if the first build fails we need to do a full emit
112112
// even whe only a single file gets updated.
113113
private _hadFullJitEmit: boolean | undefined;
114+
private _unusedFiles = new Set<string>();
114115
private _changedFileExtensions = new Set(['ts', 'tsx', 'html', 'css', 'js', 'json']);
115116

116117
// Webpack plugin.
@@ -626,12 +627,18 @@ export class AngularCompilerPlugin {
626627
}
627628
}
628629

629-
const unusedFilesWarning = program.getSourceFiles()
630-
.filter(({ fileName }) => !fileExcludeRegExp.test(fileName) && !usedFiles.has(fileName))
631-
.map(({ fileName }) => `${fileName} is part of the TypeScript compilation but it's unused.`);
630+
const sourceFiles = program.getSourceFiles();
631+
for (const { fileName } of sourceFiles) {
632+
if (
633+
fileExcludeRegExp.test(fileName)
634+
|| usedFiles.has(fileName)
635+
|| this._unusedFiles.has(fileName)
636+
) {
637+
continue;
638+
}
632639

633-
if (unusedFilesWarning.length) {
634-
compilation.warnings.push(...unusedFilesWarning);
640+
compilation.warnings.push(`${fileName} is part of the TypeScript compilation but it's unused.`);
641+
this._unusedFiles.add(fileName);
635642
}
636643
}
637644

0 commit comments

Comments
 (0)