Referencing
This article corresponds to the Pull Request:
Our custom JS plugin
myPluginwill be passed from rs-webpack-cli to rs-webpack-core. In rs-webpack-core, it uses a library called@rspack/lite-tapable, developed by the rspack team, to create abeforeRunHook:
CODEexport class Compiler {
bindingRsWebpack: BindingRsWebpack
hooks: {
beforeRun: liteTapable.SyncHook<[string]>;
}
...
}
Similar to Rust, when the
Compileris initialized, it iterates through all the plugins and executes theirapplymethods:
CODEconstructor(props: RawConfig) {
const {plugins} = props
plugins.forEach(plugin => {
plugin.apply(this)
})
}
Then, through a series of operations, it wraps a function called
register_before_run_tapsand passes it to Rust.register_before_run_tapswraps the call to thebeforeRunHook'scallfunction:
CODEthis.registers = {
registerBeforeRunTaps: this.#createHookRegisterTaps(
RegisterJsTapKind.BeforeRun,
() => this.hooks.beforeRun,
queried => (native: string) => {
// beforeRun.call
queried.call(native);
}
),
}
this.bindingRsWebpack = new BindingRsWebpack(props, this.registers)
After this function is executed, it returns an array, and each element in the array can serve as an interceptor for the
before_runHook in Rust (only thecallmethod is implemented):
CODE#[async_trait]
impl Interceptor<BeforeRunHook> for RegisterBeforeRunTaps {
async fn call(
&self,
hook: &BeforeRunHook,
) -> rswebpack_error::Result<Vec<<BeforeRunHook as Hook>::Tap>> {
if let Some(non_skippable_registers) = &self.inner.non_skippable_registers {
if !non_skippable_registers.is_non_skippable(&RegisterJsTapKind::BeforeRun) {
return Ok(Vec::new());
}
}
let js_taps = self.inner.call_register(hook).await?;
let js_taps = js_taps
.iter()
.map(|t| Box::new(BeforeRunTap::new(t.clone())) as <BeforeRunHook as Hook>::Tap)
.collect();
Ok(js_taps)
}
}
In rswebpack_binding, these interceptors are applied through the
JsHooksAdapterPlugin:
CODEimpl Plugin for JsHooksAdapterPlugin {
fn name(&self) -> &'static str {
"rspack.JsHooksAdapterPlugin"
}
fn apply(&self, _ctx: PluginContext<&mut ApplyContext>) -> rswebpack_error::Result<()> {
_ctx
.context
.compiler_hooks
.before_run
.intercept(self.register_before_run_taps.clone());
}
}
PS: The
callfunction in the interceptor is executed each time thecallfunction of the Hook is invoked. For example, in the following example:
CODEconst hook = new SyncHook(['arg1', 'arg2'])
hook.tap('test', (...args) => {
console.log('test', ...args)
})
hook.intercept({
// trigger when execute hook.call
call: (...args) => {
console.log('Execute interceptor call', ...args)
},
})
hook.call('a1', 'a2')
// log
Execute interceptor call a1 a2
test a1 a2
When the
before_runin Rust callscall, these interceptors'callfunctions will also be executed, and then thebeforeRun.callwrapped in these interceptors on the JS side will be executed, triggering the execution of the corresponding Tap function inmyPlugin.
With these steps, the entire Plugin system is completed. The complete changes can be seen here. I won't go through the code one by one, but by following the order in the diagram, you should be able to understand it.
The original intention of this series of articles was to deepen the understanding of webpack by reimplementing it. However, I found that my Rust skills were limited, and I didn't have the ability to implement a Plugin system. Most of the time was spent on integrating Rspack.
During this process, I realized that there are many areas that I don't understand well. I'll mark them down for future study:
- Napi, such as ThreadsafeFunction. Combining this with Node.js can achieve many things. I'll see if I can come up with some examples later.
- Asynchronous processing and tokio in Rust.
- Rust concurrent programming: multithreading, channels, etc.
- Macro programming in Rust, which is difficult to write and debug.
Please kindly give me a star!
↗ Original-Artikel auf dev.to lesenVollständiger Original-BerichtAusführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
Ähnliche Beiträge
Auch interessante Nachrichten Implementing Webpack from Scratch, But in Rust - [5] Support Customized JS Plugin
Thematisch verwandte Begriffe: Implementing, Webpack, from, Scratch · 6 Treffer
Reverse Engineering / Authorized Penetration Testing / Security Research Skill Router Pack AI-powered routing + On-demand toolchain bootstrapping + Self-evolving knowledge base Supports Claude Code, Kiro, Cursor, Cline, and other AI coding clients
drakoarmy/datadome-rs: High-end Rust DataDome deobfuscator & solver with VM disassembly — all 3 challenge types (tags, interstitial, slider).
ai backed x64dbg or similar ?
How I Turned Self-XSS into Reflected XSS (and Bypassed the WAF)
[webapps] Grav CMS 2.0.7 - RCE
Videos werden geladen ...
Beiträge werden geladen ...
Videos werden geladen ...
Beiträge werden geladen ...
Videos werden geladen ...
Beiträge werden geladen ...
Videos werden geladen ...
Beiträge werden geladen ...
Videos werden geladen ...
SOCIAL SHARE CARD GENERATOR