Gathering detailed insights and metrics for vite-plugin-web-extension-support-inject
Gathering detailed insights and metrics for vite-plugin-web-extension-support-inject
Gathering detailed insights and metrics for vite-plugin-web-extension-support-inject
Gathering detailed insights and metrics for vite-plugin-web-extension-support-inject
A vite plugin for generating cross browser platform, ES module based web extensions.
npm install vite-plugin-web-extension-support-inject
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (91.17%)
JavaScript (4.16%)
HTML (3.7%)
CSS (0.86%)
Shell (0.06%)
SCSS (0.05%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
349 Stars
128 Commits
30 Forks
7 Watchers
2 Branches
10 Contributors
Updated on Jul 01, 2025
Latest Version
5.0.0
Package Id
vite-plugin-web-extension-support-inject@5.0.0
Unpacked Size
103.11 kB
Size
22.04 kB
File Count
8
NPM Version
9.5.1
Node Version
18.16.0
Published on
Aug 30, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Generate cross browser platform, ES module based web extensions.
Create a new Vite web extension project
1npm init @samrum/vite-plugin-web-extension@latest
Supports choice of Manifest version, TypeScript support, and framework (Vanilla, Vue, React, Preact, Svelte).
Check the README of the generated extension for usage information.
Requires Vite 3+
1npm install @samrum/vite-plugin-web-extension
vite.config.js:
1import { defineConfig } from "vite"; 2import webExtension from "@samrum/vite-plugin-web-extension"; 3 4export default defineConfig({ 5 plugins: [ 6 webExtension({ 7 manifest: { 8 name: pkg.name, 9 description: pkg.description, 10 version: pkg.version, 11 manifest_version: 2, 12 background: { 13 scripts: ["src/background/script.js"], 14 }, 15 }, 16 }), 17 ], 18});
vite.config.js:
1import { defineConfig } from "vite"; 2import webExtension from "@samrum/vite-plugin-web-extension"; 3 4export default defineConfig({ 5 plugins: [ 6 webExtension({ 7 manifest: { 8 name: pkg.name, 9 description: pkg.description, 10 version: pkg.version, 11 manifest_version: 3, 12 background: { 13 service_worker: "src/background/serviceWorker.js", 14 }, 15 }, 16 }), 17 ], 18});
Background service workers are not supported, so you are required to use a background script.
The use_dynamic_url
property is not supported for web accessible resources. In the plugin options, set useDynamicUrlWebAccessibleResources
to false:
1 webExtension({ 2 ... 3 useDynamicUrlWebAccessibleResources: false, 4 }),
1devtools_page: "src/entries/devtools/index.html",
Place a script devtools.js
in public
dir.
1var _browser; 2if (chrome) { 3 _browser = chrome; 4} else { 5 _browser = browser; 6} 7_browser.devtools.panels.create( 8 "My Panel", // title 9 "images/icon-16.png", // icon 10 "src/entries/devtools/index.html" // content 11);
Then load the script from your devtools html which placed in src/entries/devtools/index.html
.
1<!DOCTYPE html> 2<html lang="en"> 3 <head> 4 <meta charset="UTF-8" /> 5 <title>Devtools</title> 6 </head> 7 <body> 8 <div id="app"></div> 9 <script type="module" src="./main.ts"></script> 10 <script src="/devtools.js"></script> 11 </body> 12</html>
manifest
useDynamicUrlWebAccessibleResources (optional)
boolean
true
use_dynamic_url
property to web accessible resources generated by the pluginoptimizeWebAccessibleResources (optional)
boolean
true
resource
properties and dedupe and sort resources
. In Manifest V2, sort web accessible resources.additionalInputs (optional)
Type:
1 type AdditionalInput = 2 | string 3 | { 4 fileName: string; 5 webAccessible?: 6 | boolean 7 | { 8 matches: string[]; 9 extensionIds?: string[]; 10 excludeEntryFile?: boolean; 11 } 12 | { 13 matches?: string[]; 14 extensionIds: string[]; 15 excludeEntryFile?: boolean; 16 }; 17 }; 18 19 additionalInputs?: { 20 scripts?: AdditionalInput[]; 21 html?: AdditionalInput[]; 22 styles?: AdditionalInput[]; 23 };
Additional input files that should be processed and treated as web extension inputs.
Useful for dynamically injected scripts or dynamically opened HTML pages.
The webAccessible option configures whether the input file and its dependencies are included in the manifest web_accessible_resources
property. Defaults to true.
true
, defaults to:
1 { 2 matches: ['<all_urls>'], 3 excludeEntryFile: false, 4 }
excludeEntryFile
option prevents the entry file from being added as a web accessible resource. Defaults to false.Example
1 webExtension({
2 manifest: ...,
3 additionalInputs: {
4 scripts: [
5 'src/entries/webAccessibleScript.js', // defaults to webAccessible: true
6 {
7 fileName: 'src/entries/webAccessibleScript2.js',
8 webAccessible: true,
9 },
10 {
11 fileName: 'src/entries/privateScript.js', // entry file and dependencies are not web accessible
12 webAccessible: false,
13 },
14 {
15 fileName: 'src/entries/entryFileExcluded.js', // entry file is not web accessible and dependencies are
16 webAccessible: {
17 matches: ['<all_urls>'],
18 excludeEntryFile: true,
19 },
20 },
21 ],
22 },
23 })
For HMR style support within shadow DOMs, use the addStyleTarget
function to add the shadowRoot of your element as a style target:
1if (import.meta.hot) { 2 const { addViteStyleTarget } = await import( 3 "@samrum/vite-plugin-web-extension/client" 4 ); 5 6 await addViteStyleTarget(appContainer); 7}
For builds, use the import.meta.PLUGIN_WEB_EXT_CHUNK_CSS_PATHS
variable to reference an array of CSS asset paths associated with the current output chunk.
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:
1/// <reference types="@samrum/vite-plugin-web-extension/client" />
The following requirements must be met by the browser:
import.meta.url
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.
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:
web_accessible_resources
so they are accessible by the wrapper script. Needed because content scripts are not able to be loaded directly as ES modules.
use_dynamic_url
property that will mitigate this. This option is set for manifest V3 web accessible resources generated by this plugin.import.meta.url
usages instead of rewriting to self.location
. Needed so content script static asset handling can function correctly.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.
This project uses pnpm for package management.
1pnpm lint
1pnpm test
1pnpm build
No vulnerabilities found.
No security vulnerabilities found.