index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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': '^5.0.0',
  52. 'eslint-plugin-prettier': '^3.1.0',
  53. prettier: '^1.18.2'
  54. })
  55. // prettier & default config do not have any style rules
  56. // so no need to generate an editorconfig file
  57. } else {
  58. // default
  59. eslintConfig.extends.push('eslint:recommended')
  60. }
  61. if (!lintOn.includes('save')) {
  62. pkg.vue = {
  63. lintOnSave: false // eslint-loader configured in runtime plugin
  64. }
  65. }
  66. if (lintOn.includes('commit')) {
  67. Object.assign(pkg.devDependencies, {
  68. 'lint-staged': '^8.1.5'
  69. })
  70. pkg.gitHooks = {
  71. 'pre-commit': 'lint-staged'
  72. }
  73. if (api.hasPlugin('typescript')) {
  74. pkg['lint-staged'] = {
  75. '*.{js,vue,ts}': ['vue-cli-service lint', 'git add']
  76. }
  77. } else {
  78. pkg['lint-staged'] = {
  79. '*.{js,vue}': ['vue-cli-service lint', 'git add']
  80. }
  81. }
  82. }
  83. api.extendPackage(pkg)
  84. // typescript support
  85. if (api.hasPlugin('typescript')) {
  86. applyTS(api)
  87. }
  88. // invoking only
  89. if (invoking) {
  90. if (api.hasPlugin('unit-mocha')) {
  91. // eslint-disable-next-line node/no-extraneous-require
  92. require('@vue/cli-plugin-unit-mocha/generator').applyESLint(api)
  93. } else if (api.hasPlugin('unit-jest')) {
  94. // eslint-disable-next-line node/no-extraneous-require
  95. require('@vue/cli-plugin-unit-jest/generator').applyESLint(api)
  96. }
  97. }
  98. // lint & fix after create to ensure files adhere to chosen config
  99. if (config && config !== 'base') {
  100. api.onCreateComplete(() => {
  101. require('../lint')({ silent: true }, api)
  102. })
  103. }
  104. }
  105. const applyTS = module.exports.applyTS = api => {
  106. api.extendPackage({
  107. eslintConfig: {
  108. extends: ['@vue/typescript'],
  109. parserOptions: {
  110. parser: '@typescript-eslint/parser'
  111. }
  112. },
  113. devDependencies: {
  114. '@vue/eslint-config-typescript': '^4.0.0'
  115. }
  116. })
  117. }