index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const fs = require('fs')
  2. const path = require('path')
  3. module.exports = (api, { config, lintOn = [] }, _, invoking) => {
  4. if (typeof lintOn === 'string') {
  5. lintOn = lintOn.split(',')
  6. }
  7. const eslintConfig = require('../eslintOptions').config(api)
  8. const pkg = {
  9. scripts: {
  10. lint: 'vue-cli-service lint'
  11. },
  12. eslintConfig,
  13. // TODO:
  14. // Move these dependencies to package.json in v4.
  15. // Now in v3 we have to add redundant eslint related dependencies
  16. // in order to keep compatibility with v3.0.x users who defaults to ESlint v4.
  17. devDependencies: {
  18. 'babel-eslint': '^10.0.1',
  19. 'eslint': '^5.16.0',
  20. 'eslint-plugin-vue': '^5.0.0'
  21. }
  22. }
  23. const injectEditorConfig = (config) => {
  24. const filePath = api.resolve('.editorconfig')
  25. if (fs.existsSync(filePath)) {
  26. // Append to existing .editorconfig
  27. api.render(files => {
  28. const configPath = path.resolve(__dirname, `./template/${config}/_editorconfig`)
  29. const editorconfig = fs.readFileSync(configPath, 'utf-8')
  30. files['.editorconfig'] += `\n${editorconfig}`
  31. })
  32. } else {
  33. api.render(`./template/${config}`)
  34. }
  35. }
  36. if (config === 'airbnb') {
  37. eslintConfig.extends.push('@vue/airbnb')
  38. Object.assign(pkg.devDependencies, {
  39. '@vue/eslint-config-airbnb': '^4.0.0'
  40. })
  41. injectEditorConfig('airbnb')
  42. } else if (config === 'standard') {
  43. eslintConfig.extends.push('@vue/standard')
  44. Object.assign(pkg.devDependencies, {
  45. '@vue/eslint-config-standard': '^4.0.0'
  46. })
  47. injectEditorConfig('standard')
  48. } else if (config === 'prettier') {
  49. eslintConfig.extends.push('@vue/prettier')
  50. Object.assign(pkg.devDependencies, {
  51. '@vue/eslint-config-prettier': '^4.0.1'
  52. })
  53. // prettier & default config do not have any style rules
  54. // so no need to generate an editorconfig file
  55. } else {
  56. // default
  57. eslintConfig.extends.push('eslint:recommended')
  58. }
  59. if (!lintOn.includes('save')) {
  60. pkg.vue = {
  61. lintOnSave: false // eslint-loader configured in runtime plugin
  62. }
  63. }
  64. if (lintOn.includes('commit')) {
  65. Object.assign(pkg.devDependencies, {
  66. 'lint-staged': '^8.1.5'
  67. })
  68. pkg.gitHooks = {
  69. 'pre-commit': 'lint-staged'
  70. }
  71. pkg['lint-staged'] = {
  72. '*.{js,vue}': ['vue-cli-service lint', 'git add']
  73. }
  74. }
  75. api.extendPackage(pkg)
  76. // typescript support
  77. if (api.hasPlugin('typescript')) {
  78. applyTS(api)
  79. }
  80. // invoking only
  81. if (invoking) {
  82. if (api.hasPlugin('unit-mocha')) {
  83. // eslint-disable-next-line node/no-extraneous-require
  84. require('@vue/cli-plugin-unit-mocha/generator').applyESLint(api)
  85. } else if (api.hasPlugin('unit-jest')) {
  86. // eslint-disable-next-line node/no-extraneous-require
  87. require('@vue/cli-plugin-unit-jest/generator').applyESLint(api)
  88. }
  89. }
  90. // lint & fix after create to ensure files adhere to chosen config
  91. if (config && config !== 'base') {
  92. api.onCreateComplete(() => {
  93. require('../lint')({ silent: true }, api)
  94. })
  95. }
  96. }
  97. const applyTS = module.exports.applyTS = api => {
  98. api.extendPackage({
  99. eslintConfig: {
  100. extends: ['@vue/typescript'],
  101. parserOptions: {
  102. parser: '@typescript-eslint/parser'
  103. }
  104. },
  105. devDependencies: {
  106. '@vue/eslint-config-typescript': '^4.0.0'
  107. }
  108. })
  109. }