Files
iwb-plugins-desktop/scripts/build-plugin.mjs
T
qiqi e7b6e23f8f feat(plugin): 新增 image-annotate 标注/修图插件 + 构建链
- 编辑器逐字移植自 iwb-plaus/iwb-cutout-extension(11 模块 + 2 个 shared 文件,纯 Canvas 2D、零第三方库)
- plugin/ 存 ESM 源码,src/index.js 是构建产物(提交进仓,CI 只校验不构建)
- scripts/build-plugin.mjs:esbuild 打包 + banner/footer 手写单引号 manifest + 产物自检
- 能力 commands / nodeActions / canvas / canvasWrite;入口 = 节点漂浮菜单 + 右键命令
- 导出模式按编辑器字面量映射:replace → setNodeImage,newNode → createImageNode
2026-10-19 14:03:55 +08:00

95 lines
3.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
/**
* 构建「需要打包的插件」:ESM 源码 → 单文件 IIFE 产物。
*
* 用法:node scripts/build-plugin.mjs <插件目录名> (或 npm run build -- <插件目录名>)
* 输入:<插件>/plugin/{entry.js, manifest.banner.js, manifest.footer.js, *.css}
* 输出:<插件>/<ibwPlugin.entry>(通常是 src/index.js,产物提交进仓,CI 不构建)
*
* 为什么分三段拼接,而不是把 manifest 交给 esbuild:
* 1. esbuild 的打印器会把字符串统一改写成双引号,而 scripts/validate.mjs 只认 '...' 形式;
* 2. 校验器用「文件里首个 id: '...'」判定插件 id,manifest 必须排在 bundle 之前。
* 所以 manifest 走 banner 通道(原样文本)、bundle 走 esbuild、注册调用走 footer 通道(原样文本)。
*/
import { readFileSync, existsSync } from 'node:fs'
import { resolve, dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..')
const name = process.argv[2]
if (!name) {
console.error('用法: node scripts/build-plugin.mjs <插件目录名>')
process.exit(1)
}
const dir = join(ROOT, name)
const pkgPath = join(dir, 'package.json')
if (!existsSync(pkgPath)) {
console.error('[build] 不是插件目录(缺 package.json):' + dir)
process.exit(1)
}
let pkg
try { pkg = JSON.parse(readFileSync(pkgPath, 'utf8')) } catch (error) {
console.error('[build] package.json 解析失败:' + error.message)
process.exit(1)
}
const meta = pkg.ibwPlugin || {}
const id = String(meta.id || '')
const outFile = resolve(dir, String(meta.entry || 'src/index.js'))
const entryFile = join(dir, 'plugin', 'entry.js')
const bannerFile = join(dir, 'plugin', 'manifest.banner.js')
const footerFile = join(dir, 'plugin', 'manifest.footer.js')
for (const file of [entryFile, bannerFile, footerFile]) {
if (!existsSync(file)) {
console.error('[build] 缺少构件:' + file.replace(ROOT + '/', ''))
process.exit(1)
}
}
let esbuild
try {
esbuild = await import('esbuild')
} catch (error) {
console.error('[build] 未安装 esbuild(devDependency),请先在本仓执行:npm install')
process.exit(1)
}
// 版本只有一处真源:package.json → 注入 banner 的 __PLUGIN_VERSION__ 占位符。
const banner = readFileSync(bannerFile, 'utf8').replace(/__PLUGIN_VERSION__/g, String(pkg.version))
const footer = readFileSync(footerFile, 'utf8')
await esbuild.build({
entryPoints: [entryFile],
outfile: outFile,
bundle: true,
format: 'iife',
platform: 'browser',
target: ['chrome100', 'edge100', 'firefox100', 'safari15'],
loader: { '.css': 'text' }, // 样式以字符串内联,运行时注入 <style>(宿主 CSP 允许 unsafe-inline)
charset: 'utf8',
legalComments: 'none',
banner: { js: banner },
footer: { js: footer },
logLevel: 'warning',
})
// ===== 产物自检:宁可构建失败,也不交出会被 CI 拦下或被宿主 fail-closed 拒载的文件 =====
const out = readFileSync(outFile, 'utf8')
const fail = (message) => {
console.error('✗ ' + name + ' 产物自检失败:' + message)
process.exit(1)
}
if (!/(?:^|[\s{,])id:\s*'([^']+)'/.exec(out) || /(?:^|[\s{,])id:\s*'([^']+)'/.exec(out)[1] !== id) {
fail('首个 id 字面量不是 ' + id + '(manifest 没有排在 bundle 之前?)')
}
if (out.indexOf('IWBPluginHost.register(') < 0) fail('缺少 IWBPluginHost.register(')
if (!out.includes("version: '" + pkg.version + "'")) fail('version 与 package.json(' + pkg.version + ')不一致')
if (!/apiVersion:\s*1\b/.test(out)) fail('缺少 apiVersion: 1')
if (!/capabilities:\s*\[/.test(out)) fail('缺少 capabilities 数组')
if (/\beval\s*\(|new Function\s*\(/.test(out)) fail('含 eval/new Function,宿主 CSP(无 unsafe-eval)会拦')
console.log('✓ ' + name + ' → ' + outFile.replace(ROOT + '/', '') +
' (' + out.length + ' B, id=' + id + ', version=' + pkg.version + ')')