Skip to content
开发文档
插件
发布

发布插件

¥Publishing plugins

如果你喜欢阅读代码,可以参考 官方插件的存储库 (opens in a new tab)

¥If you prefer reading codes, you can refer to the repository for official plugins (opens in a new tab).

创建 npm 包

¥Creating a npm package

构建一个插件作为 wasm

¥Building a plugin as a wasm

你可以通过运行以下命令将插件作为 wasm 文件运行

¥You can run your plugin as a wasm file by running

cargo build-wasi --release // build wasm32-wasi target binary
cargo build-wasm32 --release // build wasm32-unknown-unknown target binary

它将创建 target/wasm32-wasi/release/your_plugin_name.wasmtarget/wasm32-unknown-unknown/release/your_plugin_name.wasm,具体取决于你的配置。

¥It will create target/wasm32-wasi/release/your_plugin_name.wasm or target/wasm32-unknown-unknown/release/your_plugin_name.wasm, depending on your config.

为插件创建 npm 包

¥Creating a npm package for plugin

将以下内容添加到你的 package.json

¥Add the following to your package.json:

package.json
{
    "main": "your_plugin_name.wasm",
    "scripts": {
        "prepack": "cargo prepublish --release && cp target/wasm32-wasi/release/your_plugin_name.wasm ."
    },
}

高级:改进你的插件

¥Advanced: Improving your plugin

调整较小二进制文件的配置

¥Adjusting configuration for smaller binary

你可以通过配置 Cargo 来减小插件的大小。

¥You can reduce the size of the plugin by configuring cargo.

Cargo.toml 文件中,你可以添加以下行。

¥In your Cargo.toml file, you can add the following lines.

Cargo.toml
[profile.release]
# This removes more dead code
codegen-units = 1
lto = true
# Optimize for size
opt-level = "s"
# Optimize for performance, this is default so you don't need to specify it
# opt-level = "z"
 
# Strip debug symbols
strip = "symbols"

删除发布模式的日志

¥Removing log for release mode

如果你的 crate 的日志记录过多,你可以通过启用 release_max_level_*tracing 来删除它,例如

¥If logging of your crate is too much, you can remove it by enabling release_max_level_* of tracing, like

tracing = { version="0.1", features = ["release_max_level_info"] }