diff --git a/packages/@vuepress/core/lib/node/App.js b/packages/@vuepress/core/lib/node/App.js index 99df7fc280..5a321d20b3 100755 --- a/packages/@vuepress/core/lib/node/App.js +++ b/packages/@vuepress/core/lib/node/App.js @@ -105,6 +105,9 @@ module.exports = class App { this.applyUserPlugins() this.pluginAPI.initialize() + // invoke init hook + await this.pluginAPI.applyAsyncOption('init') + this.markdown = createMarkdown(this) await this.resolvePages() diff --git a/packages/@vuepress/core/lib/node/__tests__/plugin-api/PluginAPI.spec.js b/packages/@vuepress/core/lib/node/__tests__/plugin-api/PluginAPI.spec.js index 2f21ad454f..3eaa27e25a 100644 --- a/packages/@vuepress/core/lib/node/__tests__/plugin-api/PluginAPI.spec.js +++ b/packages/@vuepress/core/lib/node/__tests__/plugin-api/PluginAPI.spec.js @@ -9,7 +9,11 @@ describe('Plugin', () => { test('registerOption', () => { const api = new PluginAPI() const readyHandler = () => {} + const initHandler = () => {} + api.registerOption(PLUGIN_OPTION_MAP.INIT.key, initHandler) api.registerOption(PLUGIN_OPTION_MAP.READY.key, readyHandler) + expect(api.options.init.values).toHaveLength(1) + expect(api.options.init.values[0]).toBe(initHandler) expect(api.options.ready.values).toHaveLength(1) expect(api.options.ready.values[0]).toBe(readyHandler) }) diff --git a/packages/@vuepress/core/lib/node/plugin-api/constants.js b/packages/@vuepress/core/lib/node/plugin-api/constants.js index e142975459..39dd501195 100644 --- a/packages/@vuepress/core/lib/node/plugin-api/constants.js +++ b/packages/@vuepress/core/lib/node/plugin-api/constants.js @@ -2,6 +2,7 @@ const PLUGIN_OPTION_META_MAP = { // hooks + INIT: { name: 'init', types: [Function], async: true }, READY: { name: 'ready', types: [Function], async: true }, COMPILED: { name: 'compiled', types: [Function] }, UPDATED: { name: 'updated', types: [Function] }, diff --git a/packages/@vuepress/core/lib/node/plugin-api/index.js b/packages/@vuepress/core/lib/node/plugin-api/index.js index 6dc8a9e3fd..5d6bcad564 100644 --- a/packages/@vuepress/core/lib/node/plugin-api/index.js +++ b/packages/@vuepress/core/lib/node/plugin-api/index.js @@ -193,6 +193,7 @@ module.exports = class PluginAPI { shortcut, // hooks + init, ready, compiled, updated, @@ -222,6 +223,7 @@ module.exports = class PluginAPI { } this + .registerOption(PLUGIN_OPTION_MAP.INIT.key, init, pluginName) .registerOption(PLUGIN_OPTION_MAP.READY.key, ready, pluginName) .registerOption(PLUGIN_OPTION_MAP.COMPILED.key, compiled, pluginName) .registerOption(PLUGIN_OPTION_MAP.UPDATED.key, updated, pluginName) diff --git a/packages/docs/docs/plugin/life-cycle.md b/packages/docs/docs/plugin/life-cycle.md index 76996bdd9c..ed71b7cbfd 100644 --- a/packages/docs/docs/plugin/life-cycle.md +++ b/packages/docs/docs/plugin/life-cycle.md @@ -1,5 +1,22 @@ # Lifecycle +## init + +- Type: `AsyncFunction` +- Scope:`dev|build` + +```js +module.exports = { + async init() { + // ... + } +} +``` + +::: tip +The `init` hook is executed before the build process. +::: + ## ready - Type: `AsyncFunction`