Skip to content

Commit 18e4042

Browse files
perf: parse options only one time
1 parent 420b750 commit 18e4042

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

src/index.js

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,30 @@ class ESLintWebpackPlugin {
3333
// this differentiates one from the other when being cached.
3434
this.key = compiler.name || `${this.key}_${(counter += 1)}`;
3535

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

42-
// TODO: Figure out want `compiler.watching` is and how to use it in Webpack5.
43-
// From my testing of compiler.watch() ... compiler.watching is always
44-
// undefined (webpack 4 doesn't define it either) I'm leaving it out
45-
// for now.
4660
let isFirstRun = this.options.lintDirtyModulesOnly;
4761
compiler.hooks.watchRun.tapPromise(this.key, (c) => {
4862
if (isFirstRun) {
@@ -51,39 +65,26 @@ class ESLintWebpackPlugin {
5165
return Promise.resolve();
5266
}
5367

54-
return this.run(c);
68+
return this.run(c, options, wanted, exclude);
5569
});
5670
}
5771

5872
/**
5973
* @param {Compiler} compiler
74+
* @param {Options} options
75+
* @param {string[]} wanted
76+
* @param {string[]} exclude
6077
*/
61-
async run(compiler) {
78+
async run(compiler, options, wanted, exclude) {
6279
// Do not re-hook
6380
if (
6481
// @ts-ignore
65-
compiler.hooks.compilation.taps.find(({ name }) => name === this.key)
82+
compiler.hooks.thisCompilation.taps.find(({ name }) => name === this.key)
6683
) {
6784
return;
6885
}
6986

70-
const options = {
71-
...this.options,
72-
exclude: parseFiles(
73-
this.options.exclude || [],
74-
this.getContext(compiler)
75-
),
76-
extensions: arrify(this.options.extensions),
77-
files: parseFiles(this.options.files || '', this.getContext(compiler)),
78-
};
79-
80-
const wanted = parseFoldersToGlobs(options.files, options.extensions);
81-
const exclude = parseFoldersToGlobs(
82-
this.options.exclude ? options.exclude : '**/node_modules/**',
83-
[]
84-
);
85-
86-
compiler.hooks.compilation.tap(this.key, (compilation) => {
87+
compiler.hooks.thisCompilation.tap(this.key, (compilation) => {
8788
/** @type {import('./linter').Linter} */
8889
let lint;
8990
/** @type {import('./linter').Reporter} */

0 commit comments

Comments
 (0)