-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathindex.js
197 lines (159 loc) · 5.53 KB
/
index.js
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
const { isAbsolute, join } = require('path');
const { isMatch } = require('micromatch');
const { getOptions } = require('./options');
const linter = require('./linter');
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
*/
constructor(options = {}) {
this.key = ESLINT_PLUGIN;
this.options = getOptions(options);
this.run = this.run.bind(this);
}
/**
* @param {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)}`;
const excludedFiles = parseFiles(
this.options.exclude || [],
this.getContext(compiler),
);
const resourceQueries = arrify(this.options.resourceQueryExclude || []);
const excludedResourceQueries = resourceQueries.map((item) =>
item instanceof RegExp ? item : new RegExp(item),
);
const options = {
...this.options,
exclude: excludedFiles,
resourceQueryExclude: excludedResourceQueries,
extensions: arrify(this.options.extensions),
files: parseFiles(this.options.files || '', this.getContext(compiler)),
};
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, (c) =>
this.run(c, options, wanted, exclude),
);
}
let hasCompilerRunByDirtyModule = this.options.lintDirtyModulesOnly;
compiler.hooks.watchRun.tapPromise(this.key, (c) => {
if (!hasCompilerRunByDirtyModule)
return this.run(c, options, wanted, exclude);
hasCompilerRunByDirtyModule = false;
return Promise.resolve();
});
}
/**
* @param {Compiler} compiler
* @param {Omit<Options, 'resourceQueryExclude'> & {resourceQueryExclude: RegExp[]}} options
* @param {string[]} wanted
* @param {string[]} exclude
*/
async run(compiler, options, wanted, exclude) {
// @ts-ignore
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 (e) {
compilation.errors.push(e);
return;
}
/** @type {string[]} */
const files = [];
// Add the file to be linted
compilation.hooks.succeedModule.tap(this.key, addFile);
if (!this.options.lintDirtyModulesOnly) {
compilation.hooks.stillValidModule.tap(this.key, addFile);
}
/**
* @param {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);
}
}
// Lint all files added
compilation.hooks.finishModules.tap(this.key, () => {
if (files.length > 0 && threads <= 1) lint(files);
});
// await and interpret results
compilation.hooks.additionalAssets.tapPromise(this.key, processResults);
async function processResults() {
const { errors, warnings, generateReportAsset } = await report();
if (warnings && !options.failOnWarning) {
// @ts-ignore
compilation.warnings.push(warnings);
} else if (warnings) {
// @ts-ignore
compilation.errors.push(warnings);
}
if (errors && !options.failOnError) {
// @ts-ignore
compilation.warnings.push(errors);
} else if (errors) {
// @ts-ignore
compilation.errors.push(errors);
}
if (generateReportAsset) await generateReportAsset(compilation);
}
});
}
/**
*
* @param {Compiler} compiler
* @returns {string}
*/
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;