Skip to content

Commit 2a195f0

Browse files
committed
feat: allow router/vuex to be late added via vue add
close #1202, close #1204
1 parent 8b32f4a commit 2a195f0

File tree

10 files changed

+82
-30
lines changed

10 files changed

+82
-30
lines changed

packages/@vue/cli-plugin-typescript/generator/template/src/views/Home.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
extend: '@vue/cli-service/generator/template/src/views/Home.vue'
2+
extend: '@vue/cli-service/generator/router/template/src/views/Home.vue'
33
replace:
44
- !!js/regexp /Welcome to Your Vue\.js App/
55
- !!js/regexp /<script>[^]*?<\/script>/

packages/@vue/cli-service/generator/index.js

+2-14
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,11 @@ module.exports = (api, options) => {
3131
})
3232

3333
if (options.router) {
34-
api.injectImports(`src/main.js`, `import router from './router'`)
35-
api.injectRootOptions(`src/main.js`, `router`)
36-
api.extendPackage({
37-
dependencies: {
38-
'vue-router': '^3.0.1'
39-
}
40-
})
34+
require('./router')(api, options)
4135
}
4236

4337
if (options.vuex) {
44-
api.injectImports(`import store from './store'`)
45-
api.injectRootOptions(`store`)
46-
api.extendPackage({
47-
dependencies: {
48-
vuex: '^3.0.1'
49-
}
50-
})
38+
require('./vuex')(api, options)
5139
}
5240

5341
if (options.cssPreprocessor) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module.exports = (api, options) => {
2+
api.injectImports(`src/main.js`, `import router from './router'`)
3+
api.injectRootOptions(`src/main.js`, `router`)
4+
api.extendPackage({
5+
dependencies: {
6+
'vue-router': '^3.0.1'
7+
}
8+
})
9+
api.render('./template')
10+
11+
if (options.invoking) {
12+
api.postProcessFiles(files => {
13+
const appFile = files[`src/App.vue`]
14+
if (appFile) {
15+
files[`src/App.vue`] = appFile.replace(/^<template>[^]+<\/script>/, `
16+
<template>
17+
<div id="app">
18+
<div id="nav">
19+
<router-link to="/">Home</router-link> |
20+
<router-link to="/about">About</router-link>
21+
</div>
22+
<router-view/>
23+
</div>
24+
</template>
25+
`.trim())
26+
console.log(files[`src/App.vue`])
27+
}
28+
})
29+
}
30+
}

packages/@vue/cli-service/generator/template/src/router.js renamed to packages/@vue/cli-service/generator/router/template/src/router.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<%_ if (rootOptions.router) { _%>
21
import Vue from 'vue'
32
import Router from 'vue-router'
43
import Home from './views/Home.vue'
@@ -20,4 +19,3 @@ export default new Router({
2019
}
2120
]
2221
})
23-
<%_ } _%>
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
<%_ if (rootOptions.router) { _%>
21
<template>
32
<div class="about">
43
<h1>This is an about page</h1>
54
</div>
65
</template>
7-
<%_ } _%>

packages/@vue/cli-service/generator/template/src/views/Home.vue renamed to packages/@vue/cli-service/generator/router/template/src/views/Home.vue

-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<%_ if (rootOptions.router) { _%>
21
<template>
32
<div class="home">
43
<img src="../assets/logo.png">
@@ -17,4 +16,3 @@ export default {
1716
}
1817
}
1918
</script>
20-
<%_ } _%>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = (api, options) => {
2+
api.injectImports(`src/main.js`, `import store from './store'`)
3+
api.injectRootOptions(`src/main.js`, `store`)
4+
api.extendPackage({
5+
dependencies: {
6+
vuex: '^3.0.1'
7+
}
8+
})
9+
api.render('./template')
10+
}

packages/@vue/cli-service/generator/template/src/store.js renamed to packages/@vue/cli-service/generator/vuex/template/src/store.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<%_ if (rootOptions.vuex) { _%>
21
import Vue from 'vue'
32
import Vuex from 'vuex'
43

@@ -15,4 +14,3 @@ export default new Vuex.Store({
1514

1615
}
1716
})
18-
<%_ } _%>

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

+24
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ const {
1212
} = require('@vue/cli-shared-utils')
1313

1414
async function add (pluginName, options = {}, context = process.cwd()) {
15+
// special internal "plugins"
16+
if (/^(@vue\/)?router$/.test(pluginName)) {
17+
return addRouter(context)
18+
}
19+
if (/^(@vue\/)?vuex$/.test(pluginName)) {
20+
return addVuex(context)
21+
}
22+
1523
const packageName = resolvePluginId(pluginName)
1624

1725
log()
@@ -35,6 +43,22 @@ async function add (pluginName, options = {}, context = process.cwd()) {
3543
}
3644
}
3745

46+
async function addRouter (context) {
47+
invoke.runGenerator(context, {
48+
id: 'core:router',
49+
apply: require('@vue/cli-service/generator/router'),
50+
options: { invoking: true }
51+
})
52+
}
53+
54+
async function addVuex (context) {
55+
invoke.runGenerator(context, {
56+
id: 'core:vuex',
57+
apply: require('@vue/cli-service/generator/vuex'),
58+
options: { invoking: true }
59+
})
60+
}
61+
3862
module.exports = (...args) => {
3963
return add(...args).catch(err => {
4064
error(err)

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

+15-7
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,17 @@ async function readFiles (context) {
3636
return res
3737
}
3838

39-
async function invoke (pluginName, options = {}, context = process.cwd()) {
40-
delete options._
39+
function getPkg (context) {
4140
const pkgPath = path.resolve(context, 'package.json')
42-
const isTestOrDebug = process.env.VUE_CLI_TEST || process.env.VUE_CLI_DEBUG
43-
4441
if (!fs.existsSync(pkgPath)) {
4542
throw new Error(`package.json not found in ${chalk.yellow(context)}`)
4643
}
44+
return require(pkgPath)
45+
}
4746

48-
const pkg = require(pkgPath)
47+
async function invoke (pluginName, options = {}, context = process.cwd()) {
48+
delete options._
49+
const pkg = getPkg(context)
4950

5051
// attempt to locate the plugin in package.json
5152
const findPlugin = deps => {
@@ -89,6 +90,11 @@ async function invoke (pluginName, options = {}, context = process.cwd()) {
8990
options
9091
}
9192

93+
await runGenerator(context, plugin, pkg)
94+
}
95+
96+
async function runGenerator (context, plugin, pkg = getPkg(context)) {
97+
const isTestOrDebug = process.env.VUE_CLI_TEST || process.env.VUE_CLI_DEBUG
9298
const createCompleteCbs = []
9399
const generator = new Generator(context, {
94100
pkg,
@@ -98,7 +104,7 @@ async function invoke (pluginName, options = {}, context = process.cwd()) {
98104
})
99105

100106
log()
101-
logWithSpinner('🚀', `Invoking generator for ${id}...`)
107+
logWithSpinner('🚀', `Invoking generator for ${plugin.id}...`)
102108
await generator.generate({
103109
extractConfigFiles: true,
104110
checkExisting: true
@@ -127,7 +133,7 @@ async function invoke (pluginName, options = {}, context = process.cwd()) {
127133
stopSpinner()
128134

129135
log()
130-
log(` Successfully invoked generator for plugin: ${chalk.cyan(id)}`)
136+
log(` Successfully invoked generator for plugin: ${chalk.cyan(plugin.id)}`)
131137
if (!process.env.VUE_CLI_TEST && hasGit()) {
132138
const { stdout } = await execa('git', [
133139
'ls-files',
@@ -166,3 +172,5 @@ module.exports = (...args) => {
166172
}
167173
})
168174
}
175+
176+
module.exports.runGenerator = runGenerator

0 commit comments

Comments
 (0)