Skip to content

Commit

Permalink
refactor: use async components to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz committed Jun 14, 2018
1 parent 3b16c5c commit d210ec0
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 27 deletions.
27 changes: 3 additions & 24 deletions lib/prepare/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,16 @@ exports.pathsToModuleCode = function (files) {
return code
}

exports.getPageComponentsLoadingFile = function (pages) {
let code = 'const _l = {}\n'

code += pages.map(({ filepath, key }) => {
return `_l['${key}'] = () => import('${filepath}')`
}).join('\n')

code += `\n\nexport default function loadComponent(key) {
return _l[key]()
}\n`
return code
}

exports.genRoutesFile = async function ({
siteData: { pages },
plugin
}) {
await writeTemp('components-loader.js', exports.getPageComponentsLoadingFile(pages))

function genRoute ({ path: pagePath, key: componentName }, index) {
let code = `
{
name: ${JSON.stringify(componentName)},
path: ${JSON.stringify(pagePath)},
component: ThemeLayout,
beforeEnter: (to, from, next) => {
loadComponent(${JSON.stringify(componentName)}).then(comp => {
Vue.component(${JSON.stringify(componentName)}, comp.default)
next()
})
}
component: ThemeLayout
}`

const dncodedPath = decodeURIComponent(pagePath)
Expand Down Expand Up @@ -94,11 +73,11 @@ exports.genRoutesFile = async function ({
`import Vue from 'vue'\n` +
`import ThemeLayout from '@themeLayout'\n` +
`import ThemeNotFound from '@themeNotFound'\n` +
`import { injectMixins } from '@app/util'\n` +
`import loadComponent from '@temp/components-loader'\n` +
`import { injectMixins, loadComponent } from '@app/util'\n` +
`import rootMixins from '@temp/root-mixins'\n\n` +
`injectMixins(ThemeLayout, rootMixins)\n` +
`injectMixins(ThemeNotFound, rootMixins)\n\n` +
`${pages.map(({ filepath, key }) => `Vue.component('${key}', () => import('${filepath}'))`).join('\n')}\n\n` +
`export const routes = [${pages.map(genRoute).join(',')}${notFoundRoute}\n]`
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ export default {
currentPath: '/'
}
},
created () {
console.log(this.key)
},
computed: {
key () {
return this.currentPage.key
Expand Down

0 comments on commit d210ec0

Please sign in to comment.