Skip to content

Commit 32c63a8

Browse files
committed
feat: add rsdoctor native plugin
1 parent 9763310 commit 32c63a8

File tree

19 files changed

+423
-149
lines changed

19 files changed

+423
-149
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@
4343
"ukey",
4444
"Ukey"
4545
],
46-
"rust-analyzer.checkOnSave": true
46+
"rust-analyzer.checkOnSave": false
4747
}

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/node_binding/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ rspack_napi = { workspace = true }
2727
rspack_paths = { workspace = true }
2828
rspack_plugin_html = { workspace = true }
2929
rspack_plugin_javascript = { workspace = true }
30+
rspack_plugin_rsdoctor = { workspace = true }
3031
rspack_util = { workspace = true }
3132

3233
rspack_tracing = { workspace = true }

crates/node_binding/src/plugins/interceptor.rs

Lines changed: 147 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ use rspack_binding_values::{
1919
JsContextModuleFactoryBeforeResolveDataWrapper, JsContextModuleFactoryBeforeResolveResult,
2020
JsCreateData, JsExecuteModuleArg, JsFactorizeArgs, JsFactorizeOutput, JsModuleWrapper,
2121
JsNormalModuleFactoryCreateModuleArgs, JsResolveArgs, JsResolveForSchemeArgs,
22-
JsResolveForSchemeOutput, JsResolveOutput, JsRuntimeGlobals, JsRuntimeModule, JsRuntimeModuleArg,
23-
JsRuntimeRequirementInTreeArg, JsRuntimeRequirementInTreeResult, ToJsCompatSourceOwned,
22+
JsResolveForSchemeOutput, JsResolveOutput, JsRsdoctorAsset, JsRsdoctorChunkGraph,
23+
JsRsdoctorModuleGraph, JsRsdoctorModuleSource, JsRuntimeGlobals, JsRuntimeModule,
24+
JsRuntimeModuleArg, JsRuntimeRequirementInTreeArg, JsRuntimeRequirementInTreeResult,
25+
ToJsCompatSourceOwned,
2426
};
2527
use rspack_collections::IdentifierSet;
2628
use rspack_core::{
@@ -67,6 +69,12 @@ use rspack_plugin_html::{
6769
HtmlPluginBeforeAssetTagGenerationHook, HtmlPluginBeforeEmit, HtmlPluginBeforeEmitHook,
6870
};
6971
use rspack_plugin_javascript::{JavascriptModulesChunkHash, JavascriptModulesChunkHashHook};
72+
use rspack_plugin_rsdoctor::{
73+
RsdoctorAsset, RsdoctorChunkGraph, RsdoctorModuleGraph, RsdoctorModuleSource,
74+
RsdoctorPluginAssets, RsdoctorPluginAssetsHook, RsdoctorPluginChunkGraph,
75+
RsdoctorPluginChunkGraphHook, RsdoctorPluginModuleGraph, RsdoctorPluginModuleGraphHook,
76+
RsdoctorPluginModuleSources, RsdoctorPluginModuleSourcesHook,
77+
};
7078

7179
#[napi(object)]
7280
pub struct JsTap<'f> {
@@ -386,6 +394,10 @@ pub enum RegisterJsTapKind {
386394
HtmlPluginAfterTemplateExecution,
387395
HtmlPluginBeforeEmit,
388396
HtmlPluginAfterEmit,
397+
RsdoctorPluginModuleGraph,
398+
RsdoctorPluginChunkGraph,
399+
RsdoctorPluginModuleSources,
400+
RsdoctorPluginAssets,
389401
}
390402

391403
#[derive(Default, Clone)]
@@ -559,6 +571,7 @@ pub struct RegisterJsTaps {
559571
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsChunk) => Buffer); stage: number; }>"
560572
)]
561573
pub register_javascript_modules_chunk_hash_taps: RegisterFunction<JsChunkWrapper, Buffer>,
574+
// html plugin
562575
#[napi(
563576
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsBeforeAssetTagGenerationData) => JsBeforeAssetTagGenerationData); stage: number; }>"
564577
)]
@@ -589,6 +602,27 @@ pub struct RegisterJsTaps {
589602
)]
590603
pub register_html_plugin_after_emit_taps:
591604
RegisterFunction<JsAfterEmitData, Promise<JsAfterEmitData>>,
605+
// rsdoctor plugin
606+
#[napi(
607+
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsRsdoctorModuleGraph) => Promise<boolean | undefined>); stage: number; }>"
608+
)]
609+
pub register_rsdoctor_plugin_module_graph_taps:
610+
RegisterFunction<JsRsdoctorModuleGraph, Promise<Option<bool>>>,
611+
#[napi(
612+
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsRsdoctorChunkGraph) => Promise<boolean | undefined>); stage: number; }>"
613+
)]
614+
pub register_rsdoctor_plugin_chunk_graph_taps:
615+
RegisterFunction<JsRsdoctorChunkGraph, Promise<Option<bool>>>,
616+
#[napi(
617+
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: Vec<JsRsdoctorModuleSource>) => Promise<boolean | undefined>); stage: number; }>"
618+
)]
619+
pub register_rsdoctor_plugin_module_sources_taps:
620+
RegisterFunction<Vec<JsRsdoctorModuleSource>, Promise<Option<bool>>>,
621+
#[napi(
622+
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: Vec<JsRsdoctorAsset>) => Promise<boolean | undefined>); stage: number; }>"
623+
)]
624+
pub register_rsdoctor_plugin_assets_taps:
625+
RegisterFunction<Vec<JsRsdoctorAsset>, Promise<Option<bool>>>,
592626
}
593627

594628
/* Compiler Hooks */
@@ -936,6 +970,43 @@ define_register!(
936970
skip = true,
937971
);
938972

973+
/* Rsdoctor Plugin Hooks */
974+
define_register!(
975+
RegisterRsdoctorPluginModuleGraphTaps,
976+
tap = RsdoctorPluginModuleGraphTap<JsRsdoctorModuleGraph, Promise<Option<bool>>> @ RsdoctorPluginModuleGraphHook,
977+
cache = true,
978+
sync = false,
979+
kind = RegisterJsTapKind::RsdoctorPluginModuleGraph,
980+
skip = true,
981+
);
982+
983+
define_register!(
984+
RegisterRsdoctorPluginChunkGraphTaps,
985+
tap = RsdoctorPluginChunkGraphTap<JsRsdoctorChunkGraph, Promise<Option<bool>>> @ RsdoctorPluginChunkGraphHook,
986+
cache = true,
987+
sync = false,
988+
kind = RegisterJsTapKind::RsdoctorPluginChunkGraph,
989+
skip = true,
990+
);
991+
992+
define_register!(
993+
RegisterRsdoctorPluginAssetsTaps,
994+
tap = RsdoctorPluginAssetsTap<Vec<JsRsdoctorAsset>, Promise<Option<bool>>> @ RsdoctorPluginAssetsHook,
995+
cache = true,
996+
sync = false,
997+
kind = RegisterJsTapKind::RsdoctorPluginAssets,
998+
skip = true,
999+
);
1000+
1001+
define_register!(
1002+
RegisterRsdoctorPluginModuleSourcesTaps,
1003+
tap = RsdoctorPluginModuleSourcesTap<Vec<JsRsdoctorModuleSource>, Promise<Option<bool>>> @ RsdoctorPluginModuleSourcesHook,
1004+
cache = true,
1005+
sync = false,
1006+
kind = RegisterJsTapKind::RsdoctorPluginModuleSources,
1007+
skip = true,
1008+
);
1009+
9391010
#[async_trait]
9401011
impl CompilerThisCompilation for CompilerThisCompilationTap {
9411012
async fn run(
@@ -1768,3 +1839,77 @@ impl HtmlPluginAfterEmit for HtmlPluginAfterEmitTap {
17681839
self.stage
17691840
}
17701841
}
1842+
1843+
#[async_trait]
1844+
impl RsdoctorPluginModuleGraph for RsdoctorPluginModuleGraphTap {
1845+
async fn run(&self, data: &mut RsdoctorModuleGraph) -> rspack_error::Result<Option<bool>> {
1846+
let data = std::mem::take(data);
1847+
let bail = self
1848+
.function
1849+
.call_with_promise(JsRsdoctorModuleGraph::from(data))
1850+
.await?;
1851+
Ok(bail)
1852+
}
1853+
1854+
fn stage(&self) -> i32 {
1855+
self.stage
1856+
}
1857+
}
1858+
1859+
#[async_trait]
1860+
impl RsdoctorPluginChunkGraph for RsdoctorPluginChunkGraphTap {
1861+
async fn run(&self, data: &mut RsdoctorChunkGraph) -> rspack_error::Result<Option<bool>> {
1862+
let data = std::mem::take(data);
1863+
let bail = self
1864+
.function
1865+
.call_with_promise(JsRsdoctorChunkGraph::from(data))
1866+
.await?;
1867+
Ok(bail)
1868+
}
1869+
1870+
fn stage(&self) -> i32 {
1871+
self.stage
1872+
}
1873+
}
1874+
1875+
#[async_trait]
1876+
impl RsdoctorPluginModuleSources for RsdoctorPluginModuleSourcesTap {
1877+
async fn run(&self, data: &mut Vec<RsdoctorModuleSource>) -> rspack_error::Result<Option<bool>> {
1878+
let data = std::mem::take(data);
1879+
let bail = self
1880+
.function
1881+
.call_with_promise(
1882+
data
1883+
.into_iter()
1884+
.map(JsRsdoctorModuleSource::from)
1885+
.collect::<Vec<_>>(),
1886+
)
1887+
.await?;
1888+
Ok(bail)
1889+
}
1890+
1891+
fn stage(&self) -> i32 {
1892+
self.stage
1893+
}
1894+
}
1895+
1896+
#[async_trait]
1897+
impl RsdoctorPluginAssets for RsdoctorPluginAssetsTap {
1898+
async fn run(&self, data: &mut Vec<RsdoctorAsset>) -> rspack_error::Result<Option<bool>> {
1899+
let data = std::mem::take(data);
1900+
let bail = self
1901+
.function
1902+
.call_with_promise(
1903+
data
1904+
.into_iter()
1905+
.map(JsRsdoctorAsset::from)
1906+
.collect::<Vec<_>>(),
1907+
)
1908+
.await?;
1909+
Ok(bail)
1910+
}
1911+
1912+
fn stage(&self) -> i32 {
1913+
self.stage
1914+
}
1915+
}

crates/node_binding/src/plugins/mod.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rspack_hook::plugin_hook;
1414
use rspack_hook::Hook as _;
1515
use rspack_plugin_html::HtmlRspackPlugin;
1616
use rspack_plugin_javascript::JsPlugin;
17+
use rspack_plugin_rsdoctor::RsdoctorPlugin;
1718

1819
use self::interceptor::*;
1920

@@ -66,6 +67,10 @@ pub struct JsHooksAdapterPlugin {
6667
register_html_plugin_after_template_execution_taps: RegisterHtmlPluginAfterTemplateExecutionTaps,
6768
register_html_plugin_before_emit_taps: RegisterHtmlPluginBeforeEmitTaps,
6869
register_html_plugin_after_emit_taps: RegisterHtmlPluginAfterEmitTaps,
70+
register_rsdoctor_plugin_module_graph_taps: RegisterRsdoctorPluginModuleGraphTaps,
71+
register_rsdoctor_plugin_chunk_graph_taps: RegisterRsdoctorPluginChunkGraphTaps,
72+
register_rsdoctor_plugin_assets_taps: RegisterRsdoctorPluginAssetsTaps,
73+
register_rsdoctor_plugin_module_sources_taps: RegisterRsdoctorPluginModuleSourcesTaps,
6974
}
7075

7176
impl fmt::Debug for JsHooksAdapterPlugin {
@@ -310,6 +315,12 @@ impl rspack_core::Plugin for JsHooksAdapterPlugin {
310315
.compilation
311316
.tap(html_hooks_adapter_compilation::new(self));
312317

318+
ctx
319+
.context
320+
.compiler_hooks
321+
.compilation
322+
.tap(rsdoctor_hooks_adapter_compilation::new(self));
323+
313324
Ok(())
314325
}
315326

@@ -395,6 +406,14 @@ impl rspack_core::Plugin for JsHooksAdapterPlugin {
395406
.clear_cache();
396407
self.register_html_plugin_before_emit_taps.clear_cache();
397408
self.register_html_plugin_after_emit_taps.clear_cache();
409+
self
410+
.register_rsdoctor_plugin_module_graph_taps
411+
.clear_cache();
412+
self.register_rsdoctor_plugin_chunk_graph_taps.clear_cache();
413+
self.register_rsdoctor_plugin_assets_taps.clear_cache();
414+
self
415+
.register_rsdoctor_plugin_module_sources_taps
416+
.clear_cache();
398417
}
399418
}
400419

@@ -447,6 +466,29 @@ async fn html_hooks_adapter_compilation(
447466
Ok(())
448467
}
449468

469+
#[plugin_hook(CompilerCompilation for JsHooksAdapterPlugin)]
470+
async fn rsdoctor_hooks_adapter_compilation(
471+
&self,
472+
compilation: &mut Compilation,
473+
_params: &mut CompilationParams,
474+
) -> rspack_error::Result<()> {
475+
let mut hooks = RsdoctorPlugin::get_compilation_hooks_mut(compilation);
476+
hooks
477+
.module_graph
478+
.intercept(self.register_rsdoctor_plugin_module_graph_taps.clone());
479+
hooks
480+
.chunk_graph
481+
.intercept(self.register_rsdoctor_plugin_chunk_graph_taps.clone());
482+
hooks
483+
.assets
484+
.intercept(self.register_rsdoctor_plugin_assets_taps.clone());
485+
hooks
486+
.module_sources
487+
.intercept(self.register_rsdoctor_plugin_module_sources_taps.clone());
488+
489+
Ok(())
490+
}
491+
450492
impl JsHooksAdapterPlugin {
451493
pub fn from_js_hooks(_env: Env, register_js_taps: RegisterJsTaps) -> Result<Self> {
452494
let non_skippable_registers = NonSkippableRegisters::default();
@@ -631,6 +673,22 @@ impl JsHooksAdapterPlugin {
631673
register_js_taps.register_html_plugin_after_emit_taps,
632674
non_skippable_registers.clone(),
633675
),
676+
register_rsdoctor_plugin_module_graph_taps: RegisterRsdoctorPluginModuleGraphTaps::new(
677+
register_js_taps.register_rsdoctor_plugin_module_graph_taps,
678+
non_skippable_registers.clone(),
679+
),
680+
register_rsdoctor_plugin_chunk_graph_taps: RegisterRsdoctorPluginChunkGraphTaps::new(
681+
register_js_taps.register_rsdoctor_plugin_chunk_graph_taps,
682+
non_skippable_registers.clone(),
683+
),
684+
register_rsdoctor_plugin_assets_taps: RegisterRsdoctorPluginAssetsTaps::new(
685+
register_js_taps.register_rsdoctor_plugin_assets_taps,
686+
non_skippable_registers.clone(),
687+
),
688+
register_rsdoctor_plugin_module_sources_taps: RegisterRsdoctorPluginModuleSourcesTaps::new(
689+
register_js_taps.register_rsdoctor_plugin_module_sources_taps,
690+
non_skippable_registers.clone(),
691+
),
634692
non_skippable_registers,
635693
}
636694
.into(),

crates/rspack_binding_values/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ mod plugins;
2626
mod raw_options;
2727
mod resolver;
2828
mod resource_data;
29+
mod rsdoctor;
2930
mod rspack_error;
3031
mod runtime;
3132
mod source;
@@ -57,6 +58,7 @@ pub(crate) use plugins::*;
5758
pub use raw_options::*;
5859
pub use resolver::*;
5960
pub use resource_data::*;
61+
pub use rsdoctor::*;
6062
pub use rspack_error::*;
6163
pub use runtime::*;
6264
pub use source::*;
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
mod context_replacement;
22
mod js_loader;
3-
mod rsdoctor;
43

54
pub use context_replacement::*;
65
pub(super) use js_loader::{JsLoaderRspackPlugin, JsLoaderRunner};
76
pub mod buildtime_plugins;
8-
pub use rsdoctor::*;

crates/rspack_binding_values/src/plugins/rsdoctor/mod.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)