Skip to content

打包与分发 ​

加载器运行 JavaScript。使用 TypeScript 开发时,发布内容需要包含编译后的 .js 和实际使用的模板资源。

推荐目录 ​

text
my-plugin/
├─ package.json
├─ index.js
├─ src/
│  └─ index.ts
├─ dist/
│  └─ index.js
├─ resources/
├─ tsconfig.json
├─ README.md
└─ LICENSE

根 index.js 用作稳定入口:

js
export { Example } from './dist/index.js'

不要只把构建文件放进 dist/ 而没有可扫描的入口,也不要把 .ts 入口交给加载器直接运行。

package.json ​

json
{
  "name": "my-maworld-plugin",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "scripts": {
    "build": "tsc -p tsconfig.json",
    "typecheck": "tsc -p tsconfig.json --noEmit"
  },
  "dependencies": {
    "maworld": "link:../.."
  },
  "devDependencies": {
    "typescript": "^5.9.3",
    "@types/node": "^24.0.0"
  }
}

link:../.. 假定插件位于框架 plugins/插件名/。文档示例是 Git 安装布局,不是可以原样发布到 npm 的依赖约定;若走 npm 分发,需要另外设计宿主依赖解析,并确保框架包实际可获取。

tsconfig.json ​

json
{
  "compilerOptions": {
    "target": "ES2023",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "rootDir": "src",
    "outDir": "dist",
    "strict": true,
    "declaration": true,
    "skipLibCheck": true,
    "types": ["node"]
  },
  "include": ["src/**/*.ts"]
}

ESM 相对导入在 TypeScript 源码里也使用运行时的 .js 扩展名。模板资源不由 TypeScript 自动复制,按约定保留在插件根目录的 resources/。

最小 TypeScript 源码 ​

ts
import { Plugin } from 'maworld'

export class Example extends Plugin {
  constructor() {
    super({
      name: 'TypeScript 示例',
      rule: [{ reg: '^#类型示例$', fnc: 'hello' }],
    })
  }

  async hello(): Promise<boolean> {
    await this.reply('TypeScript 插件已加载')
    return true
  }
}

构建与验证 ​

将插件放入框架 plugins/ 后,在框架根目录安装依赖,再按插件自己的包名构建:

sh
pnpm install
pnpm --filter my-maworld-plugin typecheck
pnpm --filter my-maworld-plugin build

重启并检查加载日志。验证普通用户、主人、群聊、私聊、协议缺少能力和渲染失败等实际场景。热更新涉及资源时还需验证清理与恢复。

Git 仓库分发 ​

把入口、编译产物、模板、依赖清单、README 和许可证提交到插件仓库。不要提交 node_modules/、账号凭据、用户配置、业务数据和日志。

安装者获取插件后,在框架根目录安装依赖并重启;私密插件仓库还需独立的 Git 访问权限。README 应说明要求的 maworld 版本、支持的协议、安装位置、配置格式和命令权限。

升级约定 ​

插件更新不要直接覆盖用户数据和配置。需要迁移数据时使用明确的版本判断和可恢复流程。保持稳定的导出类名和入口,破坏性改动写入更新记录。

文档站本身的构建方式与插件不同,详见文档仓库根目录的 README。不要把 VitePress 依赖安装到机器人运行目录。

TypeScript + Node.js · 文档对应 maworld 1.0.0