samrum / vite-plugin-web-extension

A vite plugin for generating cross browser platform, ES module based web extensions.
MIT License
323 stars 33 forks source link
vite vite-plugin web-extension web-extensions

@samrum/vite-plugin-web-extension

npm version node compatibility ci

Generate cross browser platform, ES module based web extensions.

Quick Start

Create a new Vite web extension project

npm init @samrum/vite-plugin-web-extension@latest

Supports choice of Manifest version, TypeScript support, and framework (Preact, React, Solid, Svelte, Vanilla, Vue).

Check the README of the generated extension for usage information.

Usage

Requires Vite 3+

npm install @samrum/vite-plugin-web-extension

Examples

Manifest V2 vite.config.js: ```js import { defineConfig } from "vite"; import webExtension from "@samrum/vite-plugin-web-extension"; export default defineConfig({ plugins: [ webExtension({ manifest: { name: pkg.name, description: pkg.description, version: pkg.version, manifest_version: 2, background: { scripts: ["src/background/script.js"], }, }, }), ], }); ```
Manifest V3 vite.config.js: ```js import { defineConfig } from "vite"; import webExtension from "@samrum/vite-plugin-web-extension"; export default defineConfig({ plugins: [ webExtension({ manifest: { name: pkg.name, description: pkg.description, version: pkg.version, manifest_version: 3, background: { service_worker: "src/background/serviceWorker.js", }, }, }), ], }); ```
Firefox Experimental Manifest V3 There are two configurations an extension needs to make for experimental manifest V3 support: 1. Background service workers are not supported, so you are required to use a background script. 2. The `use_dynamic_url` property is not supported for web accessible resources. In the plugin options, set `useDynamicUrlWebAccessibleResources` to false: ```js webExtension({ ... useDynamicUrlWebAccessibleResources: false, }), ```
Devtools To add content to the browser dev tools, add `devtools_page` to your manifest ```js devtools_page: "src/entries/devtools/index.html", ``` Place a script `devtools.js` in `public` dir. ```js var _browser; if (chrome) { _browser = chrome; } else { _browser = browser; } _browser.devtools.panels.create( "My Panel", // title "images/icon-16.png", // icon "src/entries/devtools/index.html" // content ); ``` Then load the script from your devtools html which placed in `src/entries/devtools/index.html`. ```html Devtools
```

Options

manifest

useDynamicUrlWebAccessibleResources (optional)

optimizeWebAccessibleResources (optional)

additionalInputs (optional)

Content Scripts

TypeScript

In an env.d.ts file, add the following type reference to define the plugin specific import.meta variables as well as plugin client functions:

/// <reference types="@samrum/vite-plugin-web-extension/client" />

Browser Support

The following requirements must be met by the browser:

A sample of supported browsers:

Manifest V2 Manifest V3
Chromium 64 91
Firefox 89 N/A (In development)

The plugin will automatically default vite's build.target config option to these minimum browser versions if not already defined by the user.

For dev mode support in Manifest V3, Chromium version must be at least 110.

How it works

The plugin will take the provided manifest, generate rollup input scripts for supported manifest properties, then output an ES module based web extension.

This includes:

Why this is a Vite specific plugin

The plugin relies on Vite to parse and handle html files in addition to relying on Vite's manifest generation in order to map generated files to the eventual extension manifest.

Development

This project uses pnpm for package management.

Lint

pnpm lint

Tests

pnpm test

Build

pnpm build