Skip to content

Commit

Permalink
fix(@ngtools/webpack): display unused file warning once per file
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-agius4 authored and Keen Yee Liau committed Aug 6, 2019
1 parent 284de6c commit 6e9514b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { debounceTime, take, tap } from 'rxjs/operators';
import { BrowserBuilderOutput } from '../../src/browser/index';
import { createArchitect, host, ivyEnabled } from '../utils';


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

it('should only show warning once per file', async () => {
if (!ivyEnabled) {
// TODO: https://github.com/angular/angular-cli/issues/15056
pending('Only supported in Ivy.');

return;
}

host.replaceInFile(
'src/tsconfig.app.json',
'"**/*.d.ts"',
'"**/*.d.ts", "testing/**/*.ts"',
);

// Write a used file
host.writeMultipleFiles({
'src/testing/type.ts': 'export type MyType = number;',
});

const logger = new TestLogger('unused-files-warnings');
let buildNumber = 0;
const run = await architect.scheduleTarget(targetSpec, { watch: true }, { logger });

await run.output
.pipe(
debounceTime(1000),
tap(buildEvent => {
expect(buildEvent.success).toBe(true);

buildNumber ++;
switch (buildNumber) {
case 1:
// The first should have type.ts as unused.
expect(logger.includes(`type.ts ${warningMessageSuffix}`)).toBe(true, `Case ${buildNumber} failed.`);

// touch a file to trigger a rebuild
host.appendToFile('src/main.ts', '');
break;
case 2:
// The second should should have type.ts as unused but shouldn't warn.
expect(logger.includes(warningMessageSuffix)).toBe(false, `Case ${buildNumber} failed.`);
break;
}

logger.clear();
}),
take(2),
)
.toPromise();
await run.stop();
});

});
17 changes: 12 additions & 5 deletions packages/ngtools/webpack/src/angular_compiler_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export class AngularCompilerPlugin {
// This is needed because if the first build fails we need to do a full emit
// even whe only a single file gets updated.
private _hadFullJitEmit: boolean | undefined;
private _unusedFiles = new Set<string>();
private _changedFileExtensions = new Set(['ts', 'tsx', 'html', 'css', 'js', 'json']);

// Webpack plugin.
Expand Down Expand Up @@ -626,12 +627,18 @@ export class AngularCompilerPlugin {
}
}

const unusedFilesWarning = program.getSourceFiles()
.filter(({ fileName }) => !fileExcludeRegExp.test(fileName) && !usedFiles.has(fileName))
.map(({ fileName }) => `${fileName} is part of the TypeScript compilation but it's unused.`);
const sourceFiles = program.getSourceFiles();
for (const { fileName } of sourceFiles) {
if (
fileExcludeRegExp.test(fileName)
|| usedFiles.has(fileName)
|| this._unusedFiles.has(fileName)
) {
continue;
}

if (unusedFilesWarning.length) {
compilation.warnings.push(...unusedFilesWarning);
compilation.warnings.push(`${fileName} is part of the TypeScript compilation but it's unused.`);
this._unusedFiles.add(fileName);
}
}

Expand Down

0 comments on commit 6e9514b

Please sign in to comment.