diff --git a/crates/binding/src/js_hook.rs b/crates/binding/src/js_hook.rs index aa9d950f6..5b8bbfa09 100644 --- a/crates/binding/src/js_hook.rs +++ b/crates/binding/src/js_hook.rs @@ -57,6 +57,8 @@ pub struct JsHooks { }; }) => void"#)] pub generate_end: Option, + #[napi(ts_type = "() => Promise;")] + pub write_bundle: Option, #[napi(ts_type = "(path: string, content: Buffer) => Promise;")] pub _on_generate_file: Option, #[napi(ts_type = "() => Promise;")] @@ -78,6 +80,7 @@ pub struct JsHooks { pub struct TsFnHooks { pub build_start: Option>, pub build_end: Option>, + pub write_bundle: Option>, pub generate_end: Option>, pub load: Option>>, pub load_include: Option>>, @@ -97,6 +100,9 @@ impl TsFnHooks { build_end: hooks.build_end.as_ref().map(|hook| unsafe { ThreadsafeFunction::from_napi_value(env.raw(), hook.raw()).unwrap() }), + write_bundle: hooks.write_bundle.as_ref().map(|hook| unsafe { + ThreadsafeFunction::from_napi_value(env.raw(), hook.raw()).unwrap() + }), generate_end: hooks.generate_end.as_ref().map(|hook| unsafe { ThreadsafeFunction::from_napi_value(env.raw(), hook.raw()).unwrap() }), diff --git a/crates/binding/src/js_plugin.rs b/crates/binding/src/js_plugin.rs index 3277ea350..b861b68f4 100644 --- a/crates/binding/src/js_plugin.rs +++ b/crates/binding/src/js_plugin.rs @@ -121,6 +121,13 @@ impl Plugin for JsPlugin { Ok(()) } + fn write_bundle(&self, _context: &Arc) -> Result<()> { + if let Some(hook) = &self.hooks.write_bundle { + hook.call(())? + } + Ok(()) + } + fn before_write_fs(&self, path: &std::path::Path, content: &[u8]) -> Result<()> { if let Some(hook) = &self.hooks._on_generate_file { hook.call(WriteFile { diff --git a/crates/mako/src/compiler.rs b/crates/mako/src/compiler.rs index 7a8203861..121cf18b6 100644 --- a/crates/mako/src/compiler.rs +++ b/crates/mako/src/compiler.rs @@ -460,6 +460,7 @@ impl Compiler { self.context .plugin_driver .generate_end(¶ms, &self.context)?; + self.context.plugin_driver.write_bundle(&self.context)?; Ok(()) } Err(e) => Err(e), diff --git a/crates/mako/src/dev.rs b/crates/mako/src/dev.rs index 4d0e6c33a..e2a9044cf 100644 --- a/crates/mako/src/dev.rs +++ b/crates/mako/src/dev.rs @@ -402,6 +402,11 @@ impl DevServer { .plugin_driver .generate_end(¶ms, &compiler.context) .unwrap(); + compiler + .context + .plugin_driver + .write_bundle(&compiler.context) + .unwrap(); } let receiver_count = txws.receiver_count(); diff --git a/crates/mako/src/plugin.rs b/crates/mako/src/plugin.rs index 636da354b..bdc4ff793 100644 --- a/crates/mako/src/plugin.rs +++ b/crates/mako/src/plugin.rs @@ -147,6 +147,10 @@ pub trait Plugin: Any + Send + Sync { Ok(()) } + fn write_bundle(&self, _context: &Arc) -> Result<()> { + Ok(()) + } + fn runtime_plugins(&self, _context: &Arc) -> Result> { Ok(Vec::new()) } @@ -324,6 +328,13 @@ impl PluginDriver { Ok(()) } + pub fn write_bundle(&self, context: &Arc) -> Result<()> { + for plugin in &self.plugins { + plugin.write_bundle(context)?; + } + Ok(()) + } + pub fn generate_begin(&self, context: &Arc) -> Result<()> { for plugin in &self.plugins { plugin.generate_begin(context)?; diff --git a/docs/config.md b/docs/config.md index db691f897..0cdffc366 100644 --- a/docs/config.md +++ b/docs/config.md @@ -573,6 +573,7 @@ Specify the plugins to use. ... }; }) => void; + writeBundle?: () => void; load?: (filePath: string) => Promise<{ content: string, type: 'css'|'js'|'jsx'|'ts'|'tsx' }>; loadInclude?: (filePath: string) => boolean; resolveId?: (id: string, importer: string, { isEntry: bool }) => Promise<{ id: string, external: bool }>; diff --git a/docs/config.zh-CN.md b/docs/config.zh-CN.md index 4eb649a78..08d9868b1 100644 --- a/docs/config.zh-CN.md +++ b/docs/config.zh-CN.md @@ -571,6 +571,7 @@ import(/* webpackIgnore: true */ "./foo"); ... }; }) => void; + writeBundle?: () => void; load?: (filePath: string) => Promise<{ content: string, type: 'css'|'js'|'jsx'|'ts'|'tsx' }>; loadInclude?: (filePath: string) => boolean; resolveId?: (id: string, importer: string, { isEntry: bool }) => Promise<{ id: string, external: bool }>; diff --git a/e2e/fixtures/plugins/plugins.config.js b/e2e/fixtures/plugins/plugins.config.js index c8d91d5bc..ccd9928f8 100644 --- a/e2e/fixtures/plugins/plugins.config.js +++ b/e2e/fixtures/plugins/plugins.config.js @@ -8,6 +8,9 @@ module.exports = [ }; } }, + async writeBundle() { + console.log("writeBundle"); + }, }, { async loadInclude(path) { diff --git a/packages/mako/binding.d.ts b/packages/mako/binding.d.ts index b0101025d..a15d2ff1c 100644 --- a/packages/mako/binding.d.ts +++ b/packages/mako/binding.d.ts @@ -51,6 +51,7 @@ export interface JsHooks { endTime: number; }; }) => void; + writeBundle?: () => Promise; onGenerateFile?: (path: string, content: Buffer) => Promise; buildStart?: () => Promise; buildEnd?: () => Promise;