哪些會變、哪些不變
維持不變: 你的 React 元件、Tailwind 設定、測試、chrome.* API 呼叫,以及 @plasmohq/storage(如果你有用 — 它是 chrome.storage 的包裝,在這裡運作方式相同)。
會改變:
- 檔案慣例的進入點(
popup.tsx、contents/*.ts)變成真正的manifest.json中明確的條目。 package.json中的manifest欄位移到manifest.json。plasmo dev/plasmo build/plasmo package變成extension dev/extension build --zip。PLASMO_PUBLIC_*環境變數變成EXTENSION_PUBLIC_*。
步驟 1:安裝 Extension.js
步驟 2:撰寫 manifest
Plasmo 會從package.json 加上檔案慣例產生 manifest。Extension.js 則把 manifest.json 當作事實來源。逐條轉換每個慣例:
| Plasmo 慣例 | manifest.json 條目 |
|---|---|
popup.tsx 或 src/popup.tsx | "action": {"default_popup": "popup/index.html"} |
options.tsx | "options_ui": {"page": "options/index.html"} |
newtab.tsx | "chrome_url_overrides": {"newtab": "newtab/index.html"} |
background.ts | "background": {"service_worker": "background.ts"} |
contents/inline.ts 帶 CS 設定 | "content_scripts": [{...}] 條目 |
package.json 中的 manifest 欄位 | 直接合併到 manifest.json |
popup/index.html
.ts/.tsx。Extension.js 會在建置時編譯它們。
步驟 3:轉換 content scripts
Plasmo 從匯出的PlasmoCSConfig 讀取 match:
contents/inline.tsx
manifest.json
getRootContainer 與 anchor 的 content script UI),請改用一般的 DOM 程式碼掛載元件:建立一個容器元素,將它附加到你原本錨定的位置,然後渲染到其中。完整模式請見 Content scripts,包括以 shadow DOM 隔離樣式的方式。
步驟 4:環境變數與訊息傳遞
- 將
.env檔案與程式碼中的PLASMO_PUBLIC_*改名為EXTENSION_PUBLIC_*。參見環境變數。 @plasmohq/storage可以原封不動繼續使用。@plasmohq/messaging依賴 Plasmo 的background/messages/*建置慣例。請以標準的chrome.runtime.onMessage監聽器在 background script 中取代處理函式。參見訊息傳遞。
步驟 5:更新 package.json 腳本
--target=firefox-mv2 的地方,Extension.js 直接以瀏覽器作為目標:
dist/chrome 與 dist/firefox,附帶各瀏覽器正確的 manifest,以及可上傳到 Chrome Web Store 與 addons.mozilla.org 的 .zip 壓縮檔。
步驟 6:驗證
--browser=firefox 在 Firefox 上跑相同的驗證。若你的程式碼呼叫 chrome.* 並希望同一份程式碼可在 Firefox 上執行,請使用 --polyfill。
常見陷阱
assets/中的圖示: Plasmo 會從assets/icon.png自動產生各種尺寸的圖示。Extension.js 使用 manifest 中"icons"所宣告的內容;請自行匯出所需的尺寸。參見 Icons。~import 別名: Plasmo 將~別名指向專案根目錄。請在tsconfig.json的 paths 中設定它,或改用相對路徑 import。參見 Path resolution。- Storage hooks:
@plasmohq/storage/hook的useStorage可原封不動使用;它只依賴 React 與chrome.storage。

