mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
Merge pull request #2990 from David-Mueller/dev
fix: support snake case page names
This commit is contained in:
commit
702a6085d0
@ -304,9 +304,13 @@ export const createRoutes = function createRoutes(files, srcDir, pagesDir) {
|
||||
let route = { name: '', path: '', component: r(srcDir, file) }
|
||||
let parent = routes
|
||||
keys.forEach((key, i) => {
|
||||
// remove underscore only, if its the prefix
|
||||
const sanatizedKey = key.indexOf('_') === 0
|
||||
? key.replace('_', '')
|
||||
: key
|
||||
route.name = route.name
|
||||
? route.name + '-' + key.replace('_', '')
|
||||
: key.replace('_', '')
|
||||
? route.name + '-' + sanatizedKey
|
||||
: sanatizedKey
|
||||
route.name += key === '_' ? 'all' : ''
|
||||
route.chunkName = file.replace(/\.(vue|js)$/, '')
|
||||
let child = _.find(parent, { name: route.name })
|
||||
@ -318,8 +322,13 @@ export const createRoutes = function createRoutes(files, srcDir, pagesDir) {
|
||||
if (key === 'index' && i + 1 === keys.length) {
|
||||
route.path += i > 0 ? '' : '/'
|
||||
} else {
|
||||
route.path += '/' + (key === '_' ? '*' : key.replace('_', ':'))
|
||||
if (key !== '_' && key.indexOf('_') !== -1) {
|
||||
route.path += '/' +
|
||||
(key === '_'
|
||||
? '*'
|
||||
: key.indexOf('_') === 0
|
||||
? key.replace('_', ':')
|
||||
: key)
|
||||
if (key !== '_' && key.indexOf('_') === 0) {
|
||||
route.path += '?'
|
||||
}
|
||||
}
|
||||
|
@ -198,3 +198,43 @@ describe('utils', () => {
|
||||
expect(chainedFn({}, 10)).toEqual({ foo: 11, bar: 12 })
|
||||
})
|
||||
})
|
||||
|
||||
test('createRoutes should allow snake case routes', () => {
|
||||
const files = [
|
||||
'pages/_param.vue',
|
||||
'pages/subpage/_param.vue',
|
||||
'pages/snake_case_route.vue',
|
||||
'pages/another_route/_id.vue'
|
||||
]
|
||||
const srcDir = '/some/nuxt/app'
|
||||
const pagesDir = 'pages'
|
||||
const routesResult = Utils.createRoutes(files, srcDir, pagesDir)
|
||||
const expectedResult = [
|
||||
{
|
||||
name: 'snake_case_route',
|
||||
path: '/snake_case_route',
|
||||
component: Utils.r('/some/nuxt/app/pages/snake_case_route.vue'),
|
||||
chunkName: 'pages/snake_case_route'
|
||||
},
|
||||
{
|
||||
name: 'another_route-id',
|
||||
path: '/another_route/:id?',
|
||||
component: Utils.r('/some/nuxt/app/pages/another_route/_id.vue'),
|
||||
chunkName: 'pages/another_route/_id'
|
||||
},
|
||||
{
|
||||
name: 'subpage-param',
|
||||
path: '/subpage/:param?',
|
||||
component: Utils.r('/some/nuxt/app/pages/subpage/_param.vue'),
|
||||
chunkName: 'pages/subpage/_param'
|
||||
},
|
||||
{
|
||||
name: 'param',
|
||||
path: '/:param?',
|
||||
component: Utils.r('/some/nuxt/app/pages/_param.vue'),
|
||||
chunkName: 'pages/_param'
|
||||
}
|
||||
]
|
||||
|
||||
expect(routesResult).toEqual(expectedResult)
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user