|
1 | 1 | const path = require('path')
|
| 2 | +const eslintWebpackPlugin = require('eslint-webpack-plugin') |
2 | 3 |
|
| 4 | +/** @type {import('@vue/cli-service').ServicePlugin} */ |
3 | 5 | module.exports = (api, options) => {
|
4 | 6 | if (options.lintOnSave) {
|
5 | 7 | const extensions = require('./eslintOptions').extensions(api)
|
6 | 8 | // Use loadModule to allow users to customize their ESLint dependency version.
|
7 | 9 | const { resolveModule, loadModule } = require('@vue/cli-shared-utils')
|
8 | 10 | const cwd = api.getCwd()
|
| 11 | + |
9 | 12 | const eslintPkg =
|
10 | 13 | loadModule('eslint/package.json', cwd, true) ||
|
11 | 14 | loadModule('eslint/package.json', __dirname, true)
|
12 | 15 |
|
13 |
| - // eslint-loader doesn't bust cache when eslint config changes |
14 |
| - // so we have to manually generate a cache identifier that takes the config |
15 |
| - // into account. |
16 |
| - const { cacheIdentifier } = api.genCacheConfig( |
17 |
| - 'eslint-loader', |
| 16 | + // ESLint doesn't clear the cache when you upgrade ESLint plugins (ESlint do consider config changes) |
| 17 | + // so we have to manually generate a cache identifier that takes lock file into account. |
| 18 | + const { cacheIdentifier, cacheDirectory } = api.genCacheConfig( |
| 19 | + 'eslint', |
18 | 20 | {
|
19 |
| - 'eslint-loader': require('eslint-loader/package.json').version, |
20 | 21 | eslint: eslintPkg.version
|
21 | 22 | },
|
22 |
| - [ |
23 |
| - '.eslintrc.js', |
24 |
| - '.eslintrc.yaml', |
25 |
| - '.eslintrc.yml', |
26 |
| - '.eslintrc.json', |
27 |
| - '.eslintrc', |
28 |
| - '.eslintignore', |
29 |
| - 'package.json' |
30 |
| - ] |
| 23 | + ['package.json'] |
31 | 24 | )
|
32 | 25 |
|
33 | 26 | api.chainWebpack(webpackConfig => {
|
34 | 27 | const { lintOnSave } = options
|
35 | 28 | const allWarnings = lintOnSave === true || lintOnSave === 'warning'
|
36 | 29 | const allErrors = lintOnSave === 'error'
|
37 | 30 |
|
38 |
| - webpackConfig.module |
39 |
| - .rule('eslint') |
40 |
| - .pre() |
41 |
| - .exclude |
42 |
| - .add(/node_modules/) |
43 |
| - .add(path.dirname(require.resolve('@vue/cli-service'))) |
44 |
| - .end() |
45 |
| - .test(/\.(vue|(j|t)sx?)$/) |
46 |
| - .use('eslint-loader') |
47 |
| - .loader(require.resolve('eslint-loader')) |
48 |
| - .options({ |
49 |
| - extensions, |
50 |
| - cache: true, |
51 |
| - cacheIdentifier, |
52 |
| - emitWarning: allWarnings, |
53 |
| - // only emit errors in production mode. |
54 |
| - emitError: allErrors, |
55 |
| - eslintPath: path.dirname( |
56 |
| - resolveModule('eslint/package.json', cwd) || |
57 |
| - resolveModule('eslint/package.json', __dirname) |
58 |
| - ), |
59 |
| - formatter: loadModule('eslint/lib/formatters/codeframe', cwd, true) |
60 |
| - }) |
| 31 | + /** @type {import('eslint-webpack-plugin').Options & import('eslint').ESLint.Options} */ |
| 32 | + const eslintWebpackPluginOptions = { |
| 33 | + // common to both plugin and ESlint |
| 34 | + extensions, |
| 35 | + // ESlint options |
| 36 | + cwd, |
| 37 | + cache: true, |
| 38 | + cacheLocation: path.format({ |
| 39 | + dir: cacheDirectory, |
| 40 | + name: process.env.VUE_CLI_TEST |
| 41 | + ? 'cache' |
| 42 | + : cacheIdentifier, |
| 43 | + ext: '.json' |
| 44 | + }), |
| 45 | + // plugin options |
| 46 | + context: cwd, |
| 47 | + // https://github.com/webpack-contrib/eslint-webpack-plugin/issues/56 |
| 48 | + threads: false, |
| 49 | + emitWarning: allWarnings, |
| 50 | + emitError: allErrors, |
| 51 | + eslintPath: path.dirname( |
| 52 | + resolveModule('eslint/package.json', cwd) || |
| 53 | + resolveModule('eslint/package.json', __dirname) |
| 54 | + ), |
| 55 | + formatter: loadModule('eslint/lib/formatters/codeframe', cwd, true) |
| 56 | + } |
| 57 | + webpackConfig.plugin('eslint').use(eslintWebpackPlugin, [eslintWebpackPluginOptions]) |
61 | 58 | })
|
62 | 59 | }
|
63 | 60 |
|
|
0 commit comments