From 07c4bde37d8b1619c62e9e38249a529d165f7da2 Mon Sep 17 00:00:00 2001 From: Shadab Khan Date: Sun, 18 Aug 2019 21:43:56 +0530 Subject: [PATCH 1/5] Add init hook --- packages/@vuepress/core/lib/node/App.js | 3 +++ packages/@vuepress/core/lib/node/plugin-api/constants.js | 1 + packages/@vuepress/core/lib/node/plugin-api/index.js | 2 ++ 3 files changed, 6 insertions(+) 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/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) From 0b5c67ce6a786a03a9771a3ed516a76036be2d5a Mon Sep 17 00:00:00 2001 From: Shadab Khan Date: Sun, 18 Aug 2019 21:49:03 +0530 Subject: [PATCH 2/5] Add test for init hook --- .../core/lib/node/__tests__/plugin-api/PluginAPI.spec.js | 4 ++++ 1 file changed, 4 insertions(+) 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..a33d100c79 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(readyHandler) expect(api.options.ready.values).toHaveLength(1) expect(api.options.ready.values[0]).toBe(readyHandler) }) From f1d65f7531c66092e549c824e1e81cf8a746bd56 Mon Sep 17 00:00:00 2001 From: Shadab Khan Date: Sun, 18 Aug 2019 21:55:11 +0530 Subject: [PATCH 3/5] Update docs --- packages/docs/docs/plugin/life-cycle.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/docs/docs/plugin/life-cycle.md b/packages/docs/docs/plugin/life-cycle.md index 76996bdd9c..d283aab8cf 100644 --- a/packages/docs/docs/plugin/life-cycle.md +++ b/packages/docs/docs/plugin/life-cycle.md @@ -1,5 +1,22 @@ # Lifecycle +## int + +- Type: `AsyncFunction` +- Scope:`dev|build` + +```js +module.exports = { + async init() { + // ... + } +} +``` + +::: tip +The `init` hook is executed before the build process. +::: + ## ready - Type: `AsyncFunction` From d8e9b2ede792363c665d8f56ea3c4205457d3b10 Mon Sep 17 00:00:00 2001 From: Shadab Khan Date: Sun, 18 Aug 2019 22:02:18 +0530 Subject: [PATCH 4/5] Fix test --- .../core/lib/node/__tests__/plugin-api/PluginAPI.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 a33d100c79..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 @@ -13,7 +13,7 @@ describe('Plugin', () => { 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(readyHandler) + expect(api.options.init.values[0]).toBe(initHandler) expect(api.options.ready.values).toHaveLength(1) expect(api.options.ready.values[0]).toBe(readyHandler) }) From fe82383ad80ec6ffd945c223097750f051b2be05 Mon Sep 17 00:00:00 2001 From: Shadab Khan Date: Wed, 21 Aug 2019 06:44:42 +0530 Subject: [PATCH 5/5] Fix typo --- packages/docs/docs/plugin/life-cycle.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docs/docs/plugin/life-cycle.md b/packages/docs/docs/plugin/life-cycle.md index d283aab8cf..ed71b7cbfd 100644 --- a/packages/docs/docs/plugin/life-cycle.md +++ b/packages/docs/docs/plugin/life-cycle.md @@ -1,6 +1,6 @@ # Lifecycle -## int +## init - Type: `AsyncFunction` - Scope:`dev|build`