Skip to content

Commit

Permalink
feat($core): pass generated page paths to plugins (#925)
Browse files Browse the repository at this point in the history
Internal changes:

- More convenient way to add async functional option APIs.
  • Loading branch information
spinda authored and ulivz committed Dec 8, 2018
1 parent 26c0628 commit 5ee2b2b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
17 changes: 10 additions & 7 deletions packages/@vuepress/core/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,23 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
.map(renderHeadTag)
.join('\n ')

// if the user does not have a custom 404.md, generate the theme's default
if (!ctx.pages.some(p => p.path === '/404.html')) {
ctx.addPage({ path: '/404.html' })
}

// render pages
logger.wait('Rendering static HTML...')
for (const page of ctx.pages) {
await renderPage(page)
}

// if the user does not have a custom 404.md, generate the theme's default
if (!ctx.pages.some(p => p.path === '/404.html')) {
await renderPage({ path: '/404.html' })
const pagePaths = []
for (const page of ctx.pages) {
pagePaths.push(await renderPage(page))
}

readline.clearLine(process.stdout, 0)
readline.cursorTo(process.stdout, 0)

await ctx.pluginAPI.options.generated.apply()
await ctx.pluginAPI.options.generated.apply(pagePaths)

// DONE.
const relativeDir = path.relative(cwd, outDir)
Expand Down Expand Up @@ -155,6 +157,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
const filePath = path.resolve(outDir, filename)
await fs.ensureDir(path.dirname(filePath))
await fs.writeFile(filePath, html)
return filePath
}

function renderPageMeta (meta) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AsyncOption extends Option {
this.add(
name,
isFunction(value)
? await value(...args)
? await Promise.resolve(value(...args))
: value
)
} catch (error) {
Expand Down Expand Up @@ -60,7 +60,7 @@ class AsyncOption extends Option {
this.add(
name,
isFunction(value)
? await value(...args)
? await Promise.resolve(value(...args))
: value
)
} catch (error) {
Expand All @@ -84,7 +84,7 @@ class AsyncOption extends Option {

async pipeline (input) {
for (const fn of this.values) {
input = await fn(input)
input = await Promise.resolve(fn(input))
}
return input
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@vuepress/core/lib/plugin-api/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const PLUGIN_OPTION_META_MAP = {
READY: { name: 'ready', types: [Function] },
COMPILED: { name: 'compiled', types: [Function] },
UPDATED: { name: 'updated', types: [Function] },
GENERATED: { name: 'generated', types: [Function] },
GENERATED: { name: 'generated', types: [Function], async: true },
// options
CHAIN_WEBPACK: { name: 'chainWebpack', types: [Function] },
ENHANCE_DEV_SERVER: { name: 'enhanceDevServer', types: [Function] },
Expand Down
2 changes: 1 addition & 1 deletion packages/@vuepress/core/lib/plugin-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ module.exports = class PluginAPI {
initializeOptions () {
Object.keys(PLUGIN_OPTION_MAP).forEach(key => {
const option = PLUGIN_OPTION_MAP[key]
this.options[option.name] = instantiateOption(option.name)
this.options[option.name] = instantiateOption(option)
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ const ClientDynamicModulesOption = require('./ClientDynamicModulesOption')
const GlobalUIComponentsOption = require('./GlobalUIComponentsOption')
const DefineOption = require('./DefineOption')
const AliasOption = require('./AliasOption')
const AsyncOption = require('../abstract/AsyncOption')
const AdditionalPagesOption = require('./AdditionalPagesOption')
const Option = require('../abstract/Option')
const { PLUGIN_OPTION_MAP } = require('../constants')

module.exports = function instantiateOption (name) {
module.exports = function instantiateOption ({ name, async }) {
switch (name) {
case PLUGIN_OPTION_MAP.ENHANCE_APP_FILES.name:
return new EnhanceAppFilesOption(name)
Expand All @@ -27,6 +28,6 @@ module.exports = function instantiateOption (name) {
case PLUGIN_OPTION_MAP.ADDITIONAL_PAGES.name:
return new AdditionalPagesOption(name)

default: return new Option(name)
default: return async ? new AsyncOption(name) : new Option(name)
}
}

0 comments on commit 5ee2b2b

Please sign in to comment.