index.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. require('eslint/package.json')
  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. 'package.json'
  27. ]
  28. )
  29. api.chainWebpack(webpackConfig => {
  30. webpackConfig.resolveLoader.modules.prepend(
  31. path.join(__dirname, 'node_modules')
  32. )
  33. const { lintOnSave } = options
  34. const allWarnings = lintOnSave === true || lintOnSave === 'warning'
  35. const allErrors = lintOnSave === 'error'
  36. webpackConfig.module
  37. .rule('eslint')
  38. .pre()
  39. .exclude
  40. .add(/node_modules/)
  41. .add(require('path').dirname(require.resolve('@vue/cli-service')))
  42. .end()
  43. .test(/\.(vue|(j|t)sx?)$/)
  44. .use('eslint-loader')
  45. .loader('eslint-loader')
  46. .options({
  47. extensions,
  48. cache: true,
  49. cacheIdentifier,
  50. emitWarning: allWarnings,
  51. // only emit errors in production mode.
  52. emitError: allErrors,
  53. eslintPath: resolveModule('eslint', cwd) || require.resolve('eslint'),
  54. formatter:
  55. loadModule('eslint/lib/formatters/codeframe', cwd, true) ||
  56. require('eslint/lib/formatters/codeframe')
  57. })
  58. })
  59. }
  60. api.registerCommand(
  61. 'lint',
  62. {
  63. description: 'lint and fix source files',
  64. usage: 'vue-cli-service lint [options] [...files]',
  65. options: {
  66. '--format [formatter]': 'specify formatter (default: codeframe)',
  67. '--no-fix': 'do not fix errors or warnings',
  68. '--no-fix-warnings': 'fix errors, but do not fix warnings',
  69. '--max-errors [limit]':
  70. 'specify number of errors to make build failed (default: 0)',
  71. '--max-warnings [limit]':
  72. 'specify number of warnings to make build failed (default: Infinity)'
  73. },
  74. details:
  75. 'For more options, see https://eslint.org/docs/user-guide/command-line-interface#options'
  76. },
  77. args => {
  78. require('./lint')(args, api)
  79. }
  80. )
  81. }