Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add real content hash plugin hook #9097

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions Cargo.lock

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

35 changes: 18 additions & 17 deletions crates/node_binding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@ crate-type = ["cdylib"]
plugin = ["rspack_binding_values/plugin"]

[dependencies]
anyhow = { workspace = true }
ropey = { workspace = true }
rspack_allocator = { workspace = true }
rspack_binding_values = { workspace = true }
rspack_collections = { workspace = true }
rspack_core = { workspace = true }
rspack_error = { workspace = true }
rspack_fs = { workspace = true }
rspack_fs_node = { workspace = true }
rspack_hash = { workspace = true }
rspack_hook = { workspace = true }
rspack_napi = { workspace = true }
rspack_paths = { workspace = true }
rspack_plugin_html = { workspace = true }
rspack_plugin_javascript = { workspace = true }
rspack_plugin_runtime = { workspace = true }
rspack_util = { workspace = true }
anyhow = { workspace = true }
ropey = { workspace = true }
rspack_allocator = { workspace = true }
rspack_binding_values = { workspace = true }
rspack_collections = { workspace = true }
rspack_core = { workspace = true }
rspack_error = { workspace = true }
rspack_fs = { workspace = true }
rspack_fs_node = { workspace = true }
rspack_hash = { workspace = true }
rspack_hook = { workspace = true }
rspack_napi = { workspace = true }
rspack_paths = { workspace = true }
rspack_plugin_html = { workspace = true }
rspack_plugin_javascript = { workspace = true }
rspack_plugin_real_content_hash = { workspace = true }
rspack_plugin_runtime = { workspace = true }
rspack_util = { workspace = true }

rspack_tracing = { workspace = true }

Expand Down
15 changes: 11 additions & 4 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,11 @@ export interface JsTap {
stage: number
}

export interface JsUpdateHashData {
assets: Array<Buffer>
oldHash: string
}

export interface NodeFsStats {
isFile: boolean
isDirectory: boolean
Expand Down Expand Up @@ -2135,7 +2140,8 @@ export declare enum RegisterJsTapKind {
HtmlPluginAfterEmit = 40,
RuntimePluginCreateScript = 41,
RuntimePluginLinkPreload = 42,
RuntimePluginLinkPrefetch = 43
RuntimePluginLinkPrefetch = 43,
RealContentHashPluginUpdateHash = 44
}

export interface RegisterJsTaps {
Expand Down Expand Up @@ -2180,9 +2186,10 @@ export interface RegisterJsTaps {
registerHtmlPluginAfterTemplateExecutionTaps: (stages: Array<number>) => Array<{ function: ((arg: JsAfterTemplateExecutionData) => JsAfterTemplateExecutionData); stage: number; }>
registerHtmlPluginBeforeEmitTaps: (stages: Array<number>) => Array<{ function: ((arg: JsBeforeEmitData) => JsBeforeEmitData); stage: number; }>
registerHtmlPluginAfterEmitTaps: (stages: Array<number>) => Array<{ function: ((arg: JsAfterEmitData) => JsAfterEmitData); stage: number; }>
registerRuntimePluginCreateScriptTaps: (stages: Array<number>) => Array<{ function: ((arg: JsCreateScriptData) => String); stage: number; }>
registerRuntimePluginLinkPreloadTaps: (stages: Array<number>) => Array<{ function: ((arg: JsLinkPreloadData) => String); stage: number; }>
registerRuntimePluginLinkPrefetchTaps: (stages: Array<number>) => Array<{ function: ((arg: JsLinkPrefetchData) => String); stage: number; }>
registerRuntimePluginCreateScriptTaps: (stages: Array<number>) => Array<{ function: ((arg: JsCreateScriptData) => string | undefined); stage: number; }>
registerRuntimePluginLinkPreloadTaps: (stages: Array<number>) => Array<{ function: ((arg: JsLinkPreloadData) => string | undefined); stage: number; }>
registerRuntimePluginLinkPrefetchTaps: (stages: Array<number>) => Array<{ function: ((arg: JsLinkPrefetchData) => string | undefined); stage: number; }>
registerRealContentHashPluginUpdateHashTaps: (stages: Array<number>) => Array<{ function: ((arg: JsUpdateHashData) => string | undefined); stage: number; }>
}

export interface ThreadsafeNodeFS {
Expand Down
49 changes: 45 additions & 4 deletions crates/node_binding/src/plugins/interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use rspack_binding_values::{
JsLinkPrefetchData, JsLinkPreloadData, JsModuleWrapper, JsNormalModuleFactoryCreateModuleArgs,
JsResolveArgs, JsResolveForSchemeArgs, JsResolveForSchemeOutput, JsResolveOutput,
JsRuntimeGlobals, JsRuntimeModule, JsRuntimeModuleArg, JsRuntimeRequirementInTreeArg,
JsRuntimeRequirementInTreeResult, ToJsCompatSourceOwned,
JsRuntimeRequirementInTreeResult, JsUpdateHashData, ToJsCompatSourceOwned,
};
use rspack_collections::IdentifierSet;
use rspack_core::{
Expand Down Expand Up @@ -68,6 +68,9 @@ use rspack_plugin_html::{
HtmlPluginBeforeAssetTagGenerationHook, HtmlPluginBeforeEmit, HtmlPluginBeforeEmitHook,
};
use rspack_plugin_javascript::{JavascriptModulesChunkHash, JavascriptModulesChunkHashHook};
use rspack_plugin_real_content_hash::{
RealContentHashPluginUpdateHash, RealContentHashPluginUpdateHashHook,
};
use rspack_plugin_runtime::{
CreateScriptData, LinkPrefetchData, LinkPreloadData, RuntimePluginCreateScript,
RuntimePluginCreateScriptHook, RuntimePluginLinkPrefetch, RuntimePluginLinkPrefetchHook,
Expand Down Expand Up @@ -395,6 +398,7 @@ pub enum RegisterJsTapKind {
RuntimePluginCreateScript,
RuntimePluginLinkPreload,
RuntimePluginLinkPrefetch,
RealContentHashPluginUpdateHash,
}

#[derive(Default, Clone)]
Expand Down Expand Up @@ -599,20 +603,26 @@ pub struct RegisterJsTaps {
pub register_html_plugin_after_emit_taps:
RegisterFunction<JsAfterEmitData, Promise<JsAfterEmitData>>,
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsCreateScriptData) => String); stage: number; }>"
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsCreateScriptData) => string | undefined); stage: number; }>"
)]
pub register_runtime_plugin_create_script_taps:
RegisterFunction<JsCreateScriptData, Option<String>>,
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsLinkPreloadData) => String); stage: number; }>"
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsLinkPreloadData) => string | undefined); stage: number; }>"
)]
pub register_runtime_plugin_link_preload_taps:
RegisterFunction<JsLinkPreloadData, Option<String>>,
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsLinkPrefetchData) => String); stage: number; }>"
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsLinkPrefetchData) => string | undefined); stage: number; }>"
)]
pub register_runtime_plugin_link_prefetch_taps:
RegisterFunction<JsLinkPrefetchData, Option<String>>,

#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsUpdateHashData) => string | undefined); stage: number; }>"
)]
pub register_real_content_hash_plugin_update_hash_taps:
RegisterFunction<JsUpdateHashData, Option<String>>,
}

/* Compiler Hooks */
Expand Down Expand Up @@ -983,6 +993,14 @@ define_register!(
kind = RegisterJsTapKind::RuntimePluginLinkPrefetch,
skip = true,
);
define_register!(
RegisterRealContentHashPluginUpdateHashTaps,
tap = RealContentHashPluginUpdateHashTap<JsUpdateHashData, Option<String>> @ RealContentHashPluginUpdateHashHook,
cache = true,
sync = false,
kind = RegisterJsTapKind::RealContentHashPluginUpdateHash,
skip = true,
);

#[async_trait]
impl CompilerThisCompilation for CompilerThisCompilationTap {
Expand Down Expand Up @@ -1870,3 +1888,26 @@ impl RuntimePluginLinkPrefetch for RuntimePluginLinkPrefetchTap {
self.stage
}
}

#[async_trait]
impl RealContentHashPluginUpdateHash for RealContentHashPluginUpdateHashTap {
async fn run(
&self,
assets: &mut Vec<Vec<u8>>,
old_hash: &mut String,
) -> rspack_error::Result<Option<String>> {
let assets = std::mem::take(assets)
.into_iter()
.map(|asset| asset.into())
.collect::<Vec<_>>();
let old_hash = std::mem::take(old_hash);
self
.function
.call_with_sync(JsUpdateHashData { assets, old_hash })
.await
}

fn stage(&self) -> i32 {
self.stage
}
}
31 changes: 31 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_real_content_hash::RealContentHashPlugin;
use rspack_plugin_runtime::RuntimePlugin;

use self::interceptor::*;
Expand Down Expand Up @@ -71,6 +72,7 @@ pub struct JsHooksAdapterPlugin {
register_runtime_plugin_create_script_taps: RegisterRuntimePluginCreateScriptTaps,
register_runtime_plugin_link_preload_taps: RegisterRuntimePluginLinkPreloadTaps,
register_runtime_plugin_link_prefetch_taps: RegisterRuntimePluginLinkPrefetchTaps,
register_real_content_hash_plugin_update_hash_taps: RegisterRealContentHashPluginUpdateHashTaps,
}

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

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

Ok(())
}

Expand Down Expand Up @@ -413,6 +421,9 @@ impl rspack_core::Plugin for JsHooksAdapterPlugin {
self
.register_runtime_plugin_link_prefetch_taps
.clear_cache();
self
.register_real_content_hash_plugin_update_hash_taps
.clear_cache();
}
}

Expand Down Expand Up @@ -484,6 +495,21 @@ async fn runtime_hooks_adapter_compilation(
Ok(())
}

#[plugin_hook(CompilerCompilation for JsHooksAdapterPlugin)]
async fn real_content_hash_hooks_adapter_compilation(
&self,
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> rspack_error::Result<()> {
let mut hooks = RealContentHashPlugin::get_compilation_hooks_mut(compilation);
hooks.update_hash.intercept(
self
.register_real_content_hash_plugin_update_hash_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 @@ -680,6 +706,11 @@ impl JsHooksAdapterPlugin {
register_js_taps.register_runtime_plugin_link_prefetch_taps,
non_skippable_registers.clone(),
),
register_real_content_hash_plugin_update_hash_taps:
RegisterRealContentHashPluginUpdateHashTaps::new(
register_js_taps.register_real_content_hash_plugin_update_hash_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 @@ -24,6 +24,7 @@ mod options;
mod path_data;
mod plugins;
mod raw_options;
mod real_content_hash;
mod resolver;
mod resource_data;
mod rspack_error;
Expand Down Expand Up @@ -55,6 +56,7 @@ pub use path_data::*;
pub use plugins::buildtime_plugins;
pub(crate) use plugins::*;
pub use raw_options::*;
pub use real_content_hash::*;
pub use resolver::*;
pub use resource_data::*;
pub use rspack_error::*;
Expand Down
8 changes: 8 additions & 0 deletions crates/rspack_binding_values/src/real_content_hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use napi::bindgen_prelude::Buffer;
use napi_derive::napi;

#[napi(object, object_from_js = false)]
pub struct JsUpdateHashData {
pub assets: Vec<Buffer>,
pub old_hash: String,
}
2 changes: 2 additions & 0 deletions crates/rspack_plugin_real_content_hash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ version = "0.2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
dashmap = { workspace = true }
derive_more = { workspace = true, features = ["debug"] }
indexmap = { workspace = true }
once_cell = { workspace = true }
Expand All @@ -17,6 +18,7 @@ rspack_core = { workspace = true }
rspack_error = { workspace = true }
rspack_hash = { workspace = true }
rspack_hook = { workspace = true }
rspack_util = { workspace = true }
rustc-hash = { workspace = true }
tracing = { workspace = true }

Expand Down
8 changes: 8 additions & 0 deletions crates/rspack_plugin_real_content_hash/src/drive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use rspack_hook::define_hook;

define_hook!(RealContentHashPluginUpdateHash: AsyncSeriesBail(assets: &mut Vec<Vec<u8>>, old_hash: &mut String) -> String);

#[derive(Debug, Default)]
pub struct RealContentHashPluginHooks {
pub update_hash: RealContentHashPluginUpdateHashHook,
}
Loading
Loading