Skip to content

Commit

Permalink
feat($config): make extendPageData async ready without breaking chang…
Browse files Browse the repository at this point in the history
…es (#1546)
  • Loading branch information
Slashgear authored and kefranabg committed Sep 10, 2019
1 parent f964391 commit 543fd6c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
8 changes: 4 additions & 4 deletions packages/@vuepress/core/lib/node/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ module.exports = class Page {
this._computed = computed
this._localePath = computed.$localePath

this.enhance(enhancers)
await this.enhance(enhancers)
this.buildPermalink()
}

Expand Down Expand Up @@ -282,13 +282,13 @@ module.exports = class Page {
* @api private
*/

enhance (enhancers) {
async enhance (enhancers) {
for (const { name: pluginName, value: enhancer } of enhancers) {
try {
enhancer(this)
await enhancer(this)
} catch (error) {
console.log(error)
throw new Error(`[${pluginName}] excuete extendPageData failed.`)
throw new Error(`[${pluginName}] execute extendPageData failed.`)
}
}
}
Expand Down
49 changes: 49 additions & 0 deletions packages/@vuepress/core/lib/node/__tests__/prepare/Page.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,54 @@ describe('Page', () => {
expect(page._content.startsWith('---')).toBe(true)
expect(page._strippedContent.startsWith('---')).toBe(false)
})

describe('enhance - ', () => {
let page
let enhancers

beforeEach(() => {
page = new Page({ path: '/' }, app)
enhancers = [
{
pluginName: 'foo',
value: jest.fn()
},
{
pluginName: 'foo',
value: jest.fn()
}
]
global.console.log = jest.fn()
})

test('should loop over sync enhancers', async () => {
await page.enhance(enhancers)

return enhancers.map(enhancer => expect(enhancer.value).toBeCalled())
})

test('should loop over sync and async enhancers', async () => {
const mixedEnhancers = [...enhancers, {
pluginName: 'blog',
value: jest.fn().mockResolvedValue({})
}]
await page.enhance(mixedEnhancers)

return mixedEnhancers.map(enhancer => expect(enhancer.value).toBeCalled())
})

test('should log when enhancing when failing', async () => {
const error = { errorMessage: 'this is an error message' }
expect.assertions(1)
try {
await page.enhance([{
pluginName: 'error-plugin',
value: jest.fn().mockRejectedValue(error)
}])
} catch (e) {
expect(console.log).toBeCalledWith(error)
}
})
})
})

12 changes: 11 additions & 1 deletion packages/docs/docs/plugin/option-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ import { SOURCE_DIR } from '@dynamic/constants'

## extendPageData

- Type: `Function`
- Type: `Function|AsyncFunction`
- Default: `undefined`

A function used to extend or edit the [$page](../guide/global-computed.md#page) object. This function will be invoking once for each page at compile time.
Expand Down Expand Up @@ -308,6 +308,16 @@ module.exports = {
}
```

Note that `extendPageData` can also be defined as an asynchronous function.

```js
module.exports = {
async extendPageData ($page) {
$page.xxx = await getAsyncData()
}
}
```

::: warning Note
These fields starting with an `_` means you can only access them during build time.
:::
Expand Down

0 comments on commit 543fd6c

Please sign in to comment.