Skip to content

Commit b5bfce8

Browse files
perf: parse options only one time (#122)
1 parent 7458335 commit b5bfce8

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

declarations/index.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@ declare class ESLintWebpackPlugin {
1010
options: import('./options').PluginOptions;
1111
/**
1212
* @param {Compiler} compiler
13+
* @param {Options} options
14+
* @param {string[]} wanted
15+
* @param {string[]} exclude
1316
*/
14-
run(compiler: Compiler): Promise<void>;
17+
run(
18+
compiler: Compiler,
19+
options: Options,
20+
wanted: string[],
21+
exclude: string[]
22+
): Promise<void>;
1523
/**
1624
* @param {Compiler} compiler
1725
* @returns {void}

src/index.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,28 @@ class ESLintWebpackPlugin {
3131
// this differentiates one from the other when being cached.
3232
this.key = compiler.name || `${this.key}_${(counter += 1)}`;
3333

34+
const options = {
35+
...this.options,
36+
exclude: parseFiles(
37+
this.options.exclude || [],
38+
this.getContext(compiler)
39+
),
40+
extensions: arrify(this.options.extensions),
41+
files: parseFiles(this.options.files || '', this.getContext(compiler)),
42+
};
43+
44+
const wanted = parseFoldersToGlobs(options.files, options.extensions);
45+
const exclude = parseFoldersToGlobs(
46+
this.options.exclude ? options.exclude : '**/node_modules/**',
47+
[]
48+
);
49+
3450
// If `lintDirtyModulesOnly` is disabled,
3551
// execute the linter on the build
3652
if (!this.options.lintDirtyModulesOnly) {
37-
compiler.hooks.run.tapPromise(this.key, this.run);
53+
compiler.hooks.run.tapPromise(this.key, (c) =>
54+
this.run(c, options, wanted, exclude)
55+
);
3856
}
3957

4058
let isFirstRun = this.options.lintDirtyModulesOnly;
@@ -45,14 +63,17 @@ class ESLintWebpackPlugin {
4563
return Promise.resolve();
4664
}
4765

48-
return this.run(c);
66+
return this.run(c, options, wanted, exclude);
4967
});
5068
}
5169

5270
/**
5371
* @param {Compiler} compiler
72+
* @param {Options} options
73+
* @param {string[]} wanted
74+
* @param {string[]} exclude
5475
*/
55-
async run(compiler) {
76+
async run(compiler, options, wanted, exclude) {
5677
// Do not re-hook
5778
if (
5879
// @ts-ignore
@@ -61,22 +82,6 @@ class ESLintWebpackPlugin {
6182
return;
6283
}
6384

64-
const options = {
65-
...this.options,
66-
exclude: parseFiles(
67-
this.options.exclude || [],
68-
this.getContext(compiler)
69-
),
70-
extensions: arrify(this.options.extensions),
71-
files: parseFiles(this.options.files || '', this.getContext(compiler)),
72-
};
73-
74-
const wanted = parseFoldersToGlobs(options.files, options.extensions);
75-
const exclude = parseFoldersToGlobs(
76-
this.options.exclude ? options.exclude : '**/node_modules/**',
77-
[]
78-
);
79-
8085
compiler.hooks.thisCompilation.tap(this.key, (compilation) => {
8186
/** @type {import('./linter').Linter} */
8287
let lint;

src/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import normalizePath from 'normalize-path';
1919
: [T]
2020
}
2121
*/
22+
/* istanbul ignore next */
2223
export function arrify(value) {
2324
// eslint-disable-next-line no-undefined
2425
if (value === null || value === undefined) {

0 commit comments

Comments
 (0)