diff --git a/lib/prepare/codegen.js b/lib/prepare/codegen.js index 52e85355a2..766058f4ea 100644 --- a/lib/prepare/codegen.js +++ b/lib/prepare/codegen.js @@ -6,7 +6,7 @@ exports.genRoutesFile = async function ({ sourceDir, pageFiles }) { - function genRoute ({ path: pagePath, key: componentName }, index) { + function genRoute ({ path: pagePath, key: componentName }, index, pages) { const file = pageFiles[index] const filePath = path.resolve(sourceDir, file) let code = ` @@ -14,6 +14,9 @@ exports.genRoutesFile = async function ({ name: ${JSON.stringify(componentName)}, path: ${JSON.stringify(pagePath)}, component: ThemeLayout, + pathToRegexpOptions: { + strict: true + }, beforeEnter: (to, from, next) => { import(${JSON.stringify(filePath)}).then(comp => { Vue.component(${JSON.stringify(componentName)}, comp.default) @@ -37,6 +40,14 @@ exports.genRoutesFile = async function ({ path: ${JSON.stringify(pagePath + 'index.html')}, redirect: ${JSON.stringify(pagePath)} }` + const dirPath = pagePath.replace(/\/$/, '') + if (!pages.find(page => page.path === `${dirPath}.html`) && dirPath) { + code += `, + { + path: ${JSON.stringify(dirPath)}, + redirect: ${JSON.stringify(pagePath)} + }` + } } return code