UNPKG

2.87 kBJavaScriptView Raw
1const path = require('path')
2
3module.exports = (api, options) => {
4 if (options.lintOnSave) {
5 const extensions = require('./eslintOptions').extensions(api)
6 // Use loadModule to allow users to customize their ESLint dependency version.
7 const { resolveModule, loadModule } = require('@vue/cli-shared-utils')
8 const cwd = api.getCwd()
9 const eslintPkg =
10 loadModule('eslint/package.json', cwd, true) ||
11 loadModule('eslint/package.json', __dirname, true)
12
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',
18 {
19 'eslint-loader': require('eslint-loader/package.json').version,
20 eslint: eslintPkg.version
21 },
22 [
23 '.eslintrc.js',
24 '.eslintrc.yaml',
25 '.eslintrc.yml',
26 '.eslintrc.json',
27 '.eslintrc',
28 '.eslintignore',
29 'package.json'
30 ]
31 )
32
33 api.chainWebpack(webpackConfig => {
34 const { lintOnSave } = options
35 const allWarnings = lintOnSave === true || lintOnSave === 'warning'
36 const allErrors = lintOnSave === 'error'
37
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 })
61 })
62 }
63
64 api.registerCommand(
65 'lint',
66 {
67 description: 'lint and fix source files',
68 usage: 'vue-cli-service lint [options] [...files]',
69 options: {
70 '--format [formatter]': 'specify formatter (default: codeframe)',
71 '--no-fix': 'do not fix errors or warnings',
72 '--no-fix-warnings': 'fix errors, but do not fix warnings',
73 '--max-errors [limit]':
74 'specify number of errors to make build failed (default: 0)',
75 '--max-warnings [limit]':
76 'specify number of warnings to make build failed (default: Infinity)'
77 },
78 details:
79 'For more options, see https://eslint.org/docs/user-guide/command-line-interface#options'
80 },
81 args => {
82 require('./lint')(args, api)
83 }
84 )
85}