This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR fixes #68. It may look huge, but the most part of the PR is the new example directory and tests. - If there is a directory `pages`, its contents will be used to construct a router. This router will be injected by a plugin into entry object. - Dynamic routes can be created with ~~`_param.vue` or~~ `[param].vue` (or `:param.vue`, although not on Windows). - Child routes are created if there are both `some.vue` and `some/` in the same directory. If there's only `some/`, it also works, but adds prefixed routes to common list. - User-provided entry is no more **required**. Ream will work with nothing but `pages/*.vue` (same as Nuxt). If entry is there, it is used. If there's no pages (and no router that came from entry), an exception will be thrown like it used to be (but better as it is more descriptive). - It is possible to disable file system routes, change `pages` to something else, or change supported extensions. (I am also thinking about other options, e.g. `trailingSlashes: true`, or a custom filter function.) - It is possible to disable *entrypoint*. (Not that it's really needed, but it came logically.) I had to make some small changes in `create-app-template.js` to be able to allow plugins to override the router, and to make entry point optional, but most of the code went into a plugin. The default naming `pages` (not `routes` and not `views`) was chosen because I believe it reflects better what users are putting there — pages that will show on their site. (Routes are URL matching rules, and views are from MVC world where there are also controllers in front — with `fsRoutes`, users don't create neither of these.) `write-routes.js` is specifically made to be reusable. In my project, I need to have not one but two file system routers (for project.com and for app.project.com). It is now possible with a small ad-hoc plugin that calls `write-routes.js` twice. `routes-template.js` is decoupled from Ream API for easier testing.
- Loading branch information
1 parent
326e41f
commit 48c3333
Showing
43 changed files
with
1,272 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const fs = require('fs-extra') | ||
|
||
// Poor man hot reload template for the app entry. | ||
// Ideally, this fallback should be handled by webpack. | ||
// However, at the moment there is no way to disable warning emitted by | ||
// require.resolve('#app-entry') when the entry module does not exist. | ||
// | ||
// See discussion in https://github.com/ream/ream/pull/96 | ||
|
||
module.exports = api => { | ||
if ( | ||
!( | ||
api.options.entry && | ||
fs.pathExistsSync(api.resolveBaseDir(api.options.entry)) | ||
) | ||
) { | ||
return `` | ||
} | ||
|
||
return ` | ||
import entry from '#app-entry' | ||
export default entry | ||
` | ||
} |
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,24 @@ | ||
# Auto generates routes | ||
|
||
## How to use | ||
|
||
```bash | ||
git clone https://github.com/ream/ream.git | ||
``` | ||
|
||
Install dependencies: | ||
|
||
```bash | ||
cd examples/fs-routes | ||
yarn | ||
``` | ||
|
||
Run it: | ||
|
||
```bash | ||
# Start development server | ||
yarn dev | ||
|
||
# Start production server | ||
yarn build && yarn start | ||
``` |
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,12 @@ | ||
{ | ||
"private": true, | ||
"name": "fs-routes", | ||
"scripts": { | ||
"dev": "ream dev", | ||
"start": "ream start", | ||
"build": "ream build" | ||
}, | ||
"dependencies": { | ||
"ream": "latest" | ||
} | ||
} |
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,6 @@ | ||
<template> | ||
<div> | ||
<h1>fs-routes example app</h1> | ||
<router-link to="/user">Users</router-link> | ||
</div> | ||
</template> |
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,6 @@ | ||
<template> | ||
<div> | ||
<h1>Users</h1> | ||
<router-view /> | ||
</div> | ||
</template> |
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,20 @@ | ||
<template> | ||
<div> | ||
<h2 v-text="user" /> | ||
<ul> | ||
<li><router-link :to="`/user/${user}`">Profile</router-link></li> | ||
<li><router-link :to="`/user/${user}/friends`">Friends</router-link></li> | ||
</ul> | ||
<router-view /> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
computed: { | ||
user () { | ||
return this.$route.params.user_id | ||
} | ||
} | ||
} | ||
</script> |
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,3 @@ | ||
<template> | ||
<div>User {{ $route.params.user_id }} friends</div> | ||
</template> |
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,3 @@ | ||
<template> | ||
<div>User {{ $route.params.user_id }} profile</div> | ||
</template> |
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,17 @@ | ||
<template> | ||
<div> | ||
<li v-for="user in users" :key="user"> | ||
<router-link :to="`/user/${user}`" v-text="user" /> | ||
</li> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
data () { | ||
return { | ||
users: [ 'mary', 'ann', 'joe' ] | ||
} | ||
} | ||
} | ||
</script> |
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 @@ | ||
Run `WRITE_EXPECTED_ROUTES=1 yarn test` to rewrite `_expected_routes.json` |
1 change: 1 addition & 0 deletions
1
lib/plugins/fs-routes/__test__/presets/empty/_expected_routes.json
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 @@ | ||
[] |
6 changes: 6 additions & 0 deletions
6
lib/plugins/fs-routes/__test__/presets/ignore-garbage/_expected_routes.json
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,6 @@ | ||
[ | ||
{ | ||
"path": "/page", | ||
"component": "#base/page.vue" | ||
} | ||
] |
Empty file.
Empty file.
24 changes: 24 additions & 0 deletions
24
lib/plugins/fs-routes/__test__/presets/nested-children/_expected_routes.json
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,24 @@ | ||
[ | ||
{ | ||
"path": "/foo", | ||
"component": "#base/foo.vue", | ||
"children": [ | ||
{ | ||
"path": "bar", | ||
"component": "#base/foo/bar.vue", | ||
"children": [ | ||
{ | ||
"path": "baz", | ||
"component": "#base/foo/bar/baz.vue", | ||
"children": [ | ||
{ | ||
"path": "qux", | ||
"component": "#base/foo/bar/baz/qux.vue" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] |
Empty file.
Empty file.
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions
6
lib/plugins/fs-routes/__test__/presets/nested/_expected_routes.json
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,6 @@ | ||
[ | ||
{ | ||
"path": "/foo/bar/baz/qux", | ||
"component": "#base/foo/bar/baz/qux.vue" | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
lib/plugins/fs-routes/__test__/presets/nested/foo/bar/baz/qux.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 @@ | ||
|
30 changes: 30 additions & 0 deletions
30
lib/plugins/fs-routes/__test__/presets/typical/_expected_routes.json
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,30 @@ | ||
[ | ||
{ | ||
"path": "/", | ||
"component": "#base/index.vue" | ||
}, | ||
{ | ||
"path": "/foo", | ||
"component": "#base/foo.vue" | ||
}, | ||
{ | ||
"path": "/user", | ||
"component": "#base/user.vue", | ||
"children": [ | ||
{ | ||
"path": ":user", | ||
"component": "#base/user/[user].vue", | ||
"children": [ | ||
{ | ||
"path": "", | ||
"component": "#base/user/[user]/index.vue" | ||
}, | ||
{ | ||
"path": "friends", | ||
"component": "#base/user/[user]/friends.vue" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Oops, something went wrong.