MV2/MV3 双版本 Rspack 架构 #1336
cyfung1031
started this conversation in
General
Replies: 2 comments 7 replies
-
|
以后差距会越来越大,这种方案有点折磨了 |
Beta Was this translation helpful? Give feedback.
6 replies
-
|
以前有人針對 Background User Script 提問過 https://groups.google.com/a/chromium.org/g/chromium-extensions/c/q9H8KwFLkMs 先開了一個 貼文 問一下 sandbox https://discourse.mozilla.org/t/firefox-mv3-any-plan-to-support-scripting-eval-in-sandbox/147853 |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Good thinking — so you want a single codebase that compiles to both MV2 and MV3 bundles, where browser API differences (like
chrome.actionvschrome.browserAction) are abstracted away.The Core Idea
A compat layer that always exposes MV3-style APIs, but internally routes to MV2 APIs when built for MV2. Rspack injects the right implementation via a module alias at build time.
Project Structure
The Compat Layer
src/lib/browser/mv3.ts— pure passthrough:src/lib/browser/mv2.ts— shims MV3 APIs to MV2:src/lib/browser/index.ts— this file is what you always import, and Rspack aliases it:Your App Code (one version forever)
Rspack Config — Dual Build
Define Constants Per Build (optional but useful)
Add to each config so you can conditionally compile code:
Then in code:
Declare the global in a
.d.ts:Summary of the Pattern
The alias swap is the only build-time difference. Your source code stays 100% MV3-flavored.
简单来说,分开 mv2 mv3 folder 做
预设 export * from mv3
实际打包时,在 rspack 动手脚,把 mv3库换成 mv2库
只要两者的 function signature 一致就可以了
还可以用
__MV__做 tree-shakingBeta Was this translation helpful? Give feedback.
All reactions