Skip to content

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

Merged
CodFrm merged 37 commits intoscriptscat:release/v1.4from
cyfung1031:pr-mv3-firefox-support-003
Mar 29, 2026
Merged

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

Conversation

@cyfung1031
Copy link
Copy Markdown
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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

混淆代码 不能接受,不能引用库?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不是 混淆代码 是 极小化
代码是 decompress 的 algorithm

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那也无法接受,不好审查

Copy link
Copy Markdown
Collaborator Author

@cyfung1031 cyfung1031 Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改了

提交代码没办法吧
打包的都是 minimized
不只是这个. 引用库 呀 service worker js 什么的全部都是 minimized
原本的 ts worker 也是 minimized
Screenshot 2026-03-28 at 12 26 43


不过在这档案就倒过来把原本的解压代码写出来吧

Screenshot 2026-03-28 at 12 10 50

原生库是 异步。速度比较慢,还要一定的浏览器版本。异步载入可能有差

即使引用 pako, 还是要把代码塞于 打包后的档案
source code 看起来好看
但打包代码还是一样

也是「不好审查」

不过你本身的所有 js 都是压缩过
其实不会有 「不好审查」 这回事吧

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那是编译后的产物,源代码肯定无法接受

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

该 PR 面向 Firefox AMO 上架限制与 Firefox MV3 兼容性问题:通过在 Rspack 构建阶段压缩 Monaco 的 ts.worker.js 以规避单文件大小限制,并移除/规避 Firefox MV3 下不支持的 background permission 检查,避免运行时报错。

Changes:

  • 新增 ZipExecutionPlugin,在构建时对 ts.worker.js 做字符串抽取 + deflateRaw 压缩,并在 Worker 启动时原生 inflate 解压加载。
  • 调整 Rspack 压缩输出目标至 ES2022,并将新插件接入构建流程。
  • Firefox 打包与前端 UI 移除/隐藏 background permission 相关检查与交互,避免 Firefox MV3 不支持导致的错误。

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
src/pages/components/RuntimeSetting/index.tsx Firefox 下隐藏后台权限开关,并在逻辑层跳过 background permission 检查
scripts/pack.js Firefox 打包时移除 background optional permission,补充 AMO 相关字段,调整打包包含文件
rspack.config.ts 接入 ZipExecutionPlugin,并将 minimizer 的 ecma 目标提升到 2022
rspack-plugins/ZipExecutionPlugin.ts 新增:构建期压缩并包装 ts.worker.js 的核心实现
package.json 新增构建期依赖 acornmagic-string
pnpm-lock.yaml 锁文件随依赖变更更新
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

cyfung1031 and others added 2 commits March 28, 2026 11:35
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@cyfung1031 cyfung1031 marked this pull request as draft March 28, 2026 02:58
@cyfung1031 cyfung1031 marked this pull request as ready for review March 28, 2026 03:12
@CodFrm CodFrm merged commit b3db769 into scriptscat:release/v1.4 Mar 29, 2026
3 of 4 checks passed
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.

3 participants