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

Fix #1613 #1788

Closed
wants to merge 5 commits into from
Closed
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
3 changes: 3 additions & 0 deletions packages/@vuepress/core/lib/node/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
1 change: 1 addition & 0 deletions packages/@vuepress/core/lib/node/plugin-api/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] },
Expand Down
2 changes: 2 additions & 0 deletions packages/@vuepress/core/lib/node/plugin-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ module.exports = class PluginAPI {
shortcut,

// hooks
init,
ready,
compiled,
updated,
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions packages/docs/docs/plugin/life-cycle.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down