pluginResolution.js 1.9 KB

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