forked from vuejs/vue-cli
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: make router a separate plugin (vuejs#4196)
* refactor: move router to its own plugin * refactor: rename routerHistoryMode option to historyMode * test: add @vue/cli-plugin-router tests * feat: change src/router.js for most common use cases * fix: fix cli-ui tests * docs: Remove router root option from docs * fix: add support for legacy router option
- Loading branch information
1 parent
9eadfe1
commit 246ae67
Showing
33 changed files
with
305 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
__tests__ | ||
__mocks__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# @vue/cli-plugin-router | ||
|
||
> router plugin for vue-cli | ||
## Installing in an Already Created Project | ||
|
||
``` sh | ||
vue add @vue/router | ||
``` |
64 changes: 64 additions & 0 deletions
64
packages/@vue/cli-plugin-router/__tests__/routerGenerator.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const generateWithPlugin = require('@vue/cli-test-utils/generateWithPlugin') | ||
|
||
test('base', async () => { | ||
const { files, pkg } = await generateWithPlugin({ | ||
id: 'router', | ||
apply: require('../generator'), | ||
options: {} | ||
}) | ||
|
||
expect(files['src/router/index.js']).toBeTruthy() | ||
expect(files['src/router/index.js']).not.toMatch('history') | ||
expect(files['src/views/About.vue']).toBeTruthy() | ||
expect(files['src/views/Home.vue']).toBeTruthy() | ||
expect(files['src/App.vue']).toMatch('<router-link to="/">Home</router-link>') | ||
expect(files['src/App.vue']).not.toMatch('<script>') | ||
expect(files['src/App.vue']).toMatch('#nav a.router-link-exact-active') | ||
|
||
expect(pkg.dependencies).toHaveProperty('vue-router') | ||
}) | ||
|
||
test('history mode', async () => { | ||
const { files, pkg } = await generateWithPlugin({ | ||
id: 'router', | ||
apply: require('../generator'), | ||
options: { | ||
historyMode: true | ||
} | ||
}) | ||
|
||
expect(files['src/router/index.js']).toBeTruthy() | ||
expect(files['src/router/index.js']).toMatch('history') | ||
expect(files['src/views/About.vue']).toBeTruthy() | ||
expect(files['src/views/Home.vue']).toBeTruthy() | ||
expect(files['src/App.vue']).toMatch('<router-link to="/">Home</router-link>') | ||
expect(files['src/App.vue']).not.toMatch('<script>') | ||
expect(files['src/App.vue']).toMatch('#nav a.router-link-exact-active') | ||
|
||
expect(pkg.dependencies).toHaveProperty('vue-router') | ||
}) | ||
|
||
test('use with Babel', async () => { | ||
const { pkg, files } = await generateWithPlugin([ | ||
{ | ||
id: 'babel', | ||
apply: require('@vue/cli-plugin-babel/generator'), | ||
options: {} | ||
}, | ||
{ | ||
id: 'router', | ||
apply: require('../generator'), | ||
options: {} | ||
} | ||
]) | ||
|
||
expect(files['src/router/index.js']).toBeTruthy() | ||
expect(files['src/router/index.js']).toMatch('component: () => import') | ||
expect(files['src/views/About.vue']).toBeTruthy() | ||
expect(files['src/views/Home.vue']).toBeTruthy() | ||
expect(files['src/App.vue']).toMatch('<router-link to="/">Home</router-link>') | ||
expect(files['src/App.vue']).not.toMatch('<script>') | ||
expect(files['src/App.vue']).toMatch('#nav a.router-link-exact-active') | ||
|
||
expect(pkg.dependencies).toHaveProperty('vue-router') | ||
}) |
24 changes: 6 additions & 18 deletions
24
...vue/cli-service/generator/router/index.js → ...@vue/cli-plugin-router/generator/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/@vue/cli-plugin-router/generator/template/src/App.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- | ||
extend: '@vue/cli-service/generator/template/src/App.vue' | ||
replace: | ||
- !!js/regexp /<template>[^]*?<\/template>/ | ||
- !!js/regexp /\n<script>[^]*?<\/script>\n/ | ||
- !!js/regexp / margin-top[^]*?<\/style>/ | ||
--- | ||
<%# REPLACE %> | ||
<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> | ||
<%# END_REPLACE %> | ||
<%# REPLACE %> | ||
<%# END_REPLACE %> | ||
<%# REPLACE %> | ||
} | ||
<%_ if (rootOptions.cssPreprocessor !== 'stylus') { _%> | ||
<%_ if (!rootOptions.cssPreprocessor) { _%> | ||
#nav { | ||
padding: 30px; | ||
} | ||
#nav a { | ||
font-weight: bold; | ||
color: #2c3e50; | ||
} | ||
#nav a.router-link-exact-active { | ||
color: #42b983; | ||
} | ||
<%_ } else { _%> | ||
#nav { | ||
padding: 30px; | ||
a { | ||
font-weight: bold; | ||
color: #2c3e50; | ||
&.router-link-exact-active { | ||
color: #42b983; | ||
} | ||
} | ||
} | ||
<%_ } _%> | ||
<%_ } else { _%> | ||
#nav | ||
padding 30px | ||
a | ||
font-weight bold | ||
color #2c3e50 | ||
&.router-link-exact-active | ||
color #42b983 | ||
<%_ } _%> | ||
</style> | ||
<%# END_REPLACE %> |
37 changes: 37 additions & 0 deletions
37
packages/@vue/cli-plugin-router/generator/template/src/router/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import Vue from 'vue' | ||
import VueRouter from 'vue-router' | ||
import Home from '../views/Home.vue' | ||
|
||
Vue.use(VueRouter) | ||
|
||
const routes = [ | ||
{ | ||
path: '/', | ||
name: 'home', | ||
component: Home | ||
}, | ||
{ | ||
path: '/about', | ||
name: 'about', | ||
// route level code-splitting | ||
// this generates a separate chunk (about.[hash].js) for this route | ||
// which is lazy-loaded when the route is visited. | ||
<%_ if (doesCompile) { _%> | ||
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue') | ||
<%_ } else { _%> | ||
component: function () { | ||
return import(/* webpackChunkName: "about" */ '../views/About.vue') | ||
} | ||
<%_ } _%> | ||
} | ||
] | ||
|
||
const router = new VueRouter({ | ||
<%_ if (historyMode) { _%> | ||
mode: 'history', | ||
base: process.env.BASE_URL, | ||
<%_ } _%> | ||
routes | ||
}) | ||
|
||
export default router |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = (api, options = {}) => {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "@vue/cli-plugin-router", | ||
"version": "4.0.0-alpha.3", | ||
"description": "router plugin for vue-cli", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/vuejs/vue-cli.git", | ||
"directory": "packages/@vue/cli-plugin-router" | ||
}, | ||
"keywords": [ | ||
"vue", | ||
"cli", | ||
"router" | ||
], | ||
"author": "Evan You", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/vuejs/vue-cli/issues" | ||
}, | ||
"homepage": "https://github.com/vuejs/vue-cli/tree/dev/packages/@vue/cli-plugin-router#readme", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"@vue/cli-shared-utils": "^4.0.0-alpha.3" | ||
}, | ||
"devDependencies": { | ||
"@vue/cli-test-utils": "^4.0.0-alpha.3" | ||
} | ||
} |
Oops, something went wrong.