diff --git a/package.json b/package.json index bec26d17..7aec009b 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "@types/babel-code-frame": "^6.20.1", "@types/chokidar": "^1.7.5", "@types/lodash": "^4.14.117", + "@types/micromatch": "^3.1.0", "@types/minimatch": "^3.0.1", "@types/node": "^8.0.26", "@types/resolve": "0.0.4", @@ -88,6 +89,7 @@ "chalk": "^2.4.1", "chokidar": "^2.0.4", "lodash": "^4.17.11", + "micromatch": "^3.1.10", "minimatch": "^3.0.4", "resolve": "^1.5.0", "tapable": "^1.0.0" diff --git a/src/index.ts b/src/index.ts index 52bf7a5c..aac5abd2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import * as process from 'process'; import * as childProcess from 'child_process'; import chalk, { Chalk } from 'chalk'; import * as fs from 'fs'; +import * as micromatch from 'micromatch'; import * as os from 'os'; import * as webpack from 'webpack'; import isString = require('lodash/isString'); @@ -45,6 +46,7 @@ interface Options { async: boolean; ignoreDiagnostics: number[]; ignoreLints: string[]; + reportFiles: string[]; colors: boolean; logger: Logger; formatter: 'default' | 'codeframe' | Formatter; @@ -77,6 +79,7 @@ class ForkTsCheckerWebpackPlugin { watch: string[]; ignoreDiagnostics: number[]; ignoreLints: string[]; + reportFiles: string[]; logger: Logger; silent: boolean; async: boolean; @@ -130,6 +133,7 @@ class ForkTsCheckerWebpackPlugin { : options.watch || []; this.ignoreDiagnostics = options.ignoreDiagnostics || []; this.ignoreLints = options.ignoreLints || []; + this.reportFiles = options.reportFiles || []; this.logger = options.logger || console; this.silent = options.silent === true; // default false this.async = options.async !== false; // default true @@ -674,6 +678,26 @@ class ForkTsCheckerWebpackPlugin { ); } + if (this.reportFiles.length) { + const reportFilesPredicate = (diagnostic: NormalizedMessage): boolean => { + if (diagnostic.file) { + const relativeFileName = path.relative( + this.compiler.options.context, + diagnostic.file + ); + const matchResult = micromatch([relativeFileName], this.reportFiles); + + if (matchResult.length === 0) { + return false; + } + } + return true; + }; + + this.diagnostics = this.diagnostics.filter(reportFilesPredicate); + this.lints = this.lints.filter(reportFilesPredicate); + } + if ('hooks' in this.compiler) { // webpack 4 this.compiler.hooks.forkTsCheckerReceive.call( diff --git a/test/integration/index.spec.js b/test/integration/index.spec.js index 66d060b0..3592e6d9 100644 --- a/test/integration/index.spec.js +++ b/test/integration/index.spec.js @@ -61,26 +61,26 @@ describe('[INTEGRATION] index', function() { it('should detect paths', function() { var plugin = new ForkTsCheckerWebpackPlugin({ tslint: true }); - expect(plugin.tsconfig).to.be.equal('./tsconfig.json'); - expect(plugin.tslint).to.be.equal('./tslint.json'); + expect(plugin.tsconfig).to.equal('./tsconfig.json'); + expect(plugin.tslint).to.equal('./tslint.json'); }); it('should set logger to console by default', function() { var plugin = new ForkTsCheckerWebpackPlugin({}); - expect(plugin.logger).to.be.equal(console); + expect(plugin.logger).to.equal(console); }); it('should set watch to empty array by default', function() { var plugin = new ForkTsCheckerWebpackPlugin({}); - expect(plugin.watch).to.be.deep.equal([]); + expect(plugin.watch).to.deep.equal([]); }); it('should set watch to one element array for string', function() { var plugin = new ForkTsCheckerWebpackPlugin({ watch: '/test' }); - expect(plugin.watch).to.be.deep.equal(['/test']); + expect(plugin.watch).to.deep.equal(['/test']); }); it('should work without configuration', function(callback) { @@ -227,8 +227,8 @@ describe('[INTEGRATION] index', function() { }); function compareResults() { - expect(errorsA).to.be.deep.equal(errorsB); - expect(warningsA).to.be.deep.equal(warningsB); + expect(errorsA).to.deep.equal(errorsB); + expect(warningsA).to.deep.equal(warningsB); callback(); } }); @@ -284,7 +284,7 @@ describe('[INTEGRATION] index', function() { var compiler = createCompiler({}, true); compiler.run(function(error, stats) { - expect(stats.compilation.errors.length).to.be.equal(1); + expect(stats.compilation.errors.length).to.equal(1); callback(); }); }); @@ -293,7 +293,20 @@ describe('[INTEGRATION] index', function() { var compiler = createCompiler({ checkSyntacticErrors: true }, true); compiler.run(function(error, stats) { - expect(stats.compilation.errors.length).to.be.equal(2); + expect(stats.compilation.errors.length).to.equal(2); + callback(); + }); + }); + + it('should only show errors matching paths specified in reportFiles when provided', function(callback) { + var compiler = createCompiler( + { checkSyntacticErrors: true, reportFiles: ['**/index.ts'] }, + true + ); + + compiler.run(function(error, stats) { + expect(stats.compilation.errors.length).to.equal(1); + expect(stats.compilation.errors[0].file.endsWith('index.ts')).to.be.true; callback(); }); });