Js Plugin Api
Farm Js Plugin 设计了类似 rollup 风格的设计插件系统,可以轻松地从 Rollup/Vite/Webpack 迁移您的插件/项目。
配置 Js 插件
通过 plugins
选项添加 JS 插件:
farm.config.ts
import { defineConfig } from "@farmfe/core";
// import a js plugin
import farmPluginFoo from "farm-plugin-foo";
export default defineConfig({
// configuring it in plugins
plugins: [farmPluginFoo()],
});
编写Js插件
Farm Js 插件是一个普通的 javascript 对象,它公开了一组 hook
。 例如:
my-farm-plugin.ts
// 创建一个插件文件,导出一个返回 `JsPlugin` 对象的插件函数:
import type { JsPlugin } from '@farmfe/core';
// 插件选项
export interface PluginOptions {
test: boolean;
}
// 导出插件函数
export default function MyPlugin(options: PluginOptions): JsPlugin {
// 读取插件 options
const { test } = options;
// 返回一个暴露钩子的对象
return {
name: 'my-farm-plugin',
// 使用load hook加载自定义模块
load: {
filters: {
resolvedPaths: ['\\.test$'] // 过滤文件以提高性能
},
async executor({ resolvedPath }) {
if (test && resolvedPath.endsWith('.test')) {
return {
content: 'test file',
sourceMap: null
}
}
}
}
}
}
备注
- Farm提供
create-farm-plugin
工具来帮助您快速创建和开发您的js插件。 有关编写 JS 插件的更多详细信息,请参阅编写 JS 插件
Plugin Hook Overview
Js 插件 Hook 与 Rust 插件相同,请参阅 Rust 插件 Hook 概述。
备注
并非所有钩子都暴露给 Js 插件,只有本文档中列出的钩子可用。
hooks
name
- type:
string
- required:
true
该插件的名称,不能为空。
export default function MyPlugin() {
return {
name: 'my-plugin',
// ...
}
}