diff --git a/lib/app/app.js b/lib/app/app.js index dc03a7a7bbabbca5214dd9a9f8b1a7a82a6d8606..01fa4cac48034e8011fb00c60b01ece5d27fdb3b 100644 --- a/lib/app/app.js +++ b/lib/app/app.js @@ -32,7 +32,16 @@ export function createApp () { fallback: false, linkActiveClass: '', linkExactActiveClass: '', - routes + routes, + scrollBehavior: (to, from, saved) => { + if (saved) { + return saved + } else if (to.hash) { + return { selector: to.hash } + } else { + return { x: 0, y: 0 } + } + } }) const app = new Vue({ diff --git a/lib/prepare.js b/lib/prepare.js index bb61683acff55edc3e94745e01fc28b52fa7c18d..25b922c37990078aade06e3d0971c5402cedb689 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -135,20 +135,16 @@ async function resolveOptions (sourceDir) { return options } -async function genComponentRegistrationFile ({ sourceDir, pageFiles }) { +async function genComponentRegistrationFile ({ sourceDir }) { function genImport (file) { const name = fileToComponentName(file) - const baseDir = /\.md$/.test(file) - ? sourceDir - : path.resolve(sourceDir, '.vuepress/components') + const baseDir = path.resolve(sourceDir, '.vuepress/components') const absolutePath = path.resolve(baseDir, file) const code = `Vue.component(${JSON.stringify(name)}, () => import(${JSON.stringify(absolutePath)}))` return code } - const components = (await resolveComponents(sourceDir)) || [] - const all = [...pageFiles, ...components] - return `import Vue from 'vue'\n` + all.map(genImport).join('\n') + return `import Vue from 'vue'\n` + components.map(genImport).join('\n') } const indexRE = /\breadme\.md$/i @@ -189,12 +185,20 @@ async function resolveComponents (sourceDir) { return await globby(['**/*.vue'], { cwd: componentDir }) } -async function genRoutesFile ({ siteData: { pages }}) { - function genRoute ({ path }) { +async function genRoutesFile ({ siteData: { pages }, sourceDir, pageFiles }) { + function genRoute ({ path: pagePath }, index) { + const file = pageFiles[index] + const filePath = path.resolve(sourceDir, file) const code = ` { - path: ${JSON.stringify(path)}, - component: Theme + path: ${JSON.stringify(pagePath)}, + component: Theme, + beforeEnter: (to, from, next) => { + import(${JSON.stringify(filePath)}).then(comp => { + Vue.component(${JSON.stringify(fileToComponentName(file))}, comp.default) + next() + }) + } }` return code }