index.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const path = require('path')
  2. module.exports = (api, options) => {
  3. if (options.lintOnSave) {
  4. const extensions = require('./eslintOptions').extensions(api)
  5. // Use loadModule to allow users to customize their ESLint dependency version.
  6. const { resolveModule, loadModule } = require('@vue/cli-shared-utils')
  7. const cwd = api.getCwd()
  8. const eslintPkg =
  9. loadModule('eslint/package.json', cwd, true) ||
  10. loadModule('eslint/package.json', __dirname, true)
  11. // eslint-loader doesn't bust cache when eslint config changes
  12. // so we have to manually generate a cache identifier that takes the config
  13. // into account.
  14. const { cacheIdentifier } = api.genCacheConfig(
  15. 'eslint-loader',
  16. {
  17. 'eslint-loader': require('eslint-loader/package.json').version,
  18. eslint: eslintPkg.version
  19. },
  20. [
  21. '.eslintrc.js',
  22. '.eslintrc.yaml',
  23. '.eslintrc.yml',
  24. '.eslintrc.json',
  25. '.eslintrc',
  26. '.eslintignore',
  27. 'package.json'
  28. ]
  29. )
  30. api.chainWebpack(webpackConfig => {
  31. const { lintOnSave } = options
  32. const allWarnings = lintOnSave === true || lintOnSave === 'warning'
  33. const allErrors = lintOnSave === 'error'
  34. webpackConfig.module
  35. .rule('eslint')
  36. .pre()
  37. .exclude
  38. .add(/node_modules/)
  39. .add(path.dirname(require.resolve('@vue/cli-service')))
  40. .end()
  41. .test(/\.(vue|(j|t)sx?)$/)
  42. .use('eslint-loader')
  43. .loader(require.resolve('eslint-loader'))
  44. .options({
  45. extensions,
  46. cache: true,
  47. cacheIdentifier,
  48. emitWarning: allWarnings,
  49. // only emit errors in production mode.
  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. })
  58. }
  59. api.registerCommand(
  60. 'lint',
  61. {
  62. description: 'lint and fix source files',
  63. usage: 'vue-cli-service lint [options] [...files]',
  64. options: {
  65. '--format [formatter]': 'specify formatter (default: codeframe)',
  66. '--no-fix': 'do not fix errors or warnings',
  67. '--no-fix-warnings': 'fix errors, but do not fix warnings',
  68. '--max-errors [limit]':
  69. 'specify number of errors to make build failed (default: 0)',
  70. '--max-warnings [limit]':
  71. 'specify number of warnings to make build failed (default: Infinity)'
  72. },
  73. details:
  74. 'For more options, see https://eslint.org/docs/user-guide/command-line-interface#options'
  75. },
  76. args => {
  77. require('./lint')(args, api)
  78. }
  79. )
  80. }