Skip to content

Commit

Permalink
feat: add rsdoctor native plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder committed Jan 8, 2025
1 parent 9763310 commit 32c63a8
Show file tree
Hide file tree
Showing 19 changed files with 423 additions and 149 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@
"ukey",
"Ukey"
],
"rust-analyzer.checkOnSave": true
"rust-analyzer.checkOnSave": false
}
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/node_binding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ rspack_napi = { workspace = true }
rspack_paths = { workspace = true }
rspack_plugin_html = { workspace = true }
rspack_plugin_javascript = { workspace = true }
rspack_plugin_rsdoctor = { workspace = true }
rspack_util = { workspace = true }

rspack_tracing = { workspace = true }
Expand Down
149 changes: 147 additions & 2 deletions crates/node_binding/src/plugins/interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ use rspack_binding_values::{
JsContextModuleFactoryBeforeResolveDataWrapper, JsContextModuleFactoryBeforeResolveResult,
JsCreateData, JsExecuteModuleArg, JsFactorizeArgs, JsFactorizeOutput, JsModuleWrapper,
JsNormalModuleFactoryCreateModuleArgs, JsResolveArgs, JsResolveForSchemeArgs,
JsResolveForSchemeOutput, JsResolveOutput, JsRuntimeGlobals, JsRuntimeModule, JsRuntimeModuleArg,
JsRuntimeRequirementInTreeArg, JsRuntimeRequirementInTreeResult, ToJsCompatSourceOwned,
JsResolveForSchemeOutput, JsResolveOutput, JsRsdoctorAsset, JsRsdoctorChunkGraph,
JsRsdoctorModuleGraph, JsRsdoctorModuleSource, JsRuntimeGlobals, JsRuntimeModule,
JsRuntimeModuleArg, JsRuntimeRequirementInTreeArg, JsRuntimeRequirementInTreeResult,
ToJsCompatSourceOwned,
};
use rspack_collections::IdentifierSet;
use rspack_core::{
Expand Down Expand Up @@ -67,6 +69,12 @@ use rspack_plugin_html::{
HtmlPluginBeforeAssetTagGenerationHook, HtmlPluginBeforeEmit, HtmlPluginBeforeEmitHook,
};
use rspack_plugin_javascript::{JavascriptModulesChunkHash, JavascriptModulesChunkHashHook};
use rspack_plugin_rsdoctor::{
RsdoctorAsset, RsdoctorChunkGraph, RsdoctorModuleGraph, RsdoctorModuleSource,
RsdoctorPluginAssets, RsdoctorPluginAssetsHook, RsdoctorPluginChunkGraph,
RsdoctorPluginChunkGraphHook, RsdoctorPluginModuleGraph, RsdoctorPluginModuleGraphHook,
RsdoctorPluginModuleSources, RsdoctorPluginModuleSourcesHook,
};

#[napi(object)]
pub struct JsTap<'f> {
Expand Down Expand Up @@ -386,6 +394,10 @@ pub enum RegisterJsTapKind {
HtmlPluginAfterTemplateExecution,
HtmlPluginBeforeEmit,
HtmlPluginAfterEmit,
RsdoctorPluginModuleGraph,
RsdoctorPluginChunkGraph,
RsdoctorPluginModuleSources,
RsdoctorPluginAssets,
}

#[derive(Default, Clone)]
Expand Down Expand Up @@ -559,6 +571,7 @@ pub struct RegisterJsTaps {
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsChunk) => Buffer); stage: number; }>"
)]
pub register_javascript_modules_chunk_hash_taps: RegisterFunction<JsChunkWrapper, Buffer>,
// html plugin
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsBeforeAssetTagGenerationData) => JsBeforeAssetTagGenerationData); stage: number; }>"
)]
Expand Down Expand Up @@ -589,6 +602,27 @@ pub struct RegisterJsTaps {
)]
pub register_html_plugin_after_emit_taps:
RegisterFunction<JsAfterEmitData, Promise<JsAfterEmitData>>,
// rsdoctor plugin
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsRsdoctorModuleGraph) => Promise<boolean | undefined>); stage: number; }>"
)]
pub register_rsdoctor_plugin_module_graph_taps:
RegisterFunction<JsRsdoctorModuleGraph, Promise<Option<bool>>>,
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsRsdoctorChunkGraph) => Promise<boolean | undefined>); stage: number; }>"
)]
pub register_rsdoctor_plugin_chunk_graph_taps:
RegisterFunction<JsRsdoctorChunkGraph, Promise<Option<bool>>>,
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: Vec<JsRsdoctorModuleSource>) => Promise<boolean | undefined>); stage: number; }>"
)]
pub register_rsdoctor_plugin_module_sources_taps:
RegisterFunction<Vec<JsRsdoctorModuleSource>, Promise<Option<bool>>>,
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: Vec<JsRsdoctorAsset>) => Promise<boolean | undefined>); stage: number; }>"
)]
pub register_rsdoctor_plugin_assets_taps:
RegisterFunction<Vec<JsRsdoctorAsset>, Promise<Option<bool>>>,
}

/* Compiler Hooks */
Expand Down Expand Up @@ -936,6 +970,43 @@ define_register!(
skip = true,
);

/* Rsdoctor Plugin Hooks */
define_register!(
RegisterRsdoctorPluginModuleGraphTaps,
tap = RsdoctorPluginModuleGraphTap<JsRsdoctorModuleGraph, Promise<Option<bool>>> @ RsdoctorPluginModuleGraphHook,
cache = true,
sync = false,
kind = RegisterJsTapKind::RsdoctorPluginModuleGraph,
skip = true,
);

define_register!(
RegisterRsdoctorPluginChunkGraphTaps,
tap = RsdoctorPluginChunkGraphTap<JsRsdoctorChunkGraph, Promise<Option<bool>>> @ RsdoctorPluginChunkGraphHook,
cache = true,
sync = false,
kind = RegisterJsTapKind::RsdoctorPluginChunkGraph,
skip = true,
);

define_register!(
RegisterRsdoctorPluginAssetsTaps,
tap = RsdoctorPluginAssetsTap<Vec<JsRsdoctorAsset>, Promise<Option<bool>>> @ RsdoctorPluginAssetsHook,
cache = true,
sync = false,
kind = RegisterJsTapKind::RsdoctorPluginAssets,
skip = true,
);

define_register!(
RegisterRsdoctorPluginModuleSourcesTaps,
tap = RsdoctorPluginModuleSourcesTap<Vec<JsRsdoctorModuleSource>, Promise<Option<bool>>> @ RsdoctorPluginModuleSourcesHook,
cache = true,
sync = false,
kind = RegisterJsTapKind::RsdoctorPluginModuleSources,
skip = true,
);

#[async_trait]
impl CompilerThisCompilation for CompilerThisCompilationTap {
async fn run(
Expand Down Expand Up @@ -1768,3 +1839,77 @@ impl HtmlPluginAfterEmit for HtmlPluginAfterEmitTap {
self.stage
}
}

#[async_trait]
impl RsdoctorPluginModuleGraph for RsdoctorPluginModuleGraphTap {
async fn run(&self, data: &mut RsdoctorModuleGraph) -> rspack_error::Result<Option<bool>> {
let data = std::mem::take(data);
let bail = self
.function
.call_with_promise(JsRsdoctorModuleGraph::from(data))
.await?;
Ok(bail)
}

fn stage(&self) -> i32 {
self.stage
}
}

#[async_trait]
impl RsdoctorPluginChunkGraph for RsdoctorPluginChunkGraphTap {
async fn run(&self, data: &mut RsdoctorChunkGraph) -> rspack_error::Result<Option<bool>> {
let data = std::mem::take(data);
let bail = self
.function
.call_with_promise(JsRsdoctorChunkGraph::from(data))
.await?;
Ok(bail)
}

fn stage(&self) -> i32 {
self.stage
}
}

#[async_trait]
impl RsdoctorPluginModuleSources for RsdoctorPluginModuleSourcesTap {
async fn run(&self, data: &mut Vec<RsdoctorModuleSource>) -> rspack_error::Result<Option<bool>> {
let data = std::mem::take(data);
let bail = self
.function
.call_with_promise(
data
.into_iter()
.map(JsRsdoctorModuleSource::from)
.collect::<Vec<_>>(),
)
.await?;
Ok(bail)
}

fn stage(&self) -> i32 {
self.stage
}
}

#[async_trait]
impl RsdoctorPluginAssets for RsdoctorPluginAssetsTap {
async fn run(&self, data: &mut Vec<RsdoctorAsset>) -> rspack_error::Result<Option<bool>> {
let data = std::mem::take(data);
let bail = self
.function
.call_with_promise(
data
.into_iter()
.map(JsRsdoctorAsset::from)
.collect::<Vec<_>>(),
)
.await?;
Ok(bail)
}

fn stage(&self) -> i32 {
self.stage
}
}
58 changes: 58 additions & 0 deletions crates/node_binding/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use rspack_hook::plugin_hook;
use rspack_hook::Hook as _;
use rspack_plugin_html::HtmlRspackPlugin;
use rspack_plugin_javascript::JsPlugin;
use rspack_plugin_rsdoctor::RsdoctorPlugin;

use self::interceptor::*;

Expand Down Expand Up @@ -66,6 +67,10 @@ pub struct JsHooksAdapterPlugin {
register_html_plugin_after_template_execution_taps: RegisterHtmlPluginAfterTemplateExecutionTaps,
register_html_plugin_before_emit_taps: RegisterHtmlPluginBeforeEmitTaps,
register_html_plugin_after_emit_taps: RegisterHtmlPluginAfterEmitTaps,
register_rsdoctor_plugin_module_graph_taps: RegisterRsdoctorPluginModuleGraphTaps,
register_rsdoctor_plugin_chunk_graph_taps: RegisterRsdoctorPluginChunkGraphTaps,
register_rsdoctor_plugin_assets_taps: RegisterRsdoctorPluginAssetsTaps,
register_rsdoctor_plugin_module_sources_taps: RegisterRsdoctorPluginModuleSourcesTaps,
}

impl fmt::Debug for JsHooksAdapterPlugin {
Expand Down Expand Up @@ -310,6 +315,12 @@ impl rspack_core::Plugin for JsHooksAdapterPlugin {
.compilation
.tap(html_hooks_adapter_compilation::new(self));

ctx
.context
.compiler_hooks
.compilation
.tap(rsdoctor_hooks_adapter_compilation::new(self));

Ok(())
}

Expand Down Expand Up @@ -395,6 +406,14 @@ impl rspack_core::Plugin for JsHooksAdapterPlugin {
.clear_cache();
self.register_html_plugin_before_emit_taps.clear_cache();
self.register_html_plugin_after_emit_taps.clear_cache();
self
.register_rsdoctor_plugin_module_graph_taps
.clear_cache();
self.register_rsdoctor_plugin_chunk_graph_taps.clear_cache();
self.register_rsdoctor_plugin_assets_taps.clear_cache();
self
.register_rsdoctor_plugin_module_sources_taps
.clear_cache();
}
}

Expand Down Expand Up @@ -447,6 +466,29 @@ async fn html_hooks_adapter_compilation(
Ok(())
}

#[plugin_hook(CompilerCompilation for JsHooksAdapterPlugin)]
async fn rsdoctor_hooks_adapter_compilation(
&self,
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> rspack_error::Result<()> {
let mut hooks = RsdoctorPlugin::get_compilation_hooks_mut(compilation);
hooks
.module_graph
.intercept(self.register_rsdoctor_plugin_module_graph_taps.clone());
hooks
.chunk_graph
.intercept(self.register_rsdoctor_plugin_chunk_graph_taps.clone());
hooks
.assets
.intercept(self.register_rsdoctor_plugin_assets_taps.clone());
hooks
.module_sources
.intercept(self.register_rsdoctor_plugin_module_sources_taps.clone());

Ok(())
}

impl JsHooksAdapterPlugin {
pub fn from_js_hooks(_env: Env, register_js_taps: RegisterJsTaps) -> Result<Self> {
let non_skippable_registers = NonSkippableRegisters::default();
Expand Down Expand Up @@ -631,6 +673,22 @@ impl JsHooksAdapterPlugin {
register_js_taps.register_html_plugin_after_emit_taps,
non_skippable_registers.clone(),
),
register_rsdoctor_plugin_module_graph_taps: RegisterRsdoctorPluginModuleGraphTaps::new(
register_js_taps.register_rsdoctor_plugin_module_graph_taps,
non_skippable_registers.clone(),
),
register_rsdoctor_plugin_chunk_graph_taps: RegisterRsdoctorPluginChunkGraphTaps::new(
register_js_taps.register_rsdoctor_plugin_chunk_graph_taps,
non_skippable_registers.clone(),
),
register_rsdoctor_plugin_assets_taps: RegisterRsdoctorPluginAssetsTaps::new(
register_js_taps.register_rsdoctor_plugin_assets_taps,
non_skippable_registers.clone(),
),
register_rsdoctor_plugin_module_sources_taps: RegisterRsdoctorPluginModuleSourcesTaps::new(
register_js_taps.register_rsdoctor_plugin_module_sources_taps,
non_skippable_registers.clone(),
),
non_skippable_registers,
}
.into(),
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_binding_values/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod plugins;
mod raw_options;
mod resolver;
mod resource_data;
mod rsdoctor;
mod rspack_error;
mod runtime;
mod source;
Expand Down Expand Up @@ -57,6 +58,7 @@ pub(crate) use plugins::*;
pub use raw_options::*;
pub use resolver::*;
pub use resource_data::*;
pub use rsdoctor::*;
pub use rspack_error::*;
pub use runtime::*;
pub use source::*;
Expand Down
2 changes: 0 additions & 2 deletions crates/rspack_binding_values/src/plugins/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
mod context_replacement;
mod js_loader;
mod rsdoctor;

pub use context_replacement::*;
pub(super) use js_loader::{JsLoaderRspackPlugin, JsLoaderRunner};
pub mod buildtime_plugins;
pub use rsdoctor::*;
5 changes: 0 additions & 5 deletions crates/rspack_binding_values/src/plugins/rsdoctor/mod.rs

This file was deleted.

Loading

0 comments on commit 32c63a8

Please sign in to comment.