From 30951060d232c0ac231d2bb2d728a3bfe5a0ea3e Mon Sep 17 00:00:00 2001 From: Jan Vennemann Date: Fri, 7 Dec 2018 02:13:44 +0100 Subject: [PATCH] fix: async function support for additionalPages plugin (close: #1077)(#1080) --- .../override/AdditionalPagesOption.js | 19 +++++++++++++++++++ .../plugin-api/override/instantiateOption.js | 4 ++++ .../@vuepress/core/lib/prepare/AppContext.js | 6 +----- packages/docs/docs/plugin/option-api.md | 2 +- 4 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 packages/@vuepress/core/lib/plugin-api/override/AdditionalPagesOption.js diff --git a/packages/@vuepress/core/lib/plugin-api/override/AdditionalPagesOption.js b/packages/@vuepress/core/lib/plugin-api/override/AdditionalPagesOption.js new file mode 100644 index 0000000000..0c0c2b6360 --- /dev/null +++ b/packages/@vuepress/core/lib/plugin-api/override/AdditionalPagesOption.js @@ -0,0 +1,19 @@ +'use strict' + +const AsyncOption = require('../abstract/AsyncOption') + +/** + * additionalPages option. + */ + +module.exports = class AdditionalPagesOption extends AsyncOption { + async apply (ctx) { + await super.asyncApply() + + await Promise.all( + this.appliedValues.map(async (options) => { + await ctx.addPage(options) + }) + ) + } +} diff --git a/packages/@vuepress/core/lib/plugin-api/override/instantiateOption.js b/packages/@vuepress/core/lib/plugin-api/override/instantiateOption.js index ad0fac2c7a..0f0669d9fc 100644 --- a/packages/@vuepress/core/lib/plugin-api/override/instantiateOption.js +++ b/packages/@vuepress/core/lib/plugin-api/override/instantiateOption.js @@ -3,6 +3,7 @@ const ClientDynamicModulesOption = require('./ClientDynamicModulesOption') const GlobalUIComponentsOption = require('./GlobalUIComponentsOption') const DefineOption = require('./DefineOption') const AliasOption = require('./AliasOption') +const AdditionalPagesOption = require('./AdditionalPagesOption') const Option = require('../abstract/Option') const { PLUGIN_OPTION_MAP } = require('../constants') @@ -23,6 +24,9 @@ module.exports = function instantiateOption (name) { case PLUGIN_OPTION_MAP.ALIAS.name: return new AliasOption(name) + case PLUGIN_OPTION_MAP.ADDITIONAL_PAGES.name: + return new AdditionalPagesOption(name) + default: return new Option(name) } } diff --git a/packages/@vuepress/core/lib/prepare/AppContext.js b/packages/@vuepress/core/lib/prepare/AppContext.js index 57f8ca1321..f5bf258f0c 100644 --- a/packages/@vuepress/core/lib/prepare/AppContext.js +++ b/packages/@vuepress/core/lib/prepare/AppContext.js @@ -103,11 +103,7 @@ module.exports = class AppContext { this.markdown = createMarkdown(this) await this.resolvePages() - await Promise.all( - this.pluginAPI.options.additionalPages.values.map(async (options) => { - await this.addPage(options) - }) - ) + await this.pluginAPI.options.additionalPages.apply(this) await this.pluginAPI.options.ready.apply() await Promise.all([ diff --git a/packages/docs/docs/plugin/option-api.md b/packages/docs/docs/plugin/option-api.md index 6be11d8cb9..23c2578a1d 100644 --- a/packages/docs/docs/plugin/option-api.md +++ b/packages/docs/docs/plugin/option-api.md @@ -354,7 +354,7 @@ export default { ## additionalPages -- Type: `Array|Function` +- Type: `Array|AsyncFunction` - Default: `undefined` Add a page pointing to a markdown file: