Skip to content

Commit 048003c

Browse files
authored
fix: fix vue add router command in v3 projects (#4698)
fixes #4692
1 parent a759af1 commit 048003c

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

packages/@vue/cli-ui/ui-defaults/suggestions.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
const semver = require('semver')
2+
const { loadModule } = require('@vue/cli-shared-utils')
13
const invoke = require('@vue/cli/lib/invoke')
4+
const add = require('@vue/cli/lib/add')
25

36
const ROUTER = 'org.vue.vue-router-add'
47
const VUEX = 'org.vue.vuex-add'
@@ -77,7 +80,17 @@ async function install (api, id) {
7780
let error
7881

7982
try {
80-
await invoke(id, {}, context)
83+
const servicePkg = loadModule('@vue/cli-service/package.json', context)
84+
// @vue/cli-plugin-router is not compatible with @vue/cli-service v3,
85+
// so we have to check for the version and call the right generator
86+
if (semver.satisfies(servicePkg.version, '3.x')) {
87+
await invoke.runGenerator(context, {
88+
id: `core:${id}`,
89+
apply: loadModule(`@vue/cli-service/generator/${id}`, context)
90+
})
91+
} else {
92+
await add(id, {}, context)
93+
}
8194
} catch (e) {
8295
error = e
8396
}

packages/@vue/cli/lib/add.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const chalk = require('chalk')
22
const semver = require('semver')
33
const invoke = require('./invoke')
4+
const inquirer = require('inquirer')
5+
const { loadModule } = require('@vue/cli-shared-utils')
46

57
const PackageManager = require('./util/ProjectPackageManager')
68
const {
@@ -16,6 +18,18 @@ async function add (pluginName, options = {}, context = process.cwd()) {
1618
return
1719
}
1820

21+
// for `vue add` command in 3.x projects
22+
const servicePkg = loadModule('@vue/cli-service/package.json', context)
23+
if (semver.satisfies(servicePkg.version, '3.x')) {
24+
// special internal "plugins"
25+
if (/^(@vue\/)?router$/.test(pluginName)) {
26+
return addRouter(context)
27+
}
28+
if (/^(@vue\/)?vuex$/.test(pluginName)) {
29+
return addVuex(context)
30+
}
31+
}
32+
1933
const packageName = resolvePluginId(pluginName)
2034

2135
log()
@@ -45,3 +59,23 @@ module.exports = (...args) => {
4559
}
4660
})
4761
}
62+
63+
async function addRouter (context) {
64+
const options = await inquirer.prompt([{
65+
name: 'routerHistoryMode',
66+
type: 'confirm',
67+
message: `Use history mode for router? ${chalk.yellow(`(Requires proper server setup for index fallback in production)`)}`
68+
}])
69+
invoke.runGenerator(context, {
70+
id: 'core:router',
71+
apply: loadModule('@vue/cli-service/generator/router', context),
72+
options
73+
})
74+
}
75+
76+
async function addVuex (context) {
77+
invoke.runGenerator(context, {
78+
id: 'core:vuex',
79+
apply: loadModule('@vue/cli-service/generator/vuex', context)
80+
})
81+
}

0 commit comments

Comments
 (0)