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 1 commit
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
16 changes: 9 additions & 7 deletions packages/@vuepress/core/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,22 @@ 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 (!options.pages.some(p => p.path === '/404.html')) {
options.pages.push({ path: '/404.html' })
}

// render pages
logger.wait('Rendering static HTML...')
const pagePaths = []
for (const page of options.pages) {
await renderPage(page)
}

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

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

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

// DONE.
const relativeDir = path.relative(process.cwd(), outDir)
Expand Down Expand Up @@ -155,6 +156,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 @@ -148,7 +148,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,26 +3,27 @@ const ClientDynamicModulesOption = require('./ClientDynamicModulesOption')
const GlobalUIComponentsOption = require('./GlobalUIComponentsOption')
const DefineOption = require('./DefineOption')
const AliasOption = require('./AliasOption')
const AsyncOption = require('../abstract/AsyncOption')
const Option = require('../abstract/Option')
const { PLUGIN_OPTION_MAP } = require('../constants')

module.exports = function instantiateOption (name) {
switch (name) {
module.exports = function instantiateOption (option) {
switch (option.name) {
case PLUGIN_OPTION_MAP.ENHANCE_APP_FILES.name:
return new EnhanceAppFilesOption(name)
return new EnhanceAppFilesOption(option.name)

case PLUGIN_OPTION_MAP.CLIENT_DYNAMIC_MODULES.name:
return new ClientDynamicModulesOption(name)
return new ClientDynamicModulesOption(option.name)

case PLUGIN_OPTION_MAP.GLOBAL_UI_COMPONENTS.name:
return new GlobalUIComponentsOption(name)
return new GlobalUIComponentsOption(option.name)

case PLUGIN_OPTION_MAP.DEFINE.name:
return new DefineOption(name)
return new DefineOption(option.name)

case PLUGIN_OPTION_MAP.ALIAS.name:
return new AliasOption(name)
return new AliasOption(option.name)

default: return new Option(name)
default: return option.async ? new AsyncOption(option.name) : new Option(option.name)
}
}
15 changes: 15 additions & 0 deletions packages/docs/docs/plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,21 @@ module.exports = (options, ctx) => {
}
```

### generated

- Type: `Function`
- Default: undefined

Called when a (production) build finishes, with an array of generated page HTML paths.

``` js
module.exports = {
generated (pagePaths) {
// ...
}
}
```

### extendMarkdown

- Type: `Function`
Expand Down