Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(options): make enhancers async ready without breaking changes #1546

Merged
merged 3 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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