Skip to content

Commit 3b16c5c

Browse files
committed
feat: support spearate "loadComponent" util
1 parent b2c5484 commit 3b16c5c

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

lib/app/util.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,3 @@ export function findPageForPath (pages, path) {
1717
frontmatter: {}
1818
}
1919
}
20-
21-
// export function loadPage (key) {
22-
// return import(${JSON.stringify(filepath)}).then(comp => {
23-
// Vue.component(${JSON.stringify(componentName)}, comp.default)
24-
// next()
25-
// })
26-
// }

lib/prepare/codegen.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,33 @@ exports.pathsToModuleCode = function (files) {
2121
return code
2222
}
2323

24+
exports.getPageComponentsLoadingFile = function (pages) {
25+
let code = 'const _l = {}\n'
26+
27+
code += pages.map(({ filepath, key }) => {
28+
return `_l['${key}'] = () => import('${filepath}')`
29+
}).join('\n')
30+
31+
code += `\n\nexport default function loadComponent(key) {
32+
return _l[key]()
33+
}\n`
34+
return code
35+
}
36+
2437
exports.genRoutesFile = async function ({
2538
siteData: { pages },
2639
plugin
2740
}) {
28-
function genRoute ({ filepath, path: pagePath, key: componentName }, index) {
41+
await writeTemp('components-loader.js', exports.getPageComponentsLoadingFile(pages))
42+
43+
function genRoute ({ path: pagePath, key: componentName }, index) {
2944
let code = `
3045
{
3146
name: ${JSON.stringify(componentName)},
3247
path: ${JSON.stringify(pagePath)},
3348
component: ThemeLayout,
3449
beforeEnter: (to, from, next) => {
35-
loadPageComponent("v-6e489b82c1bb8").then(next)
36-
import(${JSON.stringify(filepath)}).then(comp => {
50+
loadComponent(${JSON.stringify(componentName)}).then(comp => {
3751
Vue.component(${JSON.stringify(componentName)}, comp.default)
3852
next()
3953
})
@@ -80,7 +94,8 @@ exports.genRoutesFile = async function ({
8094
`import Vue from 'vue'\n` +
8195
`import ThemeLayout from '@themeLayout'\n` +
8296
`import ThemeNotFound from '@themeNotFound'\n` +
83-
`import { injectMixins, loadPageComponent } from '@app/util'\n` +
97+
`import { injectMixins } from '@app/util'\n` +
98+
`import loadComponent from '@temp/components-loader'\n` +
8499
`import rootMixins from '@temp/root-mixins'\n\n` +
85100
`injectMixins(ThemeLayout, rootMixins)\n` +
86101
`injectMixins(ThemeNotFound, rootMixins)\n\n` +

0 commit comments

Comments
 (0)