pluginResolution.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const pluginRE = /^(@vue\/|vue-|@[\w-]+(\.)?[\w-]+\/vue-)cli-plugin-/
  2. const scopeRE = /^@[\w-]+(\.)?[\w-]+\//
  3. const officialRE = /^@vue\//
  4. const officialPlugins = [
  5. 'babel',
  6. 'e2e-cypress',
  7. 'e2e-nightwatch',
  8. 'e2e-webdriverio',
  9. 'eslint',
  10. 'pwa',
  11. 'router',
  12. 'typescript',
  13. 'unit-jest',
  14. 'unit-mocha',
  15. 'vuex'
  16. ]
  17. exports.isPlugin = id => pluginRE.test(id)
  18. exports.isOfficialPlugin = id => exports.isPlugin(id) && officialRE.test(id)
  19. exports.toShortPluginId = id => id.replace(pluginRE, '')
  20. exports.resolvePluginId = id => {
  21. // already full id
  22. // e.g. vue-cli-plugin-foo, @vue/cli-plugin-foo, @bar/vue-cli-plugin-foo
  23. if (pluginRE.test(id)) {
  24. return id
  25. }
  26. if (id === '@vue/cli-service') {
  27. return id
  28. }
  29. if (officialPlugins.includes(id)) {
  30. return `@vue/cli-plugin-${id}`
  31. }
  32. // scoped short
  33. // e.g. @vue/foo, @bar/foo
  34. if (id.charAt(0) === '@') {
  35. const scopeMatch = id.match(scopeRE)
  36. if (scopeMatch) {
  37. const scope = scopeMatch[0]
  38. const shortId = id.replace(scopeRE, '')
  39. return `${scope}${scope === '@vue/' ? `` : `vue-`}cli-plugin-${shortId}`
  40. }
  41. }
  42. // default short
  43. // e.g. foo
  44. return `vue-cli-plugin-${id}`
  45. }
  46. exports.matchesPluginId = (input, full) => {
  47. const short = full.replace(pluginRE, '')
  48. return (
  49. // input is full
  50. full === input ||
  51. // input is short without scope
  52. short === input ||
  53. // input is short with scope
  54. short === input.replace(scopeRE, '')
  55. )
  56. }
  57. exports.getPluginLink = id => {
  58. if (officialRE.test(id)) {
  59. return `https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-${
  60. exports.toShortPluginId(id)
  61. }`
  62. }
  63. let pkg = {}
  64. try {
  65. pkg = require(`${id}/package.json`)
  66. } catch (e) {}
  67. return (
  68. pkg.homepage ||
  69. (pkg.repository && pkg.repository.url) ||
  70. `https://www.npmjs.com/package/${id.replace(`/`, `%2F`)}`
  71. )
  72. }