Skip to content

Commit 543fd6c

Browse files
Slashgearkefranabg
authored andcommitted
feat($config): make extendPageData async ready without breaking changes (#1546)
1 parent f964391 commit 543fd6c

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

packages/@vuepress/core/lib/node/Page.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ module.exports = class Page {
142142
this._computed = computed
143143
this._localePath = computed.$localePath
144144

145-
this.enhance(enhancers)
145+
await this.enhance(enhancers)
146146
this.buildPermalink()
147147
}
148148

@@ -282,13 +282,13 @@ module.exports = class Page {
282282
* @api private
283283
*/
284284

285-
enhance (enhancers) {
285+
async enhance (enhancers) {
286286
for (const { name: pluginName, value: enhancer } of enhancers) {
287287
try {
288-
enhancer(this)
288+
await enhancer(this)
289289
} catch (error) {
290290
console.log(error)
291-
throw new Error(`[${pluginName}] excuete extendPageData failed.`)
291+
throw new Error(`[${pluginName}] execute extendPageData failed.`)
292292
}
293293
}
294294
}

packages/@vuepress/core/lib/node/__tests__/prepare/Page.spec.js

+49
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,54 @@ describe('Page', () => {
102102
expect(page._content.startsWith('---')).toBe(true)
103103
expect(page._strippedContent.startsWith('---')).toBe(false)
104104
})
105+
106+
describe('enhance - ', () => {
107+
let page
108+
let enhancers
109+
110+
beforeEach(() => {
111+
page = new Page({ path: '/' }, app)
112+
enhancers = [
113+
{
114+
pluginName: 'foo',
115+
value: jest.fn()
116+
},
117+
{
118+
pluginName: 'foo',
119+
value: jest.fn()
120+
}
121+
]
122+
global.console.log = jest.fn()
123+
})
124+
125+
test('should loop over sync enhancers', async () => {
126+
await page.enhance(enhancers)
127+
128+
return enhancers.map(enhancer => expect(enhancer.value).toBeCalled())
129+
})
130+
131+
test('should loop over sync and async enhancers', async () => {
132+
const mixedEnhancers = [...enhancers, {
133+
pluginName: 'blog',
134+
value: jest.fn().mockResolvedValue({})
135+
}]
136+
await page.enhance(mixedEnhancers)
137+
138+
return mixedEnhancers.map(enhancer => expect(enhancer.value).toBeCalled())
139+
})
140+
141+
test('should log when enhancing when failing', async () => {
142+
const error = { errorMessage: 'this is an error message' }
143+
expect.assertions(1)
144+
try {
145+
await page.enhance([{
146+
pluginName: 'error-plugin',
147+
value: jest.fn().mockRejectedValue(error)
148+
}])
149+
} catch (e) {
150+
expect(console.log).toBeCalledWith(error)
151+
}
152+
})
153+
})
105154
})
106155

packages/docs/docs/plugin/option-api.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ import { SOURCE_DIR } from '@dynamic/constants'
280280

281281
## extendPageData
282282

283-
- Type: `Function`
283+
- Type: `Function|AsyncFunction`
284284
- Default: `undefined`
285285

286286
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.
@@ -308,6 +308,16 @@ module.exports = {
308308
}
309309
```
310310

311+
Note that `extendPageData` can also be defined as an asynchronous function.
312+
313+
```js
314+
module.exports = {
315+
async extendPageData ($page) {
316+
$page.xxx = await getAsyncData()
317+
}
318+
}
319+
```
320+
311321
::: warning Note
312322
These fields starting with an `_` means you can only access them during build time.
313323
:::

0 commit comments

Comments
 (0)