index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. const path = require('path')
  2. const eslintWebpackPlugin = require('eslint-webpack-plugin')
  3. /** @type {import('@vue/cli-service').ServicePlugin} */
  4. module.exports = (api, options) => {
  5. if (options.lintOnSave) {
  6. const extensions = require('./eslintOptions').extensions(api)
  7. // Use loadModule to allow users to customize their ESLint dependency version.
  8. const { resolveModule, loadModule } = require('@vue/cli-shared-utils')
  9. const cwd = api.getCwd()
  10. const eslintPkg =
  11. loadModule('eslint/package.json', cwd, true) ||
  12. loadModule('eslint/package.json', __dirname, true)
  13. // ESLint doesn't clear the cache when you upgrade ESLint plugins (ESlint do consider config changes)
  14. // so we have to manually generate a cache identifier that takes lock file into account.
  15. const { cacheIdentifier, cacheDirectory } = api.genCacheConfig(
  16. 'eslint',
  17. {
  18. eslint: eslintPkg.version
  19. },
  20. ['package.json']
  21. )
  22. api.chainWebpack(webpackConfig => {
  23. const { lintOnSave } = options
  24. const treatAllAsWarnings = lintOnSave === true || lintOnSave === 'warning'
  25. const treatAllAsErrors = lintOnSave === 'error'
  26. const failOnWarning = treatAllAsErrors
  27. const failOnError = !treatAllAsWarnings
  28. /** @type {import('eslint-webpack-plugin').Options & import('eslint').ESLint.Options} */
  29. const eslintWebpackPluginOptions = {
  30. // common to both plugin and ESlint
  31. extensions,
  32. // ESlint options
  33. cwd,
  34. cache: true,
  35. cacheLocation: path.format({
  36. dir: cacheDirectory,
  37. name: process.env.VUE_CLI_TEST
  38. ? 'cache'
  39. : cacheIdentifier,
  40. ext: '.json'
  41. }),
  42. // plugin options
  43. context: cwd,
  44. failOnWarning,
  45. failOnError,
  46. eslintPath: path.dirname(
  47. resolveModule('eslint/package.json', cwd) ||
  48. resolveModule('eslint/package.json', __dirname)
  49. ),
  50. formatter: 'stylish'
  51. }
  52. webpackConfig.plugin('eslint').use(eslintWebpackPlugin, [eslintWebpackPluginOptions])
  53. })
  54. }
  55. api.registerCommand(
  56. 'lint',
  57. {
  58. description: 'lint and fix source files',
  59. usage: 'vue-cli-service lint [options] [...files]',
  60. options: {
  61. '--format [formatter]': 'specify formatter (default: stylish)',
  62. '--no-fix': 'do not fix errors or warnings',
  63. '--no-fix-warnings': 'fix errors, but do not fix warnings',
  64. '--max-errors [limit]':
  65. 'specify number of errors to make build failed (default: 0)',
  66. '--max-warnings [limit]':
  67. 'specify number of warnings to make build failed (default: Infinity)',
  68. '--output-file [file_path]':
  69. 'specify file to write report to'
  70. },
  71. details:
  72. 'For more options, see https://eslint.org/docs/user-guide/command-line-interface#options'
  73. },
  74. async args => {
  75. await require('./lint')(args, api)
  76. }
  77. )
  78. }