Skip to content

Commit

Permalink
feat: add writeBundle plugin hook
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Oct 21, 2024
1 parent c8fb311 commit 1e545e2
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/binding/src/js_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub struct JsHooks {
};
}) => void"#)]
pub generate_end: Option<JsFunction>,
#[napi(ts_type = "() => Promise<void>;")]
pub write_bundle: Option<JsFunction>,
#[napi(ts_type = "(path: string, content: Buffer) => Promise<void>;")]
pub _on_generate_file: Option<JsFunction>,
#[napi(ts_type = "() => Promise<void>;")]
Expand All @@ -78,6 +80,7 @@ pub struct JsHooks {
pub struct TsFnHooks {
pub build_start: Option<ThreadsafeFunction<(), ()>>,
pub build_end: Option<ThreadsafeFunction<(), ()>>,
pub write_bundle: Option<ThreadsafeFunction<(), ()>>,
pub generate_end: Option<ThreadsafeFunction<Value, ()>>,
pub load: Option<ThreadsafeFunction<String, Option<LoadResult>>>,
pub load_include: Option<ThreadsafeFunction<String, Option<bool>>>,
Expand All @@ -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()
}),
Expand Down
7 changes: 7 additions & 0 deletions crates/binding/src/js_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ impl Plugin for JsPlugin {
Ok(())
}

fn write_bundle(&self, _context: &Arc<Context>) -> 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 {
Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ impl Compiler {
self.context
.plugin_driver
.generate_end(&params, &self.context)?;
self.context.plugin_driver.write_bundle(&self.context)?;
Ok(())
}
Err(e) => Err(e),
Expand Down
5 changes: 5 additions & 0 deletions crates/mako/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,11 @@ impl DevServer {
.plugin_driver
.generate_end(&params, &compiler.context)
.unwrap();
compiler

Check warning on line 405 in crates/mako/src/dev.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/dev.rs#L405

Added line #L405 was not covered by tests
.context
.plugin_driver
.write_bundle(&compiler.context)

Check warning on line 408 in crates/mako/src/dev.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/dev.rs#L408

Added line #L408 was not covered by tests
.unwrap();
}

let receiver_count = txws.receiver_count();
Expand Down
11 changes: 11 additions & 0 deletions crates/mako/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ pub trait Plugin: Any + Send + Sync {
Ok(())
}

fn write_bundle(&self, _context: &Arc<Context>) -> Result<()> {
Ok(())
}

fn runtime_plugins(&self, _context: &Arc<Context>) -> Result<Vec<String>> {
Ok(Vec::new())
}
Expand Down Expand Up @@ -324,6 +328,13 @@ impl PluginDriver {
Ok(())
}

pub fn write_bundle(&self, context: &Arc<Context>) -> Result<()> {
for plugin in &self.plugins {
plugin.write_bundle(context)?;
}
Ok(())
}

pub fn generate_begin(&self, context: &Arc<Context>) -> Result<()> {
for plugin in &self.plugins {
plugin.generate_begin(context)?;
Expand Down
1 change: 1 addition & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }>;
Expand Down
1 change: 1 addition & 0 deletions docs/config.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }>;
Expand Down
3 changes: 3 additions & 0 deletions e2e/fixtures/plugins/plugins.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module.exports = [
};
}
},
async writeBundle() {
console.log("writeBundle");
},
},
{
async loadInclude(path) {
Expand Down
1 change: 1 addition & 0 deletions packages/mako/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export interface JsHooks {
endTime: number;
};
}) => void;
writeBundle?: () => Promise<void>;
onGenerateFile?: (path: string, content: Buffer) => Promise<void>;
buildStart?: () => Promise<void>;
buildEnd?: () => Promise<void>;
Expand Down

0 comments on commit 1e545e2

Please sign in to comment.