From 69b44c5d6d31de57f7ac1f289044db2cca688919 Mon Sep 17 00:00:00 2001 From: alvkN <78173939+alvkN@users.noreply.github.com> Date: Tue, 19 Nov 2024 01:12:13 +0300 Subject: [PATCH] fix: onBundleReady called before manifest is generated in build mode (#221) --- .../src/plugins/manifest-loader-plugin.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) 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 1c1d0341..6f1c507f 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 @@ -153,12 +153,14 @@ export function manifestLoaderPlugin(options: ResolvedOptions): vite.Plugin { }); } - if (options.onBundleReady) { + await copyPublicDirToOutDir({ mode, paths }); + + // Handle the onBundleReady callback in dev mode here, as writeBundle is not called in dev mode + if (mode === BuildMode.DEV && options.onBundleReady) { + logger.verbose("Running onBundleReady"); await options.onBundleReady(); } - await copyPublicDirToOutDir({ mode, paths }); - // In dev mode, open up the browser immediately after the build context is finished with the // first build. if (mode === BuildMode.DEV && !options.disableAutoLaunch) { @@ -287,6 +289,14 @@ export function manifestLoaderPlugin(options: ResolvedOptions): vite.Plugin { noInput.cleanupBundle(bundle); }, + // Runs during: build, watch + async writeBundle() { + if (options.onBundleReady) { + logger.verbose("Running onBundleReady"); + await options.onBundleReady(); + } + }, + // Runs during: watch, dev async watchChange(id) { if (