diff --git a/src/index.js b/src/index.js index ad70f07..624c2e3 100644 --- a/src/index.js +++ b/src/index.js @@ -34,7 +34,16 @@ export class ESLintWebpackPlugin { // From my testing of compiler.watch() ... compiler.watching is always // undefined (webpack 4 doesn't define it either) I'm leaving it out // for now. - compiler.hooks.watchRun.tapPromise(ESLINT_PLUGIN, this.run); + let isFirstRun = this.options.lintDirtyModulesOnly; + compiler.hooks.watchRun.tapPromise(ESLINT_PLUGIN, (c) => { + if (isFirstRun) { + isFirstRun = false; + + return Promise.resolve(); + } + + return this.run(c); + }); } /** @@ -81,7 +90,7 @@ export class ESLintWebpackPlugin { // @ts-ignore const processModule = (module) => { if (module.resource) { - const file = module.resource.split('?')[0]; + const [file] = module.resource.split('?'); if ( file && diff --git a/test/lint-dirty-modules-only.test.js b/test/lint-dirty-modules-only.test.js index 64dcb9c..84b2f14 100644 --- a/test/lint-dirty-modules-only.test.js +++ b/test/lint-dirty-modules-only.test.js @@ -1,14 +1,49 @@ +import { join } from 'path'; +import { writeFileSync } from 'fs'; + +import { removeSync } from 'fs-extra'; + import pack from './utils/pack'; +const target = join(__dirname, 'fixtures', 'lint-dirty-modules-only-entry.js'); + describe('lint dirty modules only', () => { + let watch; + afterEach(() => { + if (watch) { + watch.close(); + } + removeSync(target); + }); + it('skips linting on initial run', (done) => { - const compiler = pack('error', { lintDirtyModulesOnly: true }); + writeFileSync(target, 'const foo = false\n'); + + let next = firstPass; + const compiler = pack('lint-dirty-modules-only', { + lintDirtyModulesOnly: true, + }); + watch = compiler.watch({}, (err, stats) => next(err, stats)); - compiler.run((err, stats) => { + function firstPass(err, stats) { expect(err).toBeNull(); expect(stats.hasWarnings()).toBe(false); expect(stats.hasErrors()).toBe(false); + + next = secondPass; + + writeFileSync(target, 'const bar = false;\n'); + } + + function secondPass(err, stats) { + expect(err).toBeNull(); + expect(stats.hasWarnings()).toBe(false); + expect(stats.hasErrors()).toBe(true); + const { errors } = stats.compilation; + expect(errors.length).toBe(1); + const [{ message }] = errors; + expect(message).toEqual(expect.stringMatching('no-unused-vars')); done(); - }); + } }); });