From 4650c1098c1a6d0c146f1a11e763966e6fc9d2e9 Mon Sep 17 00:00:00 2001 From: Bent Date: Thu, 12 Sep 2024 23:26:33 +0200 Subject: [PATCH] Added bundleInfoJsonPath plugin option --- docs/config/plugin-options.md | 8 ++++++++ packages/vite-plugin-web-extension/src/index.ts | 1 + packages/vite-plugin-web-extension/src/options.ts | 6 ++++++ .../src/plugins/manifest-loader-plugin.ts | 8 ++++++++ 4 files changed, 23 insertions(+) diff --git a/docs/config/plugin-options.md b/docs/config/plugin-options.md index b4ef7ee2..63817815 100644 --- a/docs/config/plugin-options.md +++ b/docs/config/plugin-options.md @@ -160,3 +160,11 @@ webExtConfig?: any ``` This option allows you to pass configuration into `web-ext` when launching the browser. For more details, refer to [Browser Startup Configuration](/guide/configure-browser-startup.md). + +## `bundleInfoJsonPath` + +```ts +bundleInfoJsonPath?: string +``` + +If set, the plugin will write a JSON file containing information about the built bundles to the specified path in the output directory. This can for example be useful for dynamically injecting content scripts/styles from background scripts. diff --git a/packages/vite-plugin-web-extension/src/index.ts b/packages/vite-plugin-web-extension/src/index.ts index 3a7bfd91..14cb9d1c 100644 --- a/packages/vite-plugin-web-extension/src/index.ts +++ b/packages/vite-plugin-web-extension/src/index.ts @@ -26,6 +26,7 @@ export default function webExtension( scriptViteConfig: options.scriptViteConfig, transformManifest: options.transformManifest, webExtConfig: options.webExtConfig, + bundleInfoJsonPath: options.bundleInfoJsonPath, verbose: process.argv.includes("-d") || process.argv.includes("--debug"), disableColors: process.env.CI === "true" || process.env.DISABLE_COLORS === "true", // TODO: document env var diff --git a/packages/vite-plugin-web-extension/src/options.ts b/packages/vite-plugin-web-extension/src/options.ts index 6890836b..42f5bb7f 100644 --- a/packages/vite-plugin-web-extension/src/options.ts +++ b/packages/vite-plugin-web-extension/src/options.ts @@ -86,6 +86,11 @@ export interface UserOptions { * . */ webExtConfig?: any; + + /** + * Output path to a JSON file containing information about the generated bundles. + */ + bundleInfoJsonPath?: string; } /** @@ -109,6 +114,7 @@ export interface ResolvedOptions { verbose: boolean; disableColors: boolean; webExtConfig?: any; + bundleInfoJsonPath?: string; } /** diff --git a/packages/vite-plugin-web-extension/src/plugins/manifest-loader-plugin.ts b/packages/vite-plugin-web-extension/src/plugins/manifest-loader-plugin.ts index dc79e0dc..13eadd68 100644 --- a/packages/vite-plugin-web-extension/src/plugins/manifest-loader-plugin.ts +++ b/packages/vite-plugin-web-extension/src/plugins/manifest-loader-plugin.ts @@ -145,6 +145,14 @@ export function manifestLoaderPlugin(options: ResolvedOptions): vite.Plugin { name: "manifest.json", }); + if (options.bundleInfoJsonPath) { + emitFile({ + type: "asset", + source: JSON.stringify(ctx.getBundles()), + fileName: options.bundleInfoJsonPath, + }); + } + await copyPublicDirToOutDir({ mode, paths }); // In dev mode, open up the browser immediately after the build context is finished with the