Skip to content

Commit

Permalink
fix: compiler hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza committed Sep 30, 2019
1 parent 9dc0a9c commit aca2c1d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
13 changes: 7 additions & 6 deletions src/LintDirtyModulesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,31 @@ import { isMatch } from 'micromatch';
import linter from './linter';

export default class LintDirtyModulesPlugin {
constructor(options) {
constructor(compiler, options) {
this.compiler = compiler;
this.options = options;
this.startTime = Date.now();
this.prevTimestamps = {};
this.isFirstRun = true;
}

apply(compiler, callback) {
apply(compilation, callback) {
if (this.isFirstRun) {
this.isFirstRun = false;
this.prevTimestamps = compiler.fileTimestamps;
this.prevTimestamps = compilation.fileTimestamps;
callback();
return;
}

const dirtyOptions = { ...this.options };
const glob = dirtyOptions.files.join('|');
const changedFiles = this.getChangedFiles(compiler.fileTimestamps, glob);
const changedFiles = this.getChangedFiles(compilation.fileTimestamps, glob);

this.prevTimestamps = compiler.fileTimestamps;
this.prevTimestamps = compilation.fileTimestamps;

if (changedFiles.length) {
dirtyOptions.files = changedFiles;
linter(dirtyOptions, compiler, callback);
linter(dirtyOptions, this.compiler, callback);
} else {
callback();
}
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class StylelintWebpackPlugin {
const plugin = { name: this.constructor.name };

if (options.lintDirtyModulesOnly) {
const lintDirtyModulesPlugin = new LintDirtyModulesPlugin(options);
const lintDirty = new LintDirtyModulesPlugin(compiler, options);
compiler.hooks.emit.tapAsync(plugin, (compilation, callback) => {
lintDirtyModulesPlugin.apply(compilation, callback);
lintDirty.apply(compilation, callback);
});
} else {
compiler.hooks.run.tapAsync(plugin, (compilation, callback) => {
Expand Down
2 changes: 1 addition & 1 deletion src/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function linter(options, compiler, callback) {
})
.catch(callback);

compiler.hooks.afterCompile.tapAsync(
compiler.hooks.afterEmit.tapAsync(
'StylelintWebpackPlugin',
(compilation, next) => {
if (warnings.length) {
Expand Down
2 changes: 1 addition & 1 deletion test/lint-dirty-modules-only.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('lint dirty modules only', () => {
beforeAll(() => {
callback = jest.fn();

plugin = new LintDirtyModulesPlugin({ files: ['**/*.s?(c|a)ss'] });
plugin = new LintDirtyModulesPlugin(null, { files: ['**/*.s?(c|a)ss'] });
plugin.isFirstRun = false;
});

Expand Down

0 comments on commit aca2c1d

Please sign in to comment.