Skip to content

[v1.4] 压缩 ts.worker.js 至 4MB 以通过 AMO 验证,修复 MV3 background permission 错误#1221

Open
cyfung1031 wants to merge 28 commits intoscriptscat:release/v1.4from
cyfung1031:pr-mv3-firefox-support-003
Open

[v1.4] 压缩 ts.worker.js 至 4MB 以通过 AMO 验证,修复 MV3 background permission 错误#1221
cyfung1031 wants to merge 28 commits intoscriptscat:release/v1.4from
cyfung1031:pr-mv3-firefox-support-003

Conversation

@cyfung1031
Copy link
Collaborator

@cyfung1031 cyfung1031 commented Feb 7, 2026

压缩 ts.worker.js 绕过 AMO 5MB 限制,修正 Firefox MV3 permission 错误

问题背景

Firefox MV3 扩充套件上架至 addons.mozilla.org (AMO) 时遭遇两个阻断性问题:

  1. ts.worker.js 超过 5MB 限制:Monaco Editor 的 TypeScript Worker 打包后约 5.9MB,超出 Mozilla 单档上限,导致 AMO 验证失败。
  2. Firefox MV3 禁止 worker-src blob::无法透过 Blob URL 动态建立 Worker,排除了原本的绕过方案。
  3. background permission 检查报错:Firefox MV3 不支援 background permission,相关检查会抛出错误。

解决方案

1. 新增 ZipExecutionPlugin(核心改动)

在 Rspack 打包阶段新增自订插件 rspack-plugins/ZipExecutionPlugin.ts

  • 使用 acorn 解析 ts.worker.js 的 AST
  • 使用 magic-string 做字符串操作
  • 对 worker 代码进行 deflate 压缩,打包时直接嵌入压缩后的 payload
  • Worker 启动时以原生 JS inflate 解压后再执行,不依赖任何外部库,也不使用 evalblob:

压缩效果:5.9MB → ~3.8MB,低于 AMO 的 5MB 限制,且完全符合 Firefox MV3 的 CSP 规范。

2. 调整 Rspack 打包设定(rspack.config.ts

  • 升级输出目标至 ECMA 2022(配合原生 JS 解压器使用的新特性)
  • 整合 ZipExecutionPlugin 至打包流程

3. Firefox 专用打包修正(scripts/pack.js

  • 加入 data_collection_permissions 字段(AMO 审核必要项目)
  • 移除对 background permission 的检查(Firefox MV3 不支援此 permission)

4. UI 层移除 background permission 检查(RuntimeSetting/index.tsx

  • 移除前端 UI 中对 background permission 的检查逻辑,避免在 Firefox 上报错

修改档案一览

档案 说明
rspack-plugins/ZipExecutionPlugin.ts 新增 自订压缩插件
rspack.config.ts 整合插件、升级 ECMA 目标
scripts/pack.js Firefox 打包修正,加入 AMO 必要欄位
src/pages/components/RuntimeSetting/index.tsx 移除 background permission UI 检查
package.json 新增 acornmagic-string 依赖
pnpm-lock.yaml 锁定档同步更新

测试结果

  • ✅ 通过 AMO 官方验证工具
  • ✅ 在 Zen Browser(Firefox 核心)上主要功能正常运行
  • ✅ 压缩后档案大小 ~3.8MB,具备未来 Monaco TS 定义继续扩充的空间

备注

  • 解压器为原生 JS 实现,不依赖 pako 等外部库,记忆体使用更低,效能与 pako 相当(解压约 30~60ms,于 Worker 后台执行,不影响体感)
  • 独立实现的 inflateRaw 已开源:js-vanilla/inflate-raw

@cyfung1031 cyfung1031 marked this pull request as draft February 7, 2026 17:08
@cyfung1031

This comment was marked as outdated.

@cyfung1031

This comment was marked as outdated.

@cyfung1031

This comment was marked as outdated.

@cyfung1031

This comment was marked as outdated.

@cyfung1031 cyfung1031 marked this pull request as ready for review February 10, 2026 15:59
@cyfung1031 cyfung1031 force-pushed the pr-mv3-firefox-support-003 branch from 7ae0298 to 7bfacbb Compare February 11, 2026 03:48
@cyfung1031 cyfung1031 changed the title [v1.3] 让 MV3 兼容更多浏览器 [v1.3] 提升 MV3 版本在更多浏览器(特别是 Firefox)的兼容性 Feb 11, 2026
@cyfung1031 cyfung1031 marked this pull request as draft February 15, 2026 01:59
@cyfung1031 cyfung1031 changed the title [v1.3] 提升 MV3 版本在更多浏览器(特别是 Firefox)的兼容性 [v1.4] 提升 MV3 版本在更多浏览器(特别是 Firefox)的兼容性 Feb 24, 2026
@cyfung1031 cyfung1031 changed the base branch from release/v1.3 to release/v1.4 March 15, 2026 05:10
@cyfung1031 cyfung1031 marked this pull request as ready for review March 22, 2026 03:17
@cyfung1031 cyfung1031 changed the title [v1.4] 提升 MV3 版本在更多浏览器(特别是 Firefox)的兼容性 [v1.4] 压缩 ts.worker.js 至 4MB 以通过 AMO 验证,修复 MV3 background permission 错误 Mar 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant