UNPKG

@modern-js/doc-tools-doc

Version:

Website for @modern-js/doc-tools

58 lines (44 loc) 1.56 kB
# 编写一个插件 我们以注入一个全局组件为例,来看看如何定义和使用插件。 ### 1. 定义插件 ```tsx title="plugin.ts" import { DocPlugin } from '@modern-js/doc-tools'; export function pluginExample(slug: string): DocPlugin { // 组件路径,组件的内容你需要自己实现 const componentPath = path.join(__dirname, 'Example.tsx'); return { // 插件名称 name: 'plugin-example', // 全局组件的路径 globalUIComponents: [componentPath], // 构建阶段的全局变量定义 builderConfig: { source: { define: { 'process.env.slug': JSON.stringify(slug), }, }, }, }; } ``` ```tsx title="Example.tsx" import React from 'react'; const Example = () => { console.log(process.env.SLUG); return <div>Example</div>; }; export default Example; ``` 插件一般为一个函数,接收一些插件参数(可选),返回一个对象,对象中包含插件的名称以及其它配置。在上面的例子中,我们定义了一个插件,它的名称为`plugin-example`,它会在构建阶段定义一个全局变量`slug`,并且在文档中注入一个全局组件`Example.tsx`。 ### 2. 使用插件 在 `modern.config.ts` 中通过 `doc.plugins` 注册插件: ```tsx title="modern.config.ts" import { pluginExample } from './plugin'; export default { doc: { plugins: [pluginExample('test')], }, }; ``` 这样,Example 组件会自动在页面中渲染,并且我们可以在 Example 组件中访问到`process.env.slug`。