pluginResolution.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const pluginRE = /^(@vue\/|vue-|@[\w-]+\/vue-)cli-plugin-/
  2. const scopeRE = /^@[\w-]+\//
  3. const officialRE = /^@vue\//
  4. exports.isPlugin = id => pluginRE.test(id)
  5. exports.isOfficialPlugin = id => exports.isPlugin(id) && officialRE.test(id)
  6. exports.toShortPluginId = id => id.replace(pluginRE, '')
  7. exports.resolvePluginId = id => {
  8. // already full id
  9. // e.g. vue-cli-plugin-foo, @vue/cli-plugin-foo, @bar/vue-cli-plugin-foo
  10. if (pluginRE.test(id)) {
  11. return id
  12. }
  13. // scoped short
  14. // e.g. @vue/foo, @bar/foo
  15. if (id.charAt(0) === '@') {
  16. const scopeMatch = id.match(scopeRE)
  17. if (scopeMatch) {
  18. const scope = scopeMatch[0]
  19. const shortId = id.replace(scopeRE, '')
  20. return `${scope}${scope === '@vue/' ? `` : `vue-`}cli-plugin-${shortId}`
  21. }
  22. }
  23. // default short
  24. // e.g. foo
  25. return `vue-cli-plugin-${id}`
  26. }
  27. exports.matchesPluginId = (input, full) => {
  28. const short = full.replace(pluginRE, '')
  29. return (
  30. // input is full
  31. full === input ||
  32. // input is short without scope
  33. short === input ||
  34. // input is short with scope
  35. short === input.replace(scopeRE, '')
  36. )
  37. }
  38. exports.getPluginLink = id => {
  39. if (officialRE.test(id)) {
  40. return `https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-${
  41. exports.toShortPluginId(id)
  42. }`
  43. }
  44. let pkg = {}
  45. try {
  46. pkg = require(`${id}/package.json`)
  47. } catch (e) {}
  48. return (
  49. pkg.homepage ||
  50. (pkg.repository && pkg.repository.url) ||
  51. `https://www.npmjs.com/package/${id.replace(`/`, `%2F`)}`
  52. )
  53. }