Skip to content

Commit 313823e

Browse files
perf: parse options only one time (#276)
1 parent e1d19d8 commit 313823e

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

src/index.js

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,29 @@ class StylelintWebpackPlugin {
3636
// this differentiates one from the other when being cached.
3737
this.key = compiler.name || `${this.key}_${(counter += 1)}`;
3838

39+
const context = this.getContext(compiler);
40+
const excludeDefault = [
41+
'**/node_modules/**',
42+
String(compiler.options.output.path),
43+
];
44+
45+
const options = {
46+
...this.options,
47+
context,
48+
exclude: parseFiles(this.options.exclude || excludeDefault, context),
49+
extensions: arrify(this.options.extensions),
50+
files: parseFiles(this.options.files || '', context),
51+
};
52+
53+
const wanted = parseFoldersToGlobs(options.files, options.extensions);
54+
const exclude = parseFoldersToGlobs(options.exclude);
55+
3956
// If `lintDirtyModulesOnly` is disabled,
4057
// execute the linter on the build
4158
if (!this.options.lintDirtyModulesOnly) {
42-
compiler.hooks.run.tapPromise(this.key, this.run);
59+
compiler.hooks.run.tapPromise(this.key, (c) =>
60+
this.run(c, options, wanted, exclude)
61+
);
4362
}
4463

4564
let isFirstRun = this.options.lintDirtyModulesOnly;
@@ -50,14 +69,17 @@ class StylelintWebpackPlugin {
5069
return Promise.resolve();
5170
}
5271

53-
return this.run(c);
72+
return this.run(c, options, wanted, exclude);
5473
});
5574
}
5675

5776
/**
5877
* @param {Compiler} compiler
78+
* @param {Options} options
79+
* @param {string[]} wanted
80+
* @param {string[]} exclude
5981
*/
60-
async run(compiler) {
82+
async run(compiler, options, wanted, exclude) {
6183
// Do not re-hook
6284
/* istanbul ignore if */
6385
if (
@@ -67,22 +89,6 @@ class StylelintWebpackPlugin {
6789
return;
6890
}
6991

70-
const context = this.getContext(compiler);
71-
const excludeDefault = [
72-
'**/node_modules/**',
73-
String(compiler.options.output.path),
74-
];
75-
76-
const options = {
77-
...this.options,
78-
exclude: parseFiles(this.options.exclude || excludeDefault, context),
79-
extensions: arrify(this.options.extensions),
80-
files: parseFiles(this.options.files || '', context),
81-
};
82-
83-
const wanted = parseFoldersToGlobs(options.files, options.extensions);
84-
const exclude = parseFoldersToGlobs(options.exclude);
85-
8692
compiler.hooks.thisCompilation.tap(this.key, (compilation) => {
8793
/** @type {import('./linter').Linter} */
8894
let lint;
@@ -131,10 +137,10 @@ class StylelintWebpackPlugin {
131137

132138
if (threads > 1) {
133139
for (const file of files) {
134-
lint(parseFiles(file, context));
140+
lint(parseFiles(file, options.context || ''));
135141
}
136142
} else if (files.length > 0) {
137-
lint(parseFiles(files, context));
143+
lint(parseFiles(files, options.context || ''));
138144
}
139145
});
140146

types/index.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@ declare class StylelintWebpackPlugin {
88
options: Partial<import('./options').PluginOptions>;
99
/**
1010
* @param {Compiler} compiler
11+
* @param {Options} options
12+
* @param {string[]} wanted
13+
* @param {string[]} exclude
1114
*/
12-
run(compiler: Compiler): Promise<void>;
15+
run(
16+
compiler: Compiler,
17+
options: Options,
18+
wanted: string[],
19+
exclude: string[]
20+
): Promise<void>;
1321
startTime: number;
1422
prevTimestamps: Map<any, any>;
1523
/**

0 commit comments

Comments
 (0)