Skip to content

Commit

Permalink
feat($core): support async enhanceApp (close #2074) (#2075)
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy authored Feb 2, 2020
1 parent 7037882 commit 2d53fbb
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
12 changes: 6 additions & 6 deletions packages/@vuepress/core/lib/client/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Vue.prototype.$withBase = function (path) {
}
}

export function createApp (isServer) {
export async function createApp (isServer) {
const routerBase = typeof window !== 'undefined' && window.__VUEPRESS_ROUTER_BASE__
? window.__VUEPRESS_ROUTER_BASE__
: (siteData.routerBase || siteData.base)
Expand Down Expand Up @@ -90,11 +90,11 @@ export function createApp (isServer) {
const options = {}

try {
appEnhancers.forEach(enhancer => {
if (typeof enhancer === 'function') {
enhancer({ Vue, options, router, siteData, isServer })
}
})
await Promise.all(
appEnhancers
.filter(enhancer => typeof enhancer === 'function')
.map(enhancer => enhancer({ Vue, options, router, siteData, isServer }))
)
} catch (e) {
console.error(e)
}
Expand Down
8 changes: 4 additions & 4 deletions packages/@vuepress/core/lib/client/clientEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import { createApp } from './app'

const { app, router } = createApp(false /* isServer */)

window.__VUEPRESS__ = {
version: VUEPRESS_VERSION,
hash: LAST_COMMIT_HASH
}

router.onReady(() => {
app.$mount('#app')
createApp(false /* isServer */).then(({ app, router }) => {
router.onReady(() => {
app.$mount('#app')
})
})
17 changes: 9 additions & 8 deletions packages/@vuepress/core/lib/client/serverEntry.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { createApp } from './app'

export default context => new Promise((resolve, reject) => {
const { app, router } = createApp(true /* isServer */)
const { url } = context
const { fullPath } = router.resolve(url).route
createApp(true /* isServer */).then(({ app, router }) => {
const { url } = context
const { fullPath } = router.resolve(url).route

if (fullPath !== url) {
return reject({ url: fullPath })
}
if (fullPath !== url) {
return reject({ url: fullPath })
}

router.push(url)
router.onReady(() => resolve(app))
router.push(url)
router.onReady(() => resolve(app))
})
})
1 change: 1 addition & 0 deletions packages/docs/docs/guide/basic-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ To develop a custom theme, see [Writing a theme](../theme/writing-a-theme.md).
Since the VuePress app is a standard Vue app, you can apply app-level enhancements by creating a file `.vuepress/enhanceApp.js`, which will be imported into the app if it’s present. The file should `export default` a hook function which will receive an object containing some app-level values. You can use this hook to install extra Vue plugins, register global components, or add extra router hooks:

``` js
// async function is also supported, too
export default ({
Vue, // the version of Vue being used in the VuePress app
options, // the options for the root Vue instance
Expand Down
1 change: 1 addition & 0 deletions packages/docs/docs/zh/guide/basic-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module.exports = {
由于 VuePress 是一个标准的 Vue 应用,你可以通过创建一个 `.vuepress/enhanceApp.js` 文件来做一些应用级别的配置,当该文件存在的时候,会被导入到应用内部。`enhanceApp.js` 应该 `export default` 一个钩子函数,并接受一个包含了一些应用级别属性的对象作为参数。你可以使用这个钩子来安装一些附加的 Vue 插件、注册全局组件,或者增加额外的路由钩子等:

``` js
// 使用异步函数也是可以的
export default ({
Vue, // VuePress 正在使用的 Vue 构造函数
options, // 附加到根实例的一些选项
Expand Down

0 comments on commit 2d53fbb

Please sign in to comment.