Skip to content

Commit a46d888

Browse files
committed
docs: add guide for authoring a plugin
1 parent 8ba3968 commit a46d888

File tree

2 files changed

+190
-2
lines changed

2 files changed

+190
-2
lines changed

docs/guide/advanced/plugin.md

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,97 @@
11
# Writing a Plugin
22

3-
> TODO
3+
::: tip
4+
Before reading this guide, you'd better learn the VuePress [architecture](./architecture.md) first.
5+
:::
6+
7+
A VuePress plugin is a plain JavaScript object that satisfies the [Plugin API](../../reference/plugin-api.md), which is called a *Plugin Object*.
8+
9+
If a plugin wants to receive user options, it could be a function that returns a *Plugin Object*, which is called a *Plugin Function*.
10+
11+
<CodeGroup>
12+
<CodeGroupItem title="Plugin Object" active>
13+
14+
```js
15+
const fooPlugin = {
16+
name: 'vuepress-plugin-foo',
17+
// ...
18+
}
19+
```
20+
21+
</CodeGroupItem>
22+
23+
<CodeGroupItem title="Plugin Function">
24+
25+
```js
26+
const fooPlugin = (options, app) => {
27+
return {
28+
name: 'vuepress-plugin-foo',
29+
// ...
30+
}
31+
}
32+
```
33+
34+
</CodeGroupItem>
35+
</CodeGroup>
36+
37+
## Creating a Plugin Package
38+
39+
The typical structure of a plugin package is as follow:
40+
41+
```bash
42+
vuepress-plugin-foo
43+
├─ lib
44+
│ └─ index.js
45+
└─ package.json
46+
```
47+
48+
### Plugin Entry
49+
50+
The `lib/index.js` file is the plugin entry, which should export the plugin directly:
51+
52+
<CodeGroup>
53+
<CodeGroupItem title="CJS" active>
54+
55+
```js
56+
module.export = fooPlugin
57+
```
58+
59+
</CodeGroupItem>
60+
61+
<CodeGroupItem title="ESM">
62+
63+
```js
64+
export default fooPlugin
65+
```
66+
67+
</CodeGroupItem>
68+
</CodeGroup>
69+
70+
::: tip
71+
Notice that the plugin entry will be loaded in Node, so it should be in CommonJS format.
72+
73+
If you are using ESM format, you'll need to use [babel](https://babeljs.io/) or [typescript](https://www.typescriptlang.org/) to transpile it into CommonJS.
74+
:::
75+
76+
### package.json
77+
78+
The [package.json](https://docs.npmjs.com/cli/v6/configuring-npm/package-json) file is required to publish a package to NPM:
79+
80+
```json
81+
{
82+
"name": "vuepress-plugin-foo",
83+
"version": "1.0.0",
84+
"keywords": [
85+
"vuepress-plugin",
86+
],
87+
"main": "lib/index.js",
88+
"files": [
89+
"lib"
90+
]
91+
}
92+
```
93+
94+
- Set `name` to follow the naming convention: `vuepress-plugin-xxx` or `@org/vuepress-plugin-xxx`.
95+
- Set `keywords` to include `'vuepress-plugin'`, so that users can search your plugin on NPM.
96+
- Set `main` to the plugin entry file.
97+
- Set `files` to only publish those files inside `lib` directory.

docs/zh/guide/advanced/plugin.md

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,97 @@
11
# 开发插件
22

3-
> TODO
3+
::: tip
4+
在阅读该指南之前,你最好先了解一下 VuePress 的 [架构](./architecture.md)
5+
:::
6+
7+
VuePress 插件是一个符合 [Plugin API](../../reference/plugin-api.md) 的普通 JavaScript 对象,称之为 *插件对象*
8+
9+
如果插件想要接收用户配置项,那么它可以是一个返回值为 *插件对象* 的函数,称之为 *插件函数*
10+
11+
<CodeGroup>
12+
<CodeGroupItem title="插件对象" active>
13+
14+
```js
15+
const fooPlugin = {
16+
name: 'vuepress-plugin-foo',
17+
// ...
18+
}
19+
```
20+
21+
</CodeGroupItem>
22+
23+
<CodeGroupItem title="插件函数">
24+
25+
```js
26+
const fooPlugin = (options, app) => {
27+
return {
28+
name: 'vuepress-plugin-foo',
29+
// ...
30+
}
31+
}
32+
```
33+
34+
</CodeGroupItem>
35+
</CodeGroup>
36+
37+
## 创建一个插件 Package
38+
39+
一个典型的插件 Package 的结构如下所示:
40+
41+
```bash
42+
vuepress-plugin-foo
43+
├─ lib
44+
│ └─ index.js
45+
└─ package.json
46+
```
47+
48+
### 插件入口
49+
50+
`lib/index.js` 文件是插件入口,它应当直接导出插件:
51+
52+
<CodeGroup>
53+
<CodeGroupItem title="CJS" active>
54+
55+
```js
56+
module.export = fooPlugin
57+
```
58+
59+
</CodeGroupItem>
60+
61+
<CodeGroupItem title="ESM">
62+
63+
```js
64+
export default fooPlugin
65+
```
66+
67+
</CodeGroupItem>
68+
</CodeGroup>
69+
70+
::: tip
71+
注意,插件入口会在 Node 中被加载,因此它应为 CommonJS 格式。
72+
73+
如果你使用 ESM 格式,你需要使用 [babel](https://babeljs.io/)[typescript](https://www.typescriptlang.org/) 来将它编译成 CommonJS 。
74+
:::
75+
76+
### package.json
77+
78+
为了把 Package 发布到 NPM 上,[package.json](https://docs.npmjs.com/cli/v6/configuring-npm/package-json) 文件是必需的:
79+
80+
```json
81+
{
82+
"name": "vuepress-plugin-foo",
83+
"version": "1.0.0",
84+
"keywords": [
85+
"vuepress-plugin",
86+
],
87+
"main": "lib/index.js",
88+
"files": [
89+
"lib"
90+
]
91+
}
92+
```
93+
94+
-`name` 按照约定命名: `vuepress-plugin-xxx``@org/vuepress-plugin-xxx`
95+
-`keywords` 中包含 `'vuepress-plugin'` ,这样用户可以在 NPM 上搜索到你的插件。
96+
-`main` 设为插件入口文件。
97+
- 设置 `files` ,仅发布 `lib` 目录下的文件。

0 commit comments

Comments
 (0)