Skip to content
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
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
.context
.plugin_driver
.write_bundle(&compiler.context)
.unwrap();
Comment on lines +405 to +409
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

新增的 write_bundle 调用需要错误处理

新增的 write_bundle 调用是一个很好的改进,它与PR的整体目标一致。然而,当前实现缺少错误处理机制。

建议添加错误处理,例如:

compiler
    .context
    .plugin_driver
    .write_bundle(&compiler.context)
    .map_err(|e| {
        debug!("写入 bundle 时发生错误: {:?}", e);
        e
    })?;

这样可以确保在写入过程中发生的任何错误都能被适当地处理和记录。

}

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