Skip to content

Commit

Permalink
feat: router & vuex
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 8, 2018
1 parent d0de715 commit 88e9d46
Show file tree
Hide file tree
Showing 18 changed files with 220 additions and 54 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
],
"scripts": {
"test": "jest --env node",
"lint": "eslint --fix packages/**/*.js packages/**/bin/* test/**/*.js"
"posttest": "yarn clean",
"lint": "eslint --fix packages/**/*.js packages/**/bin/* test/**/*.js",
"clean": "node scripts/cleanTestDir.js",
"sync": "node scripts/syncDeps.js",
"boot": "node scripts/bootstrap.js"
},
"gitHooks": {
"pre-commit": "lint-staged"
Expand Down
22 changes: 19 additions & 3 deletions packages/@vue/cli-service/generator/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = (generatorAPI, options) => {
generatorAPI.render('./template')
generatorAPI.extendPackage({
module.exports = (api, options) => {
api.render('./template')
api.extendPackage({
scripts: {
'serve': 'vue-cli-service serve' + (
// only auto open browser on MacOS where applescript
Expand Down Expand Up @@ -28,4 +28,20 @@ module.exports = (generatorAPI, options) => {
'not ie <= 8'
]
})

if (options.router) {
api.extendPackage({
dependencies: {
'vue-router': '^3.0.1'
}
})
}

if (options.vuex) {
api.extendPackage({
dependencies: {
vuex: '^3.0.1'
}
})
}
}
29 changes: 29 additions & 0 deletions packages/@vue/cli-service/generator/template/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<%_ if (!options.router) { _%>
<template>
<div id="app">
<img src="./assets/logo.png">
Expand All @@ -15,6 +16,17 @@ export default {
}
}
</script>
<%_ } else { _%>
<template>
<div id="app">
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
</div>
<router-view/>
</div>
</template>
<%_ } _%>

<style>
#app {
Expand All @@ -23,6 +35,23 @@ export default {
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
<%_ if (!options.router) { _%>
margin-top: 60px;
<%_ } _%>
}
<%_ if (options.router) { _%>
#nav {
padding: 30px;
}
#nav a {
font-weight: bold;
color: #2c3e50;
}
#nav a.router-link-exact-active {
color: #42b983;
}
<%_ } _%>
</style>
12 changes: 12 additions & 0 deletions packages/@vue/cli-service/generator/template/src/main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import Vue from 'vue'
import App from './App.vue'
<%_ if (options.router) { _%>
import router from './router'
<%_ } _%>
<%_ if (options.vuex) { _%>
import store from './store'
<%_ } _%>

Vue.config.productionTip = false

new Vue({
<%_ if (options.router) { _%>
router,
<%_ } _%>
<%_ if (options.vuex) { _%>
store,
<%_ } _%>
render: h => h(App)
}).$mount('#app')
23 changes: 23 additions & 0 deletions packages/@vue/cli-service/generator/template/src/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<%_ if (options.router) { _%>
import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
import About from './views/About.vue'

Vue.use(Router)

export default new Router({
routes: [
{
path: '/',
name: 'home',
component: Home
},
{
path: '/about',
name: 'about',
component: About
}
]
})
<%_ } _%>
18 changes: 18 additions & 0 deletions packages/@vue/cli-service/generator/template/src/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<%_ if (options.vuex) { _%>
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
state: {

},
mutations: {

},
actions: {

}
})
<%_ } _%>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%_ if (options.router) { _%>
<template>
<div class="about">
<h1>This is an about page</h1>
</div>
</template>
<%_ } _%>
20 changes: 20 additions & 0 deletions packages/@vue/cli-service/generator/template/src/views/Home.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<%_ if (options.router) { _%>
<template>
<div class="home">
<img src="../assets/logo.png">
<HelloWorld msg="Welcome to Your Vue.js App"/>
</div>
</template>

<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'
export default {
name: 'home',
components: {
HelloWorld
}
}
</script>
<%_ } _%>
4 changes: 3 additions & 1 deletion packages/@vue/cli-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
"yorkie": "^1.0.2"
},
"devDependencies": {
"vue": "^2.5.13"
"vue": "^2.5.13",
"vue-router": "^3.0.1",
"vuex": "^3.0.1"
},
"publishConfig": {
"access": "public"
Expand Down
21 changes: 7 additions & 14 deletions packages/@vue/cli/__tests__/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,12 @@ test('save options (merge)', () => {
bar: { b: 2 }
}
})
})

// deep merge
saveOptions({
plugins: {
foo: { a: 2, c: 3 },
bar: { d: 4 }
}
}, true)
expect(loadOptions()).toEqual({
packageManager: 'yarn',
plugins: {
foo: { a: 2, c: 3 },
bar: { b: 2, d: 4 }
}
})
test('save options (replace)', () => {
const toSave = {
foo: 'bar'
}
saveOptions(toSave, true)
expect(loadOptions()).toEqual(toSave)
})
10 changes: 6 additions & 4 deletions packages/@vue/cli/lib/Creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ module.exports = class Creator {

// inject core service
options.plugins['@vue/cli-service'] = {
projectName: name
projectName: name,
router: options.router,
vuex: options.vuex
}

const packageManager = (
Expand Down Expand Up @@ -180,7 +182,7 @@ module.exports = class Creator {

// save options
if (answers.mode === 'manual' && answers.save) {
saveOptions(options)
saveOptions(options, true /* replace */)
}

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

resolveIntroPrompts () {
const defualtFeatures = formatFeatures(defaults.plugins)
const defualtFeatures = formatFeatures(defaults)
const modePrompt = {
name: 'mode',
type: 'list',
Expand All @@ -218,7 +220,7 @@ module.exports = class Creator {
}
const savedOptions = loadOptions()
if (savedOptions.plugins) {
const savedFeatures = formatFeatures(savedOptions.plugins)
const savedFeatures = formatFeatures(savedOptions)
modePrompt.choices.unshift({
name: `Use previously saved config (${savedFeatures})`,
value: 'saved'
Expand Down
10 changes: 8 additions & 2 deletions packages/@vue/cli/lib/GeneratorAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ module.exports = class GeneratorAPI {
})
for (const file of _files) {
const relativePath = path.relative(fileDir, file.path)
files[relativePath] = renderFile(file.path, data, ejsOptions)
const content = renderFile(file.path, data, ejsOptions)
if (Buffer.isBuffer(content) || content.trim()) {
files[relativePath] = content
}
}
})
} else if (isObject(fileDir)) {
Expand All @@ -71,7 +74,10 @@ module.exports = class GeneratorAPI {
}, additionalData)
for (const targetPath in fileDir) {
const sourcePath = path.resolve(baseDir, fileDir[targetPath])
files[targetPath] = renderFile(sourcePath, data, ejsOptions)
const content = renderFile(sourcePath, data, ejsOptions)
if (Buffer.isBuffer(content) || content.trim()) {
files[targetPath] = content
}
}
})
} else if (isFunction(fileDir)) {
Expand Down
14 changes: 10 additions & 4 deletions packages/@vue/cli/lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,16 @@ async function create (projectName, options) {
}
}

const promptModules = fs
.readdirSync(path.resolve(__dirname, './promptModules'))
.filter(file => file.charAt(0) !== '.' && file.charAt(0) !== '_')
.map(file => require(`./promptModules/${file}`))
const promptModules = [
'babel',
'router',
'vuex',
'eslint',
'unit',
'e2e',
'typescript',
'pwa'
].map(file => require(`./promptModules/${file}`))

const creator = new Creator(projectName, targetDir, promptModules)
await creator.create(options)
Expand Down
26 changes: 9 additions & 17 deletions packages/@vue/cli/lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const rcPath = exports.rcPath = (
)

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

exports.defaults = {
router: false,
vuex: false,
useTaobaoRegistry: null,
packageManager: hasYarn ? 'yarn' : 'npm',
plugins: {
Expand Down Expand Up @@ -55,12 +59,12 @@ exports.loadOptions = () => {
}
}

exports.saveOptions = (toSave, deep) => {
const options = exports.loadOptions()
if (deep) {
deepMerge(options, toSave)
exports.saveOptions = (toSave, replace) => {
let options
if (replace) {
options = toSave
} else {
Object.assign(options, toSave)
options = Object.assign(exports.loadOptions(), toSave)
}
for (const key in options) {
if (!(key in exports.defaults)) {
Expand All @@ -78,15 +82,3 @@ exports.saveOptions = (toSave, deep) => {
)
}
}

const isObject = val => val && typeof val === 'object'

function deepMerge (to, from) {
for (const key in from) {
if (isObject(to[key]) && isObject(from[key])) {
deepMerge(to[key], from[key])
} else {
to[key] = from[key]
}
}
}
10 changes: 10 additions & 0 deletions packages/@vue/cli/lib/promptModules/router.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
module.exports = cli => {
cli.injectFeature({
name: 'Router',
value: 'router'
})

cli.onPromptComplete((answers, options) => {
if (answers.features.includes('router')) {
options.router = true
}
})
}
10 changes: 10 additions & 0 deletions packages/@vue/cli/lib/promptModules/vuex.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
module.exports = cli => {
cli.injectFeature({
name: 'Vuex',
value: 'vuex'
})

cli.onPromptComplete((answers, options) => {
if (answers.features.includes('vuex')) {
options.vuex = true
}
})
}
Loading

0 comments on commit 88e9d46

Please sign in to comment.