index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. const fs = require('fs')
  2. const path = require('path')
  3. module.exports = (api, { config, lintOn = [] }, rootOptions, invoking) => {
  4. const eslintConfig = require('../eslintOptions').config(api, config, rootOptions)
  5. const devDependencies = require('../eslintDeps').getDeps(api, config, rootOptions)
  6. const pkg = {
  7. scripts: {
  8. lint: 'vue-cli-service lint'
  9. },
  10. eslintConfig,
  11. devDependencies
  12. }
  13. const editorConfigTemplatePath = path.resolve(__dirname, `./template/${config}/_editorconfig`)
  14. if (fs.existsSync(editorConfigTemplatePath)) {
  15. if (fs.existsSync(api.resolve('.editorconfig'))) {
  16. // Append to existing .editorconfig
  17. api.render(files => {
  18. const editorconfig = fs.readFileSync(editorConfigTemplatePath, 'utf-8')
  19. files['.editorconfig'] += `\n${editorconfig}`
  20. })
  21. } else {
  22. api.render(`./template/${config}`)
  23. }
  24. }
  25. if (typeof lintOn === 'string') {
  26. lintOn = lintOn.split(',')
  27. }
  28. if (!lintOn.includes('save')) {
  29. pkg.vue = {
  30. lintOnSave: false // eslint-loader configured in runtime plugin
  31. }
  32. }
  33. if (lintOn.includes('commit')) {
  34. Object.assign(pkg.devDependencies, {
  35. 'lint-staged': '^11.1.2'
  36. })
  37. pkg.gitHooks = {
  38. 'pre-commit': 'lint-staged'
  39. }
  40. const extensions = require('../eslintOptions').extensions(api)
  41. .map(ext => ext.replace(/^\./, '')) // remove the leading `.`
  42. pkg['lint-staged'] = {
  43. [`*.{${extensions.join(',')}}`]: 'vue-cli-service lint'
  44. }
  45. }
  46. api.extendPackage(pkg)
  47. // invoking only
  48. if (invoking) {
  49. if (api.hasPlugin('unit-mocha')) {
  50. // eslint-disable-next-line node/no-extraneous-require
  51. require('@vue/cli-plugin-unit-mocha/generator').applyESLint(api)
  52. } else if (api.hasPlugin('unit-jest')) {
  53. // eslint-disable-next-line node/no-extraneous-require
  54. require('@vue/cli-plugin-unit-jest/generator').applyESLint(api)
  55. }
  56. }
  57. // lint & fix after create to ensure files adhere to chosen config
  58. // for older versions that do not support the `hooks` feature
  59. try {
  60. api.assertCliVersion('^4.0.0-beta.0')
  61. } catch (e) {
  62. if (config && config !== 'base') {
  63. api.onCreateComplete(async () => {
  64. await require('../lint')({ silent: true }, api)
  65. })
  66. }
  67. }
  68. }
  69. // In PNPM v4, due to their implementation of the module resolution mechanism,
  70. // put require('../lint') in the callback would raise a "Module not found" error,
  71. // But we cannot cache the file outside the callback,
  72. // because the node_module layout may change after the "intall additional dependencies"
  73. // phase, thus making the cached module fail to execute.
  74. // FIXME: at the moment we have to catch the bug and silently fail. Need to fix later.
  75. module.exports.hooks = (api) => {
  76. // lint & fix after create to ensure files adhere to chosen config
  77. api.afterAnyInvoke(async () => {
  78. try {
  79. await require('../lint')({ silent: true }, api)
  80. } catch (e) {}
  81. })
  82. }
  83. // exposed for the typescript plugin
  84. module.exports.applyTS = api => {
  85. api.extendPackage({
  86. eslintConfig: {
  87. extends: ['@vue/typescript'],
  88. parserOptions: {
  89. parser: '@typescript-eslint/parser'
  90. }
  91. },
  92. devDependencies: require('../eslintDeps').DEPS_MAP.typescript
  93. })
  94. }