Skip to content

Commit 5ee2b2b

Browse files
spindaulivz
authored andcommitted
feat($core): pass generated page paths to plugins (#925)
Internal changes: - More convenient way to add async functional option APIs.
1 parent 26c0628 commit 5ee2b2b

File tree

5 files changed

+18
-14
lines changed

5 files changed

+18
-14
lines changed

packages/@vuepress/core/lib/build.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,23 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
6363
.map(renderHeadTag)
6464
.join('\n ')
6565

66+
// if the user does not have a custom 404.md, generate the theme's default
67+
if (!ctx.pages.some(p => p.path === '/404.html')) {
68+
ctx.addPage({ path: '/404.html' })
69+
}
70+
6671
// render pages
6772
logger.wait('Rendering static HTML...')
68-
for (const page of ctx.pages) {
69-
await renderPage(page)
70-
}
7173

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

7779
readline.clearLine(process.stdout, 0)
7880
readline.cursorTo(process.stdout, 0)
7981

80-
await ctx.pluginAPI.options.generated.apply()
82+
await ctx.pluginAPI.options.generated.apply(pagePaths)
8183

8284
// DONE.
8385
const relativeDir = path.relative(cwd, outDir)
@@ -155,6 +157,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
155157
const filePath = path.resolve(outDir, filename)
156158
await fs.ensureDir(path.dirname(filePath))
157159
await fs.writeFile(filePath, html)
160+
return filePath
158161
}
159162

160163
function renderPageMeta (meta) {

packages/@vuepress/core/lib/plugin-api/abstract/AsyncOption.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class AsyncOption extends Option {
3030
this.add(
3131
name,
3232
isFunction(value)
33-
? await value(...args)
33+
? await Promise.resolve(value(...args))
3434
: value
3535
)
3636
} catch (error) {
@@ -60,7 +60,7 @@ class AsyncOption extends Option {
6060
this.add(
6161
name,
6262
isFunction(value)
63-
? await value(...args)
63+
? await Promise.resolve(value(...args))
6464
: value
6565
)
6666
} catch (error) {
@@ -84,7 +84,7 @@ class AsyncOption extends Option {
8484

8585
async pipeline (input) {
8686
for (const fn of this.values) {
87-
input = await fn(input)
87+
input = await Promise.resolve(fn(input))
8888
}
8989
return input
9090
}

packages/@vuepress/core/lib/plugin-api/constants.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const PLUGIN_OPTION_META_MAP = {
55
READY: { name: 'ready', types: [Function] },
66
COMPILED: { name: 'compiled', types: [Function] },
77
UPDATED: { name: 'updated', types: [Function] },
8-
GENERATED: { name: 'generated', types: [Function] },
8+
GENERATED: { name: 'generated', types: [Function], async: true },
99
// options
1010
CHAIN_WEBPACK: { name: 'chainWebpack', types: [Function] },
1111
ENHANCE_DEV_SERVER: { name: 'enhanceDevServer', types: [Function] },

packages/@vuepress/core/lib/plugin-api/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ module.exports = class PluginAPI {
147147
initializeOptions () {
148148
Object.keys(PLUGIN_OPTION_MAP).forEach(key => {
149149
const option = PLUGIN_OPTION_MAP[key]
150-
this.options[option.name] = instantiateOption(option.name)
150+
this.options[option.name] = instantiateOption(option)
151151
})
152152
}
153153

packages/@vuepress/core/lib/plugin-api/override/instantiateOption.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ const ClientDynamicModulesOption = require('./ClientDynamicModulesOption')
33
const GlobalUIComponentsOption = require('./GlobalUIComponentsOption')
44
const DefineOption = require('./DefineOption')
55
const AliasOption = require('./AliasOption')
6+
const AsyncOption = require('../abstract/AsyncOption')
67
const AdditionalPagesOption = require('./AdditionalPagesOption')
78
const Option = require('../abstract/Option')
89
const { PLUGIN_OPTION_MAP } = require('../constants')
910

10-
module.exports = function instantiateOption (name) {
11+
module.exports = function instantiateOption ({ name, async }) {
1112
switch (name) {
1213
case PLUGIN_OPTION_MAP.ENHANCE_APP_FILES.name:
1314
return new EnhanceAppFilesOption(name)
@@ -27,6 +28,6 @@ module.exports = function instantiateOption (name) {
2728
case PLUGIN_OPTION_MAP.ADDITIONAL_PAGES.name:
2829
return new AdditionalPagesOption(name)
2930

30-
default: return new Option(name)
31+
default: return async ? new AsyncOption(name) : new Option(name)
3132
}
3233
}

0 commit comments

Comments
 (0)