From 0f11b6adabd6937beb87a86cbc4eb1ada23b7614 Mon Sep 17 00:00:00 2001 From: Haoqun Jiang Date: Mon, 18 May 2020 21:20:46 +0800 Subject: [PATCH] fix: allow specifying plugin version when calling `vue add` fixes #4700 --- packages/@vue/cli/lib/add.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/@vue/cli/lib/add.js b/packages/@vue/cli/lib/add.js index 6001baf81e..d0dd93f917 100644 --- a/packages/@vue/cli/lib/add.js +++ b/packages/@vue/cli/lib/add.js @@ -17,7 +17,7 @@ const { } = require('@vue/cli-shared-utils') const confirmIfGitDirty = require('./util/confirmIfGitDirty') -async function add (pluginName, options = {}, context = process.cwd()) { +async function add (pluginToAdd, options = {}, context = process.cwd()) { if (!(await confirmIfGitDirty(context))) { return } @@ -26,14 +26,21 @@ async function add (pluginName, options = {}, context = process.cwd()) { const servicePkg = loadModule('@vue/cli-service/package.json', context) if (servicePkg && semver.satisfies(servicePkg.version, '3.x')) { // special internal "plugins" - if (/^(@vue\/)?router$/.test(pluginName)) { + if (/^(@vue\/)?router$/.test(pluginToAdd)) { return addRouter(context) } - if (/^(@vue\/)?vuex$/.test(pluginName)) { + if (/^(@vue\/)?vuex$/.test(pluginToAdd)) { return addVuex(context) } } + const pluginRe = /^(@?[^@]+)(?:@(.+))?$/ + const [ + // eslint-disable-next-line + _skip, + pluginName, + pluginVersion + ] = pluginToAdd.match(pluginRe) const packageName = resolvePluginId(pluginName) log() @@ -41,9 +48,11 @@ async function add (pluginName, options = {}, context = process.cwd()) { log() const pm = new PackageManager({ context }) - const { latestMinor } = await getVersions() - if (isOfficialPlugin(packageName)) { + if (pluginVersion) { + await pm.add(`${packageName}@${pluginVersion}`) + } else if (isOfficialPlugin(packageName)) { + const { latestMinor } = await getVersions() await pm.add(`${packageName}@~${latestMinor}`) } else { await pm.add(packageName, { tilde: true })