@multiplatform.one/vite-plugin-webext

中文文档
7.82.2发布于 2026/11/02Apache-2.0TypeScriptESM + CJS

Vite config factories for building a browser web extension (MV3 views, background service worker, content scripts) as a target of a multiplatform.one One app — workspace source aliases, the one-server-only browser stub, Tamagui wiring, and the manifest/de

原始使用文档
文档目录4 节
  1. Usage
  2. Required dependencies (the real consumer contract)
  3. one must be aliased to the router seam
  4. workspaceRoot semantics

@multiplatform.one/vite-plugin-webext

Vite config factories for building a browser web extension (MV3 views, background service worker, content scripts) as a target of a multiplatform.one One app. Three vite builds cover the three JS worlds (DOM extension pages, worker, injected content script); runWebextPrepare writes the manifest and the dev-mode HMR scaffolding into a loadable staging dir:

<stagingDir>/manifest.json
<stagingDir>/dist/views/<view>/index.html   (popup/options/sidepanel/…)
<stagingDir>/dist/background/index.mjs      (service worker / bg script)
<stagingDir>/dist/contentScripts/index.global.js
<stagingDir>/dist/contentScripts/webext.css (web-accessible resource)

Usage

One config file per JS world, plus a prepare script. Every directory under <targetDir>/views/ with an index.html becomes an extension page.

// vite.config.webext.ts — extension pages (popup/options/…)
import { resolve } from "node:path";
import { createWebextViewsConfig } from "@multiplatform.one/vite-plugin-webext";
import { defineConfig } from "vite";
import packageJson from "./package.json";

export default defineConfig(async () =>
  createWebextViewsConfig({
    targetDir: resolve(import.meta.dirname, "webext"),
    stagingDir: resolve(import.meta.dirname, "dist-webext"),
    tamaguiConfig: resolve(import.meta.dirname, "config/tamagui.config.ts"),
    packageInfo: packageJson,
    // MANDATORY for extension builds — see "one must be aliased" below.
    aliases: [{ find: /^one$/, replacement: "@multiplatform.one/router/seam" }],
  }),
);

createWebextBackgroundConfig (sync) and createWebextContentConfig (async) take the same options for the worker and content-script builds; runWebextPrepare({ targetDir, stagingDir, getManifest, isDev, port, watch }) stages the manifest, static assets, and dev HMR scaffolding.

Required dependencies (the real consumer contract)

Declared peers — install them next to this plugin:

packagewhy
vite >=5the build tool itself
@vitejs/plugin-reactviews build (react fast-refresh + jsx). Lazy-imported at factory call
@tamagui/vite-pluginviews + content builds (static extraction). Lazy-imported

Beyond the declared peers, a real app build needs:

  • @tamagui/web as a direct dependency of your app. The Tamagui static extractor bundles your tamaguiConfig to <cwd>/.tamagui/tamagui.config.{cjs,mjs} and requires it from there, with @tamagui/web left external. Under pnpm's default isolated node_modules, transitive packages are not resolvable from your app, so the extractor fails with "Error bundling tamagui config: Cannot find package '@tamagui/web'…" — and (upstream) swallows it, silently shipping a deoptimized runtime-styled build. This plugin turns that into a hard error in production builds; fix it by adding @tamagui/web to your app's dependencies.
  • react-i18next + i18next, if you render the i18n-using catalog components. @multiplatform.one/components translates built-in copy (Alert, Toast, Carousel, ConfirmDialog, Pagination, views/, layouts/, …) via an optional react-i18next peer. Apps that never render those components may omit both packages and tree-shake the components away; apps that do render them get a named runtime error telling you to install react-i18next and i18next.

one must be aliased to the router seam

Extension pages must NOT bundle the real one runtime: it drags in @react-navigation/core (and server-only modules) and the build fails. This is mandatory, not stylistic — alias one to the stack-navigation seam in every webext config that renders shared feature code:

aliases: [{ find: /^one$/, replacement: "@multiplatform.one/router/seam" }],

The seam (@multiplatform.one/router/seam) implements One's router API over DOM pages, so shared features/ code written against One's router works unchanged inside the extension.

Relatedly, one's client code imports ./vite/one-server-only.mjs (node-only); the factories redirect any one-server-only id to one's published browser/native no-op. The redirect resolves the installed one package through node resolution (build cwd, then workspaceRoot); when one is not installed at all the redirect is skipped with a one-line warning and normal resolution proceeds.

workspaceRoot semantics

workspaceRoot defaults to the git toplevel (git rev-parse --show-toplevel) of the build cwd. It is used for two things:

  1. Workspace source aliases. Inside the multiplatform.one monorepo, every public/* package (and its subpath exports) is aliased to its TypeScript source so builds never consume stale dist artifacts. Out-of-repo there is no public/ under the root, the aliasing no-ops, and @multiplatform.one/* resolves from node_modules like any other package — the factories log one line when this happens: no public/ under <root> — resolving @multiplatform.one/* from node_modules.
  2. The one-server-only redirect falls back to resolving one from workspaceRoot when it cannot be resolved from the build cwd.

Pass workspaceRoot explicitly when your app builds from a directory whose git toplevel is not the dependency root (e.g. a nested repo).

版本与发布记录

@multiplatform.one/vite-plugin-webext 共发布 135 个版本,首次发布于 2026/08/13;过去 12 个月发布了 135 个版本。

版本发布日期距今
7.82.2latest2026/11/021 天前
7.82.12026/11/012 天前
7.82.02026/11/012 天前
7.81.22026/11/012 天前
7.81.12026/11/012 天前
7.81.02026/11/012 天前

关于 @multiplatform.one/vite-plugin-webext 的常见问题

@multiplatform.one/vite-plugin-webext 是什么?

Vite配置工厂用于构建浏览器web扩展(MV3视图,后台服务工作者,内容脚本)作为一个多平台的目标。一个应用程序-工作区源别名,一个服务器专用的浏览器存根,Tamagui连接和manifest/de

如何安装 @multiplatform.one/vite-plugin-webext?

在项目目录运行 npm install @multiplatform.one/vite-plugin-webext,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:vite(>=5)、@tamagui/vite-plugin(*)、@vitejs/plugin-react(>=4)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@multiplatform.one/vite-plugin-webext 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@multiplatform.one/vite-plugin-webext 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@multiplatform.one/vite-plugin-webext 可以商用吗?

@multiplatform.one/vite-plugin-webext 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@multiplatform.one/vite-plugin-webext 还在维护吗?

最新版本 7.82.2 发布于 2026/11/02(1 天前);至今共发布 135 个版本,其中过去 12 个月发布了 135 个;首次发布于 2026/08/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。