vite-plugin-web-extension

repository·main·Indexed 21 days ago

https://github.com/aklinker1/vite-plugin-web-extension

A Vite plugin that simplifies the development and building of web extensions by automating manifest-based builds and providing fast development modes with automatic browser installation. It supports all major frontend frameworks and browsers. Note: This project is in maintenance mode and is being deprecated in favor of WXT.

Tokens
13.6K
Snippets
55
Records
70
Agent score
74%

What's inside vite-plugin-web-extension

  1. Overview of Vite Plugin Web Extension features

    main

    Vite Plugin Web Extension is a tool designed for modern web extension development using Vite. Key capabilities include:

    • Manifest-driven builds: Automatically discovers and builds all files listed in your manifest.json.
    • Fast development workflow: Automatically launches Chrome or Firefox with the extension installed. It supports Hot Module Replacement (HMR) for UI components and a watch mode for content scripts.
    • Framework agnostic: You can use any frontend framework's Vite plugin (e.g., React, Vue) anywhere in your extension, including within content scripts.
    • Cross-browser support: Allows customization of manifest.json to build for multiple browsers.
    • Manifest validation: Validates that all required fields are present in your manifest.json during the build process.
    • TypeScript support: Provides out-of-the-box TypeScript support without extra configuration.
  2. Configure browser startup using config files

    main

    The plugin automatically discovers and merges configuration files. If multiple files are found, they are merged in order of priority (higher priority files override lower priority ones).

    Important: Array fields are overwritten, not combined.

    Configuration Priority (Highest to Lowest)

    1. webExtConfig option in vite.config.ts
    2. <viteRoot>/.webextrc
    3. <viteRoot>/.webextrc.(json|json5|yml|yaml)
    4. <cwd>/.webextrc
    5. <cwd>/.webextrc.(json|json5|yml|yaml)
    6. ~/.webextrc
    7. ~/.webextrc.(json|json5|yml|yaml)

    Using a file in your home directory (~/.webextrc) allows you to set global preferences (like specific browser binaries) for all your projects.

    // ~/.webextrc
    {
      "chromiumBinary": "/Applications/Google Chrome Beta.app/Contents/MacOS/Google Chrome Beta",
      "firefox": "firefoxdeveloperedition"
    }
  3. Standardize browser APIs with webextension-polyfill

    main

    For standardizing the behavior of multiple browsers at runtime (e.g., using browser.* instead of chrome.*), use the webextension-polyfill package. Simply import it wherever you need to use extension APIs.

    // Works on Chrome, Edge, Firefox, Safari... every browser
    import browser from "webextension-polyfill";
    
    browser.runtime.getURL("/popup.html");
  4. Use Manifest Templates for Multibrowser Support

    main

    You can create a single manifest file that contains browser-specific fields by using the {{browser}}. prefix. This is useful for handling differences between Manifest V3 (required by Chrome) and Manifest V2 (supported by Firefox).

    To activate a specific flavor, set the browser option in the webExtension plugin configuration. You can control this via an environment variable during the build process.

    ```json
    {
      "{{chrome}}.manifest_version": 3,
      "{{firefox}}.manifest_version": 2,
      "name": "Example",
      "version": "1.0.0",
      "{{chrome}}.action": {
        "default_popup": "popup/index.html"
      },
      "{{firefox}}.browser_action": {
        "default_popup": "popup/index.html"
      }
    }
    // vite.config.ts
    import defineConfig from "vite";
    import webExtension from "vite-plugin-web-extension";
    
    export default defineConfig({
      plugins: [
        webExtension({
          browser: process.env.TARGET || "chrome",
        }),
      ],
    });

    Build commands:

    • TARGET=chrome vite build
    • TARGET=firefox vite build (Use cross-env TARGET=firefox vite build on Windows)
  5. Use separate manifest files for each browser

    main

    If you prefer to maintain completely distinct manifest files for different browsers (e.g., manifest.chrome.json and manifest.firefox.json), you can use a conditional expression within the manifest option of the webExtension plugin.

    import defineConfig from "vite";
    import webExtension from "vite-plugin-web-extension";
    
    const target = process.env.TARGET || "chrome";
    
    export default defineConfig({
      plugins: [
        webExtension({
          manifest: target == "chrome" ? "manifest.chrome.json" : "manifest.firefox.json",
        }),
      ],
    });
  6. Upgrade from v2 to v3

    main

    Upgrading from version 2 to version 3 primarily requires updating your project to use Vite 4.

    To perform the upgrade:

    1. Install the latest version of Vite using your package manager.
    2. Ensure all other Vite plugins in your project are updated to versions compatible with Vite 4.

    Note for v1 users: If you are migrating from version 1, you must first complete the migration steps for version 2 before proceeding to version 3.

    pnpm i vite@latest
  7. Integrate React (SWC) into your extension

    main

    If you prefer using the SWC-based React plugin for faster builds, install @vitejs/plugin-react-swc and add it to your vite.config.ts.

    // vite.config.ts
    import { defineConfig } from "vite";
    import webExtension from "vite-plugin-web-extension";
    import react from "@vitejs/plugin-react-swc";
    
    export default defineConfig({
      plugins: [
        react(),
        webExtension({
          // ...
        }),
      ],
    });
  8. Add or fix templates in the Starter Kit

    main

    Templates are consumed by the create command by cloning the main branch of the repository. To add or fix a template, you do not need to publish to NPM; simply merge your changes into the main branch.

    How templates are processed:

    1. The tool clones the repo.
    2. It retrieves a list of templates from templates/templates.json via the raw GitHub URL.
    3. It copies the templates/shared folder into the target project folder.
    4. It copies the specific templates/<template-name> folder into the project folder, overwriting any files from the shared folder.

    To add a new template:

    Create a new folder inside the templates/ directory named after your template and place the template files inside it. Ensure the template results in a functional extension similar to existing examples.