Skip to content

Commit 7e2495e

Browse files
perf: require lint of stylelint only one time
* refactor: separate utils * perf: require lint of stylelint only one time
1 parent bf8cc46 commit 7e2495e

File tree

5 files changed

+33
-19
lines changed

5 files changed

+33
-19
lines changed

src/LintDirtyModulesPlugin.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { isMatch } from 'micromatch';
22

33
import linter from './linter';
4+
import { replaceBackslashes } from './utils';
45

56
export default class LintDirtyModulesPlugin {
6-
constructor(compiler, options) {
7+
constructor(lint, compiler, options) {
8+
this.lint = lint;
79
this.compiler = compiler;
810
this.options = options;
911
this.startTime = Date.now();
@@ -22,14 +24,14 @@ export default class LintDirtyModulesPlugin {
2224
}
2325

2426
const dirtyOptions = { ...this.options };
25-
const glob = dirtyOptions.files.join('|').replace(/\\/g, '/');
27+
const glob = replaceBackslashes(dirtyOptions.files.join('|'));
2628
const changedFiles = this.getChangedFiles(fileTimestamps, glob);
2729

2830
this.prevTimestamps = fileTimestamps;
2931

3032
if (changedFiles.length) {
3133
dirtyOptions.files = changedFiles;
32-
linter(dirtyOptions, this.compiler, callback);
34+
linter(this.lint, dirtyOptions, this.compiler, callback);
3335
} else {
3436
callback();
3537
}
@@ -53,7 +55,7 @@ export default class LintDirtyModulesPlugin {
5355

5456
for (const [filename, timestamp] of fileTimestamps.entries()) {
5557
if (hasFileChanged(filename, timestamp) && isMatch(filename, glob)) {
56-
changedFiles.push(filename.replace(/\\/g, '/'));
58+
changedFiles.push(replaceBackslashes(filename));
5759
}
5860
}
5961

src/index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
11
import { isAbsolute, join } from 'path';
22

3-
import arrify from 'arrify';
4-
53
import getOptions from './getOptions';
64
import LintDirtyModulesPlugin from './LintDirtyModulesPlugin';
75
import linter from './linter';
6+
import { parseFiles } from './utils';
87

98
class StylelintWebpackPlugin {
109
constructor(options = {}) {
1110
this.options = getOptions(options);
1211
}
1312

1413
apply(compiler) {
15-
const context = this.getContext(compiler);
16-
const options = { ...this.options };
14+
const options = {
15+
...this.options,
16+
files: parseFiles(this.options.files, this.getContext(compiler)),
17+
};
1718

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

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

2424
if (options.lintDirtyModulesOnly) {
25-
const lintDirty = new LintDirtyModulesPlugin(compiler, options);
25+
const lintDirty = new LintDirtyModulesPlugin(lint, compiler, options);
2626

2727
/* istanbul ignore next */
2828
compiler.hooks.watchRun.tapAsync(plugin, (compilation, callback) => {
2929
lintDirty.apply(compilation, callback);
3030
});
3131
} else {
3232
compiler.hooks.run.tapAsync(plugin, (compilation, callback) => {
33-
linter(options, compilation, callback);
33+
linter(lint, options, compilation, callback);
3434
});
3535

3636
/* istanbul ignore next */
3737
compiler.hooks.watchRun.tapAsync(plugin, (compilation, callback) => {
38-
linter(options, compilation, callback);
38+
linter(lint, options, compilation, callback);
3939
});
4040
}
4141
}

src/linter.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import StylelintError from './StylelintError';
22

3-
export default function linter(options, compiler, callback) {
3+
export default function linter(lint, options, compiler, callback) {
44
let errors = [];
55
let warnings = [];
66

7-
// eslint-disable-next-line
8-
const { lint } = require(options.stylelintPath);
9-
107
lint(options)
118
.then(({ results }) => {
129
({ errors, warnings } = parseResults(options, results));

src/utils.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { join } from 'path';
2+
3+
import arrify from 'arrify';
4+
5+
export function parseFiles(files, context) {
6+
return arrify(files).map((file) =>
7+
replaceBackslashes(join(context, '/', file))
8+
);
9+
}
10+
11+
export function replaceBackslashes(str) {
12+
return str.replace(/\\/g, '/');
13+
}

test/LintDirtyModulesPlugin.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ describe('lint dirty modules only', () => {
1010
beforeAll(() => {
1111
callback = jest.fn();
1212

13-
plugin = new LintDirtyModulesPlugin(null, { files: ['**\\*.s?(c|a)ss'] });
13+
plugin = new LintDirtyModulesPlugin(null, null, {
14+
files: ['**\\*.s?(c|a)ss'],
15+
});
1416
});
1517

1618
beforeEach(() => {

0 commit comments

Comments
 (0)