-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Make router a separate plugin #4196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b6f5c8f
refactor: move router to it's own plugin
pksunkara 31427bc
refactor: rename routerHistoryMode option to historyMode
pksunkara fc2c31e
test: add @vue/cli-plugin-router tests
pksunkara 3cc5e04
feat: change src/router.js for most common use cases
pksunkara 7303320
fix: fix cli-ui tests
pksunkara 1245426
docs: Remove router root option from docs
pksunkara 613da0a
fix: add support for legacy router option
pksunkara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||
pksunkara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` |
64 changes: 64 additions & 0 deletions
64
packages/@vue/cli-plugin-router/__tests__/routerGenerator.spec.js
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though we now have a standalone router plugin, the legacy preset format (
router
androuterHistoryMode
option) can and should still be supported, so as to reduce the migration costs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added support for legacy router option but have it removed from docs completely.