Skip to content

Commit

Permalink
perf: require lint of stylelint only one time
Browse files Browse the repository at this point in the history
* refactor: separate utils

* perf: require lint of stylelint only one time
  • Loading branch information
ricardogobbosouza committed Feb 8, 2020
1 parent bf8cc46 commit 7e2495e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
10 changes: 6 additions & 4 deletions src/LintDirtyModulesPlugin.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { isMatch } from 'micromatch';

import linter from './linter';
import { replaceBackslashes } from './utils';

export default class LintDirtyModulesPlugin {
constructor(compiler, options) {
constructor(lint, compiler, options) {
this.lint = lint;
this.compiler = compiler;
this.options = options;
this.startTime = Date.now();
Expand All @@ -22,14 +24,14 @@ export default class LintDirtyModulesPlugin {
}

const dirtyOptions = { ...this.options };
const glob = dirtyOptions.files.join('|').replace(/\\/g, '/');
const glob = replaceBackslashes(dirtyOptions.files.join('|'));
const changedFiles = this.getChangedFiles(fileTimestamps, glob);

this.prevTimestamps = fileTimestamps;

if (changedFiles.length) {
dirtyOptions.files = changedFiles;
linter(dirtyOptions, this.compiler, callback);
linter(this.lint, dirtyOptions, this.compiler, callback);
} else {
callback();
}
Expand All @@ -53,7 +55,7 @@ export default class LintDirtyModulesPlugin {

for (const [filename, timestamp] of fileTimestamps.entries()) {
if (hasFileChanged(filename, timestamp) && isMatch(filename, glob)) {
changedFiles.push(filename.replace(/\\/g, '/'));
changedFiles.push(replaceBackslashes(filename));
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
import { isAbsolute, join } from 'path';

import arrify from 'arrify';

import getOptions from './getOptions';
import LintDirtyModulesPlugin from './LintDirtyModulesPlugin';
import linter from './linter';
import { parseFiles } from './utils';

class StylelintWebpackPlugin {
constructor(options = {}) {
this.options = getOptions(options);
}

apply(compiler) {
const context = this.getContext(compiler);
const options = { ...this.options };
const options = {
...this.options,
files: parseFiles(this.options.files, this.getContext(compiler)),
};

options.files = arrify(options.files).map((file) =>
join(context, '/', file).replace(/\\/g, '/')
);
// eslint-disable-next-line
const { lint } = require(options.stylelintPath);

const plugin = { name: this.constructor.name };

if (options.lintDirtyModulesOnly) {
const lintDirty = new LintDirtyModulesPlugin(compiler, options);
const lintDirty = new LintDirtyModulesPlugin(lint, compiler, options);

/* istanbul ignore next */
compiler.hooks.watchRun.tapAsync(plugin, (compilation, callback) => {
lintDirty.apply(compilation, callback);
});
} else {
compiler.hooks.run.tapAsync(plugin, (compilation, callback) => {
linter(options, compilation, callback);
linter(lint, options, compilation, callback);
});

/* istanbul ignore next */
compiler.hooks.watchRun.tapAsync(plugin, (compilation, callback) => {
linter(options, compilation, callback);
linter(lint, options, compilation, callback);
});
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/linter.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import StylelintError from './StylelintError';

export default function linter(options, compiler, callback) {
export default function linter(lint, options, compiler, callback) {
let errors = [];
let warnings = [];

// eslint-disable-next-line
const { lint } = require(options.stylelintPath);

lint(options)
.then(({ results }) => {
({ errors, warnings } = parseResults(options, results));
Expand Down
13 changes: 13 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { join } from 'path';

import arrify from 'arrify';

export function parseFiles(files, context) {
return arrify(files).map((file) =>
replaceBackslashes(join(context, '/', file))
);
}

export function replaceBackslashes(str) {
return str.replace(/\\/g, '/');
}
4 changes: 3 additions & 1 deletion test/LintDirtyModulesPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ describe('lint dirty modules only', () => {
beforeAll(() => {
callback = jest.fn();

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

beforeEach(() => {
Expand Down

0 comments on commit 7e2495e

Please sign in to comment.