From 27cf75899ddec183b359fe0f07d431d4bcf6f75e Mon Sep 17 00:00:00 2001 From: Slashgear Date: Sun, 7 Apr 2019 20:51:52 +0200 Subject: [PATCH 1/3] feat(options): make enhancers async ready --- packages/@vuepress/core/lib/node/Page.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/@vuepress/core/lib/node/Page.js b/packages/@vuepress/core/lib/node/Page.js index 0da8783010..3178f63ba4 100644 --- a/packages/@vuepress/core/lib/node/Page.js +++ b/packages/@vuepress/core/lib/node/Page.js @@ -142,7 +142,7 @@ module.exports = class Page { this._computed = computed this._localePath = computed.$localePath - this.enhance(enhancers) + await this.enhance(enhancers) this.buildPermalink() } @@ -283,13 +283,13 @@ module.exports = class Page { */ enhance (enhancers) { - for (const { name: pluginName, value: enhancer } of enhancers) { - try { - enhancer(this) - } catch (error) { - console.log(error) - throw new Error(`[${pluginName}] excuete extendPageData failed.`) - } - } + return enhancers.reduce(async (accumulator, { pluginName, value }) => { + return accumulator + .then(() => value(this)) + .catch(error => { + console.log(error) + throw new Error(`[${pluginName}] execute extendPageData failed.`) + }) + }, Promise.resolve()) } } From 13ea22f65a07a9844029532d75b1fd3ef399f36d Mon Sep 17 00:00:00 2001 From: Slashgear Date: Tue, 16 Apr 2019 21:18:17 +0200 Subject: [PATCH 2/3] add test and move to async/await --- packages/@vuepress/core/lib/node/Page.js | 18 +++---- .../lib/node/__tests__/prepare/Page.spec.js | 49 +++++++++++++++++++ 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/packages/@vuepress/core/lib/node/Page.js b/packages/@vuepress/core/lib/node/Page.js index 3178f63ba4..0ea0242d84 100644 --- a/packages/@vuepress/core/lib/node/Page.js +++ b/packages/@vuepress/core/lib/node/Page.js @@ -282,14 +282,14 @@ module.exports = class Page { * @api private */ - enhance (enhancers) { - return enhancers.reduce(async (accumulator, { pluginName, value }) => { - return accumulator - .then(() => value(this)) - .catch(error => { - console.log(error) - throw new Error(`[${pluginName}] execute extendPageData failed.`) - }) - }, Promise.resolve()) + async enhance (enhancers) { + for (const { name: pluginName, value: enhancer } of enhancers) { + try { + await enhancer(this) + } catch (error) { + console.log(error) + throw new Error(`[${pluginName}] execute extendPageData failed.`) + } + } } } diff --git a/packages/@vuepress/core/lib/node/__tests__/prepare/Page.spec.js b/packages/@vuepress/core/lib/node/__tests__/prepare/Page.spec.js index 2f3b4b6f30..530f7f4af5 100644 --- a/packages/@vuepress/core/lib/node/__tests__/prepare/Page.spec.js +++ b/packages/@vuepress/core/lib/node/__tests__/prepare/Page.spec.js @@ -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) + } + }) + }) }) From 3cbfbf1a6be73bdd762242dd16fa6b3d1c0ab00f Mon Sep 17 00:00:00 2001 From: Franck Date: Tue, 10 Sep 2019 18:04:15 +0200 Subject: [PATCH 3/3] docs: Update doc for extendPageData --- packages/docs/docs/plugin/option-api.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/docs/docs/plugin/option-api.md b/packages/docs/docs/plugin/option-api.md index b9c729fc89..53e594c3b9 100644 --- a/packages/docs/docs/plugin/option-api.md +++ b/packages/docs/docs/plugin/option-api.md @@ -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. @@ -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. :::