Skip to content

Commit

Permalink
[id] for props mode && windows path bug fixed (temporary)
Browse files Browse the repository at this point in the history
  • Loading branch information
2nthony committed Dec 14, 2018
1 parent e276627 commit 544ac50
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions lib/collect-fs-routes.js
Expand Up @@ -27,7 +27,7 @@ class FileCollector {
add(path, props) {
if (!this.lookup[path]) {
this.lookup[path] = {
path: solveRoutePath(path)
path: dyanmicRoutePath(path)
}
this.records.push(this.lookup[path])
}
Expand All @@ -42,12 +42,17 @@ class FileCollector {
}
}

const solveRoutePath = path => path.replace('index', '').replace(/_/g, ':')
const dyanmicRoutePath = path =>
path
.replace('index', '')
.replace(/\_/g, ':')
.replace(/\[(.*?)\]/g, ':$1')


const filenamify = str =>
const chunkNamify = str =>
path
.relative(process.cwd(), str)
.replace(/[^a-zA-Z0-9\-_]/g, '-')
.replace(/[^a-zA-Z0-9\-_\[\]]/g, '-')
.replace(/^-+/, '')

function renderRoutes(routes, { chunkPrefix = '' } = {}) {
Expand All @@ -57,15 +62,31 @@ function renderRoutes(routes, { chunkPrefix = '' } = {}) {
.map(
route => `
{
path: ${JSON.stringify(route.path)},
component: () => import(/* webpackChunkName: "${chunkPrefix}${filenamify(
path: ${
JSON.stringify(route.path.replace(/\\/g, '/'))
},
${
route.component.match(/\[(.*?)\]/g)
? 'props: true,'
: ''
}
component: () => import(/* webpackChunkName: "${
chunkPrefix
}${
chunkNamify(
route.component
)}" */ ${JSON.stringify(route.component)}),
${route.children ? `children: ${renderRoutes(route.children)}` : ''}
)
}" */ ${JSON.stringify(route.component)}),
${
route.children
? `children: ${renderRoutes(route.children)}`
: ''
}
}
`
)
.join(',')}
.join(',')
}
]
`
}
Expand Down

0 comments on commit 544ac50

Please sign in to comment.