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($core): pass generated page paths to plugins #925

Merged
merged 7 commits into from
Dec 8, 2018
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
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)
}
}