Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement callback that is called after parent build is completed #219

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/config/plugin-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,11 @@ 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.

## `onBundleReady`

```ts
onBundleReady?: () => void | Promise<void>
```

This callback is invoked after the main (parent) build completes, but not for child builds triggered by different entry points. Use it to perform additional actions when the main extension bundle is ready in both development (serve) and production (build) modes.
1 change: 1 addition & 0 deletions packages/vite-plugin-web-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default function webExtension(
transformManifest: options.transformManifest,
webExtConfig: options.webExtConfig,
bundleInfoJsonPath: options.bundleInfoJsonPath,
onBundleReady: options.onBundleReady,
verbose: process.argv.includes("-d") || process.argv.includes("--debug"),
disableColors:
process.env.CI === "true" || process.env.DISABLE_COLORS === "true", // TODO: document env var
Expand Down
6 changes: 6 additions & 0 deletions packages/vite-plugin-web-extension/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ export interface UserOptions {
* Output path to a JSON file containing information about the generated bundles.
*/
bundleInfoJsonPath?: string;

/**
* Action to be executed after build ends.
*/
onBundleReady?: () => void | Promise<void>;
}

/**
Expand All @@ -115,6 +120,7 @@ export interface ResolvedOptions {
disableColors: boolean;
webExtConfig?: any;
bundleInfoJsonPath?: string;
onBundleReady?: () => void | Promise<void>;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ export function manifestLoaderPlugin(options: ResolvedOptions): vite.Plugin {
});
}

if (options.onBundleReady) {
await options.onBundleReady();
}

await copyPublicDirToOutDir({ mode, paths });

// In dev mode, open up the browser immediately after the build context is finished with the
Expand Down
Loading