Skip to content

Commit 88e9d46

Browse files
committedJan 8, 2018
feat: router & vuex
1 parent d0de715 commit 88e9d46

File tree

18 files changed

+220
-54
lines changed

18 files changed

+220
-54
lines changed
 

‎package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
],
77
"scripts": {
88
"test": "jest --env node",
9-
"lint": "eslint --fix packages/**/*.js packages/**/bin/* test/**/*.js"
9+
"posttest": "yarn clean",
10+
"lint": "eslint --fix packages/**/*.js packages/**/bin/* test/**/*.js",
11+
"clean": "node scripts/cleanTestDir.js",
12+
"sync": "node scripts/syncDeps.js",
13+
"boot": "node scripts/bootstrap.js"
1014
},
1115
"gitHooks": {
1216
"pre-commit": "lint-staged"

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

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
module.exports = (generatorAPI, options) => {
2-
generatorAPI.render('./template')
3-
generatorAPI.extendPackage({
1+
module.exports = (api, options) => {
2+
api.render('./template')
3+
api.extendPackage({
44
scripts: {
55
'serve': 'vue-cli-service serve' + (
66
// only auto open browser on MacOS where applescript
@@ -28,4 +28,20 @@ module.exports = (generatorAPI, options) => {
2828
'not ie <= 8'
2929
]
3030
})
31+
32+
if (options.router) {
33+
api.extendPackage({
34+
dependencies: {
35+
'vue-router': '^3.0.1'
36+
}
37+
})
38+
}
39+
40+
if (options.vuex) {
41+
api.extendPackage({
42+
dependencies: {
43+
vuex: '^3.0.1'
44+
}
45+
})
46+
}
3147
}

‎packages/@vue/cli-service/generator/template/src/App.vue

+29
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<%_ if (!options.router) { _%>
12
<template>
23
<div id="app">
34
<img src="./assets/logo.png">
@@ -15,6 +16,17 @@ export default {
1516
}
1617
}
1718
</script>
19+
<%_ } else { _%>
20+
<template>
21+
<div id="app">
22+
<div id="nav">
23+
<router-link to="/">Home</router-link> |
24+
<router-link to="/about">About</router-link>
25+
</div>
26+
<router-view/>
27+
</div>
28+
</template>
29+
<%_ } _%>
1830

1931
<style>
2032
#app {
@@ -23,6 +35,23 @@ export default {
2335
-moz-osx-font-smoothing: grayscale;
2436
text-align: center;
2537
color: #2c3e50;
38+
<%_ if (!options.router) { _%>
2639
margin-top: 60px;
40+
<%_ } _%>
41+
}
42+
43+
<%_ if (options.router) { _%>
44+
#nav {
45+
padding: 30px;
46+
}
47+
48+
#nav a {
49+
font-weight: bold;
50+
color: #2c3e50;
51+
}
52+
53+
#nav a.router-link-exact-active {
54+
color: #42b983;
2755
}
56+
<%_ } _%>
2857
</style>
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import Vue from 'vue'
22
import App from './App.vue'
3+
<%_ if (options.router) { _%>
4+
import router from './router'
5+
<%_ } _%>
6+
<%_ if (options.vuex) { _%>
7+
import store from './store'
8+
<%_ } _%>
39

410
Vue.config.productionTip = false
511

612
new Vue({
13+
<%_ if (options.router) { _%>
14+
router,
15+
<%_ } _%>
16+
<%_ if (options.vuex) { _%>
17+
store,
18+
<%_ } _%>
719
render: h => h(App)
820
}).$mount('#app')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<%_ if (options.router) { _%>
2+
import Vue from 'vue'
3+
import Router from 'vue-router'
4+
import Home from './views/Home.vue'
5+
import About from './views/About.vue'
6+
7+
Vue.use(Router)
8+
9+
export default new Router({
10+
routes: [
11+
{
12+
path: '/',
13+
name: 'home',
14+
component: Home
15+
},
16+
{
17+
path: '/about',
18+
name: 'about',
19+
component: About
20+
}
21+
]
22+
})
23+
<%_ } _%>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<%_ if (options.vuex) { _%>
2+
import Vue from 'vue'
3+
import Vuex from 'vuex'
4+
5+
Vue.use(Vuex)
6+
7+
export default new Vuex.Store({
8+
state: {
9+
10+
},
11+
mutations: {
12+
13+
},
14+
actions: {
15+
16+
}
17+
})
18+
<%_ } _%>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<%_ if (options.router) { _%>
2+
<template>
3+
<div class="about">
4+
<h1>This is an about page</h1>
5+
</div>
6+
</template>
7+
<%_ } _%>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<%_ if (options.router) { _%>
2+
<template>
3+
<div class="home">
4+
<img src="../assets/logo.png">
5+
<HelloWorld msg="Welcome to Your Vue.js App"/>
6+
</div>
7+
</template>
8+
9+
<script>
10+
// @ is an alias to /src
11+
import HelloWorld from '@/components/HelloWorld.vue'
12+
13+
export default {
14+
name: 'home',
15+
components: {
16+
HelloWorld
17+
}
18+
}
19+
</script>
20+
<%_ } _%>

‎packages/@vue/cli-service/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@
5858
"yorkie": "^1.0.2"
5959
},
6060
"devDependencies": {
61-
"vue": "^2.5.13"
61+
"vue": "^2.5.13",
62+
"vue-router": "^3.0.1",
63+
"vuex": "^3.0.1"
6264
},
6365
"publishConfig": {
6466
"access": "public"

‎packages/@vue/cli/__tests__/options.spec.js

+7-14
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,12 @@ test('save options (merge)', () => {
5959
bar: { b: 2 }
6060
}
6161
})
62+
})
6263

63-
// deep merge
64-
saveOptions({
65-
plugins: {
66-
foo: { a: 2, c: 3 },
67-
bar: { d: 4 }
68-
}
69-
}, true)
70-
expect(loadOptions()).toEqual({
71-
packageManager: 'yarn',
72-
plugins: {
73-
foo: { a: 2, c: 3 },
74-
bar: { b: 2, d: 4 }
75-
}
76-
})
64+
test('save options (replace)', () => {
65+
const toSave = {
66+
foo: 'bar'
67+
}
68+
saveOptions(toSave, true)
69+
expect(loadOptions()).toEqual(toSave)
7770
})

‎packages/@vue/cli/lib/Creator.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ module.exports = class Creator {
7272

7373
// inject core service
7474
options.plugins['@vue/cli-service'] = {
75-
projectName: name
75+
projectName: name,
76+
router: options.router,
77+
vuex: options.vuex
7678
}
7779

7880
const packageManager = (
@@ -180,7 +182,7 @@ module.exports = class Creator {
180182

181183
// save options
182184
if (answers.mode === 'manual' && answers.save) {
183-
saveOptions(options)
185+
saveOptions(options, true /* replace */)
184186
}
185187

186188
debug('vue:cli-ptions')(options)
@@ -200,7 +202,7 @@ module.exports = class Creator {
200202
}
201203

202204
resolveIntroPrompts () {
203-
const defualtFeatures = formatFeatures(defaults.plugins)
205+
const defualtFeatures = formatFeatures(defaults)
204206
const modePrompt = {
205207
name: 'mode',
206208
type: 'list',
@@ -218,7 +220,7 @@ module.exports = class Creator {
218220
}
219221
const savedOptions = loadOptions()
220222
if (savedOptions.plugins) {
221-
const savedFeatures = formatFeatures(savedOptions.plugins)
223+
const savedFeatures = formatFeatures(savedOptions)
222224
modePrompt.choices.unshift({
223225
name: `Use previously saved config (${savedFeatures})`,
224226
value: 'saved'

‎packages/@vue/cli/lib/GeneratorAPI.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ module.exports = class GeneratorAPI {
6161
})
6262
for (const file of _files) {
6363
const relativePath = path.relative(fileDir, file.path)
64-
files[relativePath] = renderFile(file.path, data, ejsOptions)
64+
const content = renderFile(file.path, data, ejsOptions)
65+
if (Buffer.isBuffer(content) || content.trim()) {
66+
files[relativePath] = content
67+
}
6568
}
6669
})
6770
} else if (isObject(fileDir)) {
@@ -71,7 +74,10 @@ module.exports = class GeneratorAPI {
7174
}, additionalData)
7275
for (const targetPath in fileDir) {
7376
const sourcePath = path.resolve(baseDir, fileDir[targetPath])
74-
files[targetPath] = renderFile(sourcePath, data, ejsOptions)
77+
const content = renderFile(sourcePath, data, ejsOptions)
78+
if (Buffer.isBuffer(content) || content.trim()) {
79+
files[targetPath] = content
80+
}
7581
}
7682
})
7783
} else if (isFunction(fileDir)) {

‎packages/@vue/cli/lib/create.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ async function create (projectName, options) {
3434
}
3535
}
3636

37-
const promptModules = fs
38-
.readdirSync(path.resolve(__dirname, './promptModules'))
39-
.filter(file => file.charAt(0) !== '.' && file.charAt(0) !== '_')
40-
.map(file => require(`./promptModules/${file}`))
37+
const promptModules = [
38+
'babel',
39+
'router',
40+
'vuex',
41+
'eslint',
42+
'unit',
43+
'e2e',
44+
'typescript',
45+
'pwa'
46+
].map(file => require(`./promptModules/${file}`))
4147

4248
const creator = new Creator(projectName, targetDir, promptModules)
4349
await creator.create(options)

‎packages/@vue/cli/lib/options.js

+9-17
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const rcPath = exports.rcPath = (
1414
)
1515

1616
const schema = createSchema(joi => joi.object().keys({
17+
router: joi.boolean(),
18+
vuex: joi.boolean(),
1719
useTaobaoRegistry: joi.any().only([true, false, null]),
1820
packageManager: joi.string().only(['yarn', 'npm']),
1921
plugins: joi.object().required()
@@ -22,6 +24,8 @@ const schema = createSchema(joi => joi.object().keys({
2224
exports.validate = options => validate(options, schema)
2325

2426
exports.defaults = {
27+
router: false,
28+
vuex: false,
2529
useTaobaoRegistry: null,
2630
packageManager: hasYarn ? 'yarn' : 'npm',
2731
plugins: {
@@ -55,12 +59,12 @@ exports.loadOptions = () => {
5559
}
5660
}
5761

58-
exports.saveOptions = (toSave, deep) => {
59-
const options = exports.loadOptions()
60-
if (deep) {
61-
deepMerge(options, toSave)
62+
exports.saveOptions = (toSave, replace) => {
63+
let options
64+
if (replace) {
65+
options = toSave
6266
} else {
63-
Object.assign(options, toSave)
67+
options = Object.assign(exports.loadOptions(), toSave)
6468
}
6569
for (const key in options) {
6670
if (!(key in exports.defaults)) {
@@ -78,15 +82,3 @@ exports.saveOptions = (toSave, deep) => {
7882
)
7983
}
8084
}
81-
82-
const isObject = val => val && typeof val === 'object'
83-
84-
function deepMerge (to, from) {
85-
for (const key in from) {
86-
if (isObject(to[key]) && isObject(from[key])) {
87-
deepMerge(to[key], from[key])
88-
} else {
89-
to[key] = from[key]
90-
}
91-
}
92-
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
11
module.exports = cli => {
2+
cli.injectFeature({
3+
name: 'Router',
4+
value: 'router'
5+
})
6+
7+
cli.onPromptComplete((answers, options) => {
8+
if (answers.features.includes('router')) {
9+
options.router = true
10+
}
11+
})
212
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
11
module.exports = cli => {
2+
cli.injectFeature({
3+
name: 'Vuex',
4+
value: 'vuex'
5+
})
6+
7+
cli.onPromptComplete((answers, options) => {
8+
if (answers.features.includes('vuex')) {
9+
options.vuex = true
10+
}
11+
})
212
}

0 commit comments

Comments
 (0)
Please sign in to comment.