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