pluginResolution.js 1.8 KB

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