-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathindex.js
More file actions
200 lines (162 loc) · 5.7 KB
/
Copy pathindex.js
File metadata and controls
200 lines (162 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const { isAbsolute, join } = require("node:path");
const { isMatch } = require("micromatch");
const linter = require("./linter");
const { getOptions } = require("./options");
const { arrify, parseFiles, parseFoldersToGlobs } = require("./utils");
/** @typedef {import("webpack").Compiler} Compiler */
/** @typedef {import("webpack").Module} Module */
/** @typedef {import("webpack").NormalModule} NormalModule */
/** @typedef {import("./options").Options} Options */
const ESLINT_PLUGIN = "ESLintWebpackPlugin";
const DEFAULT_FOLDER_TO_EXCLUDE = "**/node_modules/**";
let compilerId = 0;
class ESLintWebpackPlugin {
/**
* @param {Options=} options options
*/
constructor(options = {}) {
this.key = ESLINT_PLUGIN;
this.options = getOptions(options);
this.run = this.run.bind(this);
}
/**
* @param {Compiler} compiler compiler
* @returns {void}
*/
apply(compiler) {
// Generate key for each compilation,
// this differentiates one from the other when being cached.
this.key = compiler.name || `${this.key}_${(compilerId += 1)}`;
this.options.failOnError ??= compiler.options.mode !== "development";
const context = this.getContext(compiler);
const excludedFiles = parseFiles(this.options.exclude || [], context);
const resourceQueries = arrify(this.options.resourceQueryExclude || []);
const excludedResourceQueries = resourceQueries.map((item) =>
item instanceof RegExp ? item : new RegExp(item),
);
const options = {
...this.options,
context,
exclude: excludedFiles,
resourceQueryExclude: excludedResourceQueries,
extensions: arrify(this.options.extensions),
files: parseFiles(this.options.files || "", context),
};
const foldersToExclude = this.options.exclude
? options.exclude
: DEFAULT_FOLDER_TO_EXCLUDE;
const exclude = parseFoldersToGlobs(foldersToExclude);
const wanted = parseFoldersToGlobs(options.files, options.extensions);
// If `lintDirtyModulesOnly` is disabled,
// execute the linter on the build
if (!this.options.lintDirtyModulesOnly) {
compiler.hooks.run.tapPromise(this.key, (compiler) =>
this.run(compiler, options, wanted, exclude),
);
}
let hasCompilerRunByDirtyModule = this.options.lintDirtyModulesOnly;
compiler.hooks.watchRun.tapPromise(this.key, (compiler) => {
if (!hasCompilerRunByDirtyModule) {
return this.run(compiler, options, wanted, exclude);
}
hasCompilerRunByDirtyModule = false;
return Promise.resolve();
});
}
/**
* @param {Compiler} compiler compiler
* @param {Omit<Options, "resourceQueryExclude"> & { resourceQueryExclude: RegExp[] }} options options
* @param {string[]} wanted wanted files
* @param {string[]} exclude excluded files
*/
async run(compiler, options, wanted, exclude) {
// Do not re-hook
const isCompilerHooked = compiler.hooks.compilation.taps.find(
({ name }) => name === this.key,
);
if (isCompilerHooked) return;
compiler.hooks.compilation.tap(this.key, async (compilation) => {
/** @type {import("./linter").Linter} */
let lint;
/** @type {import("./linter").Reporter} */
let report;
/** @type number */
let threads;
try {
({ lint, report, threads } = await linter(
this.key,
options,
compilation,
));
} catch (err) {
compilation.errors.push(err);
return;
}
/** @type {string[]} */
const files = [];
/**
* @param {Module} module module
*/
function addFile(module) {
const { resource } = /** @type {NormalModule} */ (module);
if (!resource) return;
const [file, query] = resource.split("?");
const isFileNotListed = file && !files.includes(file);
const isFileWanted =
isMatch(file, wanted, { dot: true }) &&
!isMatch(file, exclude, { dot: true });
const isQueryNotExclude = options.resourceQueryExclude.every(
(reg) => !reg.test(query),
);
if (isFileNotListed && isFileWanted && isQueryNotExclude) {
files.push(file);
if (threads > 1) lint(file);
}
}
// Add the file to be linted
compilation.hooks.succeedModule.tap(this.key, addFile);
if (!this.options.lintDirtyModulesOnly) {
compilation.hooks.stillValidModule.tap(this.key, addFile);
}
// Lint all files added
compilation.hooks.finishModules.tap(this.key, () => {
if (files.length > 0 && threads <= 1) lint(files);
});
// await and interpret results
compilation.hooks.processAssets.tapAsync(
this.key,
async (_, callback) => {
const { errors, warnings, generateReportAsset } = await report();
if (warnings) {
compilation.warnings.push(warnings);
}
if (errors) {
compilation.errors.push(errors);
}
if (generateReportAsset) {
await generateReportAsset(compilation);
}
if (warnings && options.failOnWarning) {
callback(warnings);
} else if (errors && options.failOnError) {
callback(errors);
} else {
callback();
}
},
);
});
}
/**
* @param {Compiler} compiler compiler
* @returns {string} context
*/
getContext(compiler) {
const compilerContext = String(compiler.options.context);
const optionContext = this.options.context;
if (!optionContext) return compilerContext;
if (isAbsolute(optionContext)) return optionContext;
return join(compilerContext, optionContext);
}
}
module.exports = ESLintWebpackPlugin;