From a60788924c5443e05c3335edc1ab6d1145cfe5d1 Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Sun, 27 Oct 2019 08:00:44 -0300 Subject: [PATCH 1/3] fix: use hook `afterCompile` instead of `afterEmit` --- src/linter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/linter.js b/src/linter.js index 96a705c..3001ae1 100644 --- a/src/linter.js +++ b/src/linter.js @@ -21,7 +21,7 @@ export default function linter(options, compiler, callback) { }) .catch(callback); - compiler.hooks.afterEmit.tapAsync( + compiler.hooks.afterCompile.tapAsync( 'StylelintWebpackPlugin', (compilation, next) => { if (warnings.length) { From eed324ba757a5f3dd45eba75fc8df6805f9b7095 Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Sun, 27 Oct 2019 08:01:50 -0300 Subject: [PATCH 2/3] fix: use hook `watchRun` instead of `emit` for lintDirtyModulesOnly --- src/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 0e755ee..53f02ed 100644 --- a/src/index.js +++ b/src/index.js @@ -23,7 +23,9 @@ class StylelintWebpackPlugin { if (options.lintDirtyModulesOnly) { const lintDirty = new LintDirtyModulesPlugin(compiler, options); - compiler.hooks.emit.tapAsync(plugin, (compilation, callback) => { + + /* istanbul ignore next */ + compiler.hooks.watchRun.tapAsync(plugin, (compilation, callback) => { lintDirty.apply(compilation, callback); }); } else { From 0e574515a8f4c2909e74b42d947979c28cb95e4f Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Sun, 27 Oct 2019 08:05:00 -0300 Subject: [PATCH 3/3] test: refactor --- test/LintDirtyModulesPlugin.test.js | 59 ++++++++++++++++++++++++++++ test/lint-dirty-modules-only.test.js | 47 ---------------------- 2 files changed, 59 insertions(+), 47 deletions(-) create mode 100644 test/LintDirtyModulesPlugin.test.js diff --git a/test/LintDirtyModulesPlugin.test.js b/test/LintDirtyModulesPlugin.test.js new file mode 100644 index 0000000..ba87fbc --- /dev/null +++ b/test/LintDirtyModulesPlugin.test.js @@ -0,0 +1,59 @@ +import LintDirtyModulesPlugin from '../src/LintDirtyModulesPlugin'; +import linter from '../src/linter'; + +jest.mock('../src/linter'); + +describe('lint dirty modules only', () => { + let plugin; + let callback; + + beforeAll(() => { + callback = jest.fn(); + + plugin = new LintDirtyModulesPlugin(null, { files: ['**\\*.s?(c|a)ss'] }); + }); + + beforeEach(() => { + linter.mockRestore(); + callback.mockRestore(); + }); + + it('skips linting on initial run', () => { + expect(plugin.isFirstRun).toBe(true); + expect(callback).not.toBeCalled(); + + plugin.apply({}, callback); + + expect(plugin.isFirstRun).toBe(false); + expect(callback).toBeCalledTimes(1); + }); + + it('linting on change file', () => { + const fileTimestamps = new Map([ + ['foo/changed.scss', 1], + ['bar\\changed.scss', 1], + ['new-file.scss'], + ]); + + plugin.isFirstRun = false; + plugin.prevTimestamps = new Map([ + ['foo/changed.scss', 2], + ['bar\\changed.scss', 2], + ]); + plugin.apply({ fileTimestamps }, callback); + + expect(linter).toBeCalledTimes(1); + expect(callback).not.toBeCalled(); + }); + + it('not linter if files are not changed', () => { + const fileTimestamps = new Map([['not-changed.scss', 1]]); + + plugin.isFirstRun = false; + plugin.prevTimestamps = fileTimestamps; + plugin.apply({ fileTimestamps }, callback); + + expect(linter).not.toBeCalled(); + expect(callback).toBeCalledTimes(1); + }); +}); diff --git a/test/lint-dirty-modules-only.test.js b/test/lint-dirty-modules-only.test.js index 122e34d..39aa59b 100644 --- a/test/lint-dirty-modules-only.test.js +++ b/test/lint-dirty-modules-only.test.js @@ -1,26 +1,6 @@ -import LintDirtyModulesPlugin from '../src/LintDirtyModulesPlugin'; -import linter from '../src/linter'; - import pack from './utils/pack'; -jest.mock('../src/linter'); - describe('lint dirty modules only', () => { - let plugin; - let callback; - - beforeAll(() => { - callback = jest.fn(); - - plugin = new LintDirtyModulesPlugin(null, { files: ['**\\*.s?(c|a)ss'] }); - plugin.isFirstRun = false; - }); - - beforeEach(() => { - linter.mockRestore(); - callback.mockRestore(); - }); - it('skips linting on initial run', (done) => { const compiler = pack('error', { lintDirtyModulesOnly: true }); @@ -30,31 +10,4 @@ describe('lint dirty modules only', () => { done(); }); }); - - it('linting on change file', () => { - const fileTimestamps = new Map([ - ['foo/changed.scss', 1], - ['bar\\changed.scss', 1], - ['new-file.scss'], - ]); - - plugin.prevTimestamps = new Map([ - ['foo/changed.scss', 2], - ['bar\\changed.scss', 2], - ]); - plugin.apply({ fileTimestamps }, callback); - - expect(linter).toBeCalledTimes(1); - expect(callback).not.toBeCalled(); - }); - - it('not linter if files are not changed', () => { - const fileTimestamps = new Map([['not-changed.scss', 1]]); - - plugin.prevTimestamps = fileTimestamps; - plugin.apply({ fileTimestamps }, callback); - - expect(linter).not.toBeCalled(); - expect(callback).toBeCalledTimes(1); - }); });