{"id":19950869,"url":"https://github.com/samrum/vite-plugin-web-extension","last_synced_at":"2025-04-07T01:12:42.259Z","repository":{"id":37103976,"uuid":"443940388","full_name":"samrum/vite-plugin-web-extension","owner":"samrum","description":"A vite plugin for generating cross browser platform, ES module based web extensions.","archived":false,"fork":false,"pushed_at":"2024-09-26T05:15:40.000Z","size":511,"stargazers_count":344,"open_issues_count":23,"forks_count":32,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-03-30T23:11:57.978Z","etag":null,"topics":["vite","vite-plugin","web-extension","web-extensions"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/samrum.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-01-03T04:31:59.000Z","updated_at":"2025-03-25T06:56:32.000Z","dependencies_parsed_at":"2024-06-18T16:59:11.832Z","dependency_job_id":"99e1b480-a53c-48e9-8611-01449c0f17b8","html_url":"https://github.com/samrum/vite-plugin-web-extension","commit_stats":{"total_commits":128,"total_committers":10,"mean_commits":12.8,"dds":0.078125,"last_synced_commit":"fd56ebb80fe424a968baf64e7ec739b64b6f14bd"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samrum%2Fvite-plugin-web-extension","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samrum%2Fvite-plugin-web-extension/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samrum%2Fvite-plugin-web-extension/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samrum%2Fvite-plugin-web-extension/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/samrum","download_url":"https://codeload.github.com/samrum/vite-plugin-web-extension/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247574089,"owners_count":20960496,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["vite","vite-plugin","web-extension","web-extensions"],"created_at":"2024-11-13T01:05:50.792Z","updated_at":"2025-04-07T01:12:42.232Z","avatar_url":"https://github.com/samrum.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# @samrum/vite-plugin-web-extension\n\n[![npm version](https://img.shields.io/npm/v/@samrum/vite-plugin-web-extension)](https://www.npmjs.com/package/@samrum/vite-plugin-web-extension)\n[![node compatibility](https://img.shields.io/node/v/@samrum/vite-plugin-web-extension)](https://nodejs.org/en/about/releases/)\n[![ci](https://github.com/samrum/vite-plugin-web-extension/actions/workflows/ci.yml/badge.svg)](https://github.com/samrum/vite-plugin-web-extension/actions/workflows/ci.yml)\n\n\u003e Generate cross browser platform, ES module based web extensions.\n\n- Manifest V2 \u0026 V3 Support\n- Completely ES module based extensions\n  - Including in content scripts!\n- Vite based html and static asset handling\n  - Including in content scripts!\n- HMR support\n  - All Manifest entry points\n    - Including content scripts when using a Chromium browser!\n  - CSS styles in content scripts\n    - Including shadow DOM rendered content!\n  - Including Manifest V3 support since Chromium 110!\n\n## Quick Start\n\nCreate a new Vite web extension project\n\n```sh\nnpm init @samrum/vite-plugin-web-extension@latest\n```\n\nSupports choice of Manifest version, TypeScript support, and framework (Preact, React, Solid, Svelte, Vanilla, Vue).\n\nCheck the README of the generated extension for usage information.\n\n## Usage\n\nRequires Vite 3+\n\n```sh\nnpm install @samrum/vite-plugin-web-extension\n```\n\n### Examples\n\n\u003cdetails\u003e\n  \u003csummary\u003eManifest V2\u003c/summary\u003e\n\nvite.config.js:\n\n```js\nimport { defineConfig } from \"vite\";\nimport webExtension from \"@samrum/vite-plugin-web-extension\";\n\nexport default defineConfig({\n  plugins: [\n    webExtension({\n      manifest: {\n        name: pkg.name,\n        description: pkg.description,\n        version: pkg.version,\n        manifest_version: 2,\n        background: {\n          scripts: [\"src/background/script.js\"],\n        },\n      },\n    }),\n  ],\n});\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eManifest V3\u003c/summary\u003e\n\nvite.config.js:\n\n```js\nimport { defineConfig } from \"vite\";\nimport webExtension from \"@samrum/vite-plugin-web-extension\";\n\nexport default defineConfig({\n  plugins: [\n    webExtension({\n      manifest: {\n        name: pkg.name,\n        description: pkg.description,\n        version: pkg.version,\n        manifest_version: 3,\n        background: {\n          service_worker: \"src/background/serviceWorker.js\",\n        },\n      },\n    }),\n  ],\n});\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eFirefox Experimental Manifest V3 \u003c/summary\u003e\n  There are two configurations an extension needs to make for experimental manifest V3 support:\n  \n  1. Background service workers are not supported, so you are required to use a background script.\n  2. The `use_dynamic_url` property is not supported for web accessible resources. In the plugin options, set `useDynamicUrlWebAccessibleResources` to false:\n\n      ```js\n        webExtension({\n          ...\n          useDynamicUrlWebAccessibleResources: false,\n        }),\n      ```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eDevtools\u003c/summary\u003e\n  To add content to the browser dev tools, add `devtools_page` to your manifest\n\n```js\ndevtools_page: \"src/entries/devtools/index.html\",\n```\n\nPlace a script `devtools.js` in `public` dir.\n\n```js\nvar _browser;\nif (chrome) {\n  _browser = chrome;\n} else {\n  _browser = browser;\n}\n_browser.devtools.panels.create(\n  \"My Panel\", // title\n  \"images/icon-16.png\", // icon\n  \"src/entries/devtools/index.html\" // content\n);\n```\n\nThen load the script from your devtools html which placed in `src/entries/devtools/index.html`.\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\"\u003e\n  \u003chead\u003e\n    \u003cmeta charset=\"UTF-8\" /\u003e\n    \u003ctitle\u003eDevtools\u003c/title\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    \u003cdiv id=\"app\"\u003e\u003c/div\u003e\n    \u003cscript type=\"module\" src=\"./main.ts\"\u003e\u003c/script\u003e\n    \u003cscript src=\"/devtools.js\"\u003e\u003c/script\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n\u003c/details\u003e\n\n### Options\n\nmanifest\n\n- The [manifest](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json) definition for your extension\n- All manifest property file names should be relative to the root of the project.\n\nuseDynamicUrlWebAccessibleResources (optional)\n\n- Type: `boolean`\n- Default: `false`\n- Adds the `use_dynamic_url` property to web accessible resources generated by the plugin\n  - Caution: In Chrome 130, with this option enabled, importing a generated resource using `chrome.runtime.getURL` will not work ([Chromium Issue](https://issues.chromium.org/issues/363027634))l\n\noptimizeWebAccessibleResources (optional)\n\n- Type: `boolean`\n- Default: `true`\n- On build, in Manifest V3, merge web accessible resource definitions that have matching non-`resource` properties and dedupe and sort `resources`. In Manifest V2, sort web accessible resources.\n\nadditionalInputs (optional)\n\n- Type:\n\n  ```ts\n    type AdditionalInput =\n      | string\n      | {\n          fileName: string;\n          webAccessible?:\n            | boolean\n            | {\n                matches: string[];\n                extensionIds?: string[];\n                excludeEntryFile?: boolean;\n              }\n            | {\n                matches?: string[];\n                extensionIds: string[];\n                excludeEntryFile?: boolean;\n              };\n        };\n\n    additionalInputs?: {\n      scripts?: AdditionalInput[];\n      html?: AdditionalInput[];\n      styles?: AdditionalInput[];\n    };\n  ```\n\n- Additional input files that should be processed and treated as web extension inputs.\n- Useful for dynamically injected scripts or dynamically opened HTML pages.\n- The webAccessible option configures whether the input file and its dependencies are included in the manifest `web_accessible_resources` property. Defaults to true.\n  - When set to `true`, defaults to:\n    ```ts\n      {\n        matches: ['\u003call_urls\u003e'],\n        excludeEntryFile: false,\n      }\n    ```\n  - The `excludeEntryFile` option prevents the entry file from being added as a web accessible resource. Defaults to false.\n- Example\n  ```ts\n    webExtension({\n      manifest: ...,\n      additionalInputs: {\n        scripts: [\n          'src/entries/webAccessibleScript.js', // defaults to webAccessible: true\n          {\n            fileName: 'src/entries/webAccessibleScript2.js',\n            webAccessible: true,\n          },\n          {\n            fileName: 'src/entries/privateScript.js', // entry file and dependencies are not web accessible\n            webAccessible: false,\n          },\n          {\n            fileName: 'src/entries/entryFileExcluded.js', // entry file is not web accessible and dependencies are\n            webAccessible: {\n              matches: ['\u003call_urls\u003e'],\n              excludeEntryFile: true,\n            },\n          },\n        ],\n      },\n    })\n  ```\n\n### Content Scripts\n\n- For HMR style support within shadow DOMs, use the `addStyleTarget` function to add the shadowRoot of your element as a style target:\n\n  ```js\n  if (import.meta.hot) {\n    const { addViteStyleTarget } = await import(\n      \"@samrum/vite-plugin-web-extension/client\"\n    );\n\n    await addViteStyleTarget(appContainer);\n  }\n  ```\n\n- 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.\n\n### TypeScript\n\nIn an [env.d.ts file](https://vitejs.dev/guide/env-and-mode.html#intellisense-for-typescript), add the following type reference to define the plugin specific `import.meta` variables as well as plugin client functions:\n\n```ts\n/// \u003creference types=\"@samrum/vite-plugin-web-extension/client\" /\u003e\n```\n\n### Browser Support\n\nThe following requirements must be met by the browser:\n\n- Must support dynamic module imports made by web extension content scripts.\n- Must support `import.meta.url`\n\nA sample of supported browsers:\n\n|          | Manifest V2 | Manifest V3                                                                            |\n| -------- | ----------- | -------------------------------------------------------------------------------------- |\n| Chromium | 64          | 91                                                                                     |\n| Firefox  | 89          | N/A ([In development](https://blog.mozilla.org/addons/2021/05/27/manifest-v3-update/)) |\n\nThe plugin will automatically default vite's `build.target` config option to these minimum browser versions if not already defined by the user.\n\nFor dev mode support in Manifest V3, Chromium version must be at least 110.\n\n## How it works\n\nThe plugin will take the provided manifest, generate rollup input scripts for supported manifest properties, then output an ES module based web extension.\n\nThis includes:\n\n- Generating and using a dynamic import wrapper script in place of original content scripts. Then, moving the original scripts to `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.\n  - This may expose your extension to fingerprinting by other extensions or websites. Manifest V3 supports a [`use_dynamic_url` property](https://developer.chrome.com/docs/extensions/mv3/manifest/web_accessible_resources/#:~:text=access%20the%20resources.-,use_dynamic_url,-If%20true%2C%20only) that will mitigate this. This option is set for manifest V3 web accessible resources generated by this plugin.\n- Modifying Vite's static asset handling to maintain `import.meta.url` usages instead of rewriting to `self.location`. Needed so content script static asset handling can function correctly.\n- Modifying Vite's HMR client to add support for targeting specific elements as style injection locations. Needed to support HMR styles in shadow DOM rendered content.\n\n### Why this is a Vite specific plugin\n\nThe 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.\n\n## Development\n\nThis project uses [pnpm](https://pnpm.io/) for package management.\n\n### Lint\n\n```sh\npnpm lint\n```\n\n### Tests\n\n```sh\npnpm test\n```\n\n### Build\n\n```sh\npnpm build\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamrum%2Fvite-plugin-web-extension","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamrum%2Fvite-plugin-web-extension","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamrum%2Fvite-plugin-web-extension/lists"}