Skip to content

Commit 4167816

Browse files
perf: parse options only one time (#280)
1 parent d6d7d98 commit 4167816

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

declarations/index.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ declare class StylelintWebpackPlugin {
1717
options: Partial<import('./options').PluginOptions>;
1818
/**
1919
* @param {Compiler} compiler
20+
* @param {Options} options
21+
* @param {string[]} wanted
22+
* @param {string[]} exclude
2023
*/
21-
run(compiler: Compiler): Promise<void>;
24+
run(
25+
compiler: Compiler,
26+
options: Options,
27+
wanted: string[],
28+
exclude: string[]
29+
): Promise<void>;
2230
startTime: number;
2331
/** @type {ReadonlyMap<string, null | FileSystemInfoEntry | "ignore" | undefined>} */
2432
prevTimestamps: ReadonlyMap<

src/index.js

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

44+
const context = this.getContext(compiler);
45+
const excludeDefault = [
46+
'**/node_modules/**',
47+
String(compiler.options.output.path),
48+
];
49+
50+
const options = {
51+
...this.options,
52+
context,
53+
exclude: parseFiles(this.options.exclude || excludeDefault, context),
54+
extensions: arrify(this.options.extensions),
55+
files: parseFiles(this.options.files || '', context),
56+
};
57+
58+
const wanted = parseFoldersToGlobs(options.files, options.extensions);
59+
const exclude = parseFoldersToGlobs(options.exclude);
60+
4461
// If `lintDirtyModulesOnly` is disabled,
4562
// execute the linter on the build
4663
if (!this.options.lintDirtyModulesOnly) {
47-
compiler.hooks.run.tapPromise(this.key, this.run);
64+
compiler.hooks.run.tapPromise(this.key, (c) =>
65+
this.run(c, options, wanted, exclude)
66+
);
4867
}
4968

5069
let isFirstRun = this.options.lintDirtyModulesOnly;
@@ -55,14 +74,17 @@ class StylelintWebpackPlugin {
5574
return Promise.resolve();
5675
}
5776

58-
return this.run(c);
77+
return this.run(c, options, wanted, exclude);
5978
});
6079
}
6180

6281
/**
6382
* @param {Compiler} compiler
83+
* @param {Options} options
84+
* @param {string[]} wanted
85+
* @param {string[]} exclude
6486
*/
65-
async run(compiler) {
87+
async run(compiler, options, wanted, exclude) {
6688
// Do not re-hook
6789
/* istanbul ignore if */
6890
if (
@@ -72,23 +94,6 @@ class StylelintWebpackPlugin {
7294
return;
7395
}
7496

75-
const context = this.getContext(compiler);
76-
const options = {
77-
...this.options,
78-
exclude: parseFiles(
79-
this.options.exclude || [
80-
'**/node_modules/**',
81-
compiler.options.output.path,
82-
],
83-
context
84-
),
85-
extensions: arrify(this.options.extensions),
86-
files: parseFiles(this.options.files || '', context),
87-
};
88-
89-
const wanted = parseFoldersToGlobs(options.files, options.extensions);
90-
const exclude = parseFoldersToGlobs(options.exclude);
91-
9297
compiler.hooks.thisCompilation.tap(this.key, (compilation) => {
9398
/** @type {import('./linter').Linter} */
9499
let lint;
@@ -109,10 +114,10 @@ class StylelintWebpackPlugin {
109114

110115
if (threads > 1) {
111116
for (const file of files) {
112-
lint(parseFiles(file, context));
117+
lint(parseFiles(file, options.context || ''));
113118
}
114119
} else if (files.length > 0) {
115-
lint(parseFiles(files, context));
120+
lint(parseFiles(files, options.context || ''));
116121
}
117122
});
118123

0 commit comments

Comments
 (0)