liufumingren 6818da707e build(extension): 分发用压缩产物(esbuild minify,不混淆)
MV3 没有编译产物,桌面安装包会把插件源码装在 <安装目录>\extension,用户能直接看到
带注释的代码并随手改。这里加一层「压缩」:去注释、压空白、缩短标识符,体积 71KB → 30KB。

- backend/scripts/deploy/build-extension.cjs(新):esbuild 逐文件 minify 输出 extension/dist,
  不做 bundle(importScripts('lib/rules.js') 与各处路径原样有效),无 eval/Function,
  满足 MV3 的 CSP 与商店「代码可读」要求;
- publish-extension.cjs:打包/压缩包一律从 dist 取源;商店 zip 改用自写的
  zip-util.cjs —— PowerShell 5.1 的 Compress-Archive 会把条目名写成 `src\x.js`,
  8.3 短路径下还会多出前导 `/`,商店会当成怪文件名;
- desktop csproj 改为打 ..\extension\dist\**,installer/build.ps1 在 dotnet publish 前
  自动跑一次 bundle(找不到 node/esbuild 明确报错,避免打进上次的旧 dist);
- extension:rules.js 的兜底 urlTemplate/fallback 从 localhost:7301 改为线上站点
  (和服务端默认值保持一致),版本 1.1.2;
- backend 增加 esbuild devDependency 与 npm run extension:bundle;文档同步。

验证:5 个 js 全部 node --check 通过;dist 里 manifest 引用的 14 个文件都在;
Chrome --pack-extension 成功(扩展 ID 不变);zip 条目全部是标准 `/` 且 manifest.json 在根目录、
可正常解压;desktop Debug 编译 0 警告 0 错误,输出目录里是指向 dist 的压缩版(无 keys/README/test)。

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-01 03:53:13 +08:00

137 lines
5.0 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.

'use strict';
/**
* 极简 ZIP 打包(deflate,无第三方依赖)
*
* 为什么不用 PowerShell 的 `Compress-Archive` / `ZipFile::CreateFromDirectory`:
* - PowerShell 5.1(.NET Framework)写出来的条目名是**反斜杠**(`src\content.js`),
* 而 ZIP 规范要求 `/`,商店上传/解压工具会把它当成带 \ 的怪文件名;
* - 走 8.3 短路径(`C:\Users\LFMING~1\...`)时,`FullName.Substring($src.Length + 1)`
* 算出来的相对路径还会多一个前导 `/`,manifest.json 就不在根目录了。
*
* 这里自己写:条目名一律 `/`、无前导斜杠、UTF-8 文件名、Deflate 压缩。
* 只支持「单文件 < 4GB、总长 < 4GB」的普通包(插件包够用,不需要 ZIP64)。
*/
const fs = require('node:fs');
const path = require('node:path');
const zlib = require('node:zlib');
const CRC_TABLE = (() => {
const table = new Int32Array(256);
for (let index = 0; index < 256; index++) {
let value = index;
for (let bit = 0; bit < 8; bit++) {
value = value & 1 ? 0xedb88320 ^ (value >>> 1) : value >>> 1;
}
table[index] = value;
}
return table;
})();
function crc32(buffer) {
let crc = -1;
for (let index = 0; index < buffer.length; index++) {
crc = CRC_TABLE[(crc ^ buffer[index]) & 0xff] ^ (crc >>> 8);
}
return (crc ^ -1) >>> 0;
}
/** 递归列出文件(返回相对路径,统一用 `/`) */
function listFiles(dir, base = dir, files = []) {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) {
listFiles(full, base, files);
} else if (entry.isFile()) {
files.push({ full, name: path.relative(base, full).split(path.sep).join('/') });
}
}
return files;
}
/** 转成 DOS 时间(ZIP 头里是 1980 起的本地时间) */
function toDosTime(date) {
const year = Math.max(date.getFullYear(), 1980);
return {
time: (date.getHours() << 11) | (date.getMinutes() << 5) | (date.getSeconds() >> 1),
date: ((year - 1980) << 9) | ((date.getMonth() + 1) << 5) | date.getDate(),
};
}
/**
* 把目录打成 zip
* @param {string} dir 源目录
* @param {string} zipPath 目标 zip(不存在会自动创建父目录)
* @returns {{ files: number, bytes: number }} 条目数与压缩包大小
*/
function zipDirectory(dir, zipPath) {
const files = listFiles(dir).sort((a, b) => a.name.localeCompare(b.name));
const localParts = [];
const centralParts = [];
let offset = 0;
for (const file of files) {
const data = fs.readFileSync(file.full);
const deflated = zlib.deflateRawSync(data, { level: 9 });
const name = Buffer.from(file.name, 'utf8');
const crc = crc32(data);
const { time, date } = toDosTime(fs.statSync(file.full).mtime);
const local = Buffer.alloc(30);
local.writeUInt32LE(0x04034b50, 0); // 局部文件头签名
local.writeUInt16LE(20, 4); // 解压所需版本
local.writeUInt16LE(0x0800, 6); // 文件名 UTF-8
local.writeUInt16LE(8, 8); // Deflate
local.writeUInt16LE(time, 10);
local.writeUInt16LE(date, 12);
local.writeUInt32LE(crc, 14);
local.writeUInt32LE(deflated.length, 18);
local.writeUInt32LE(data.length, 22);
local.writeUInt16LE(name.length, 26);
local.writeUInt16LE(0, 28); // 无扩展字段
localParts.push(local, name, deflated);
const central = Buffer.alloc(46);
central.writeUInt32LE(0x02014b50, 0); // 中央目录签名
central.writeUInt16LE(20, 4); // 打包者版本
central.writeUInt16LE(20, 6); // 解压所需版本
central.writeUInt16LE(0x0800, 8); // 文件名 UTF-8
central.writeUInt16LE(8, 10); // Deflate
central.writeUInt16LE(time, 12);
central.writeUInt16LE(date, 14);
central.writeUInt32LE(crc, 16);
central.writeUInt32LE(deflated.length, 20);
central.writeUInt32LE(data.length, 24);
central.writeUInt16LE(name.length, 28);
central.writeUInt16LE(0, 30); // 扩展字段长度
central.writeUInt16LE(0, 32); // 注释长度
central.writeUInt16LE(0, 34); // 起始磁盘号
central.writeUInt16LE(0, 36); // 内部属性
central.writeUInt32LE(0, 38); // 外部属性
central.writeUInt32LE(offset, 42); // 局部头偏移
centralParts.push(central, name);
offset += local.length + name.length + deflated.length;
}
const centralBuffer = Buffer.concat(centralParts);
const end = Buffer.alloc(22);
end.writeUInt32LE(0x06054b50, 0); // 目录结束签名
end.writeUInt16LE(0, 4); // 本磁盘号
end.writeUInt16LE(0, 6); // 中央目录起始磁盘
end.writeUInt16LE(files.length, 8);
end.writeUInt16LE(files.length, 10);
end.writeUInt32LE(centralBuffer.length, 12);
end.writeUInt32LE(offset, 16);
end.writeUInt16LE(0, 20); // 注释长度
fs.mkdirSync(path.dirname(zipPath), { recursive: true });
fs.writeFileSync(zipPath, Buffer.concat([ ...localParts, centralBuffer, end ]));
return { files: files.length, bytes: fs.statSync(zipPath).size };
}
module.exports = { zipDirectory };