Skip to content

Commit

Permalink
refactor: add compilation id to html hook data (#9236)
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder authored Feb 11, 2025
1 parent 7f11461 commit 1e74e33
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ rspack_sources = { version = "0.4.5" }
rustc-hash = { version = "2.1.0" }
serde = { version = "1.0.217" }
serde_json = { version = "1.0.134" }
sha2 = { version = "0.10.8" }
simd-json = { version = "0.14.3" }
smol_str = { version = "0.3.0" }
stacker = { version = "0.1.17" }
Expand Down
8 changes: 8 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ export interface JsAdditionalTreeRuntimeRequirementsResult {

export interface JsAfterEmitData {
outputName: string
compilationId: number
}

export interface JsAfterResolveData {
Expand All @@ -429,19 +430,22 @@ export interface JsAfterTemplateExecutionData {
headTags: Array<JsHtmlPluginTag>
bodyTags: Array<JsHtmlPluginTag>
outputName: string
compilationId: number
}

export interface JsAlterAssetTagGroupsData {
headTags: Array<JsHtmlPluginTag>
bodyTags: Array<JsHtmlPluginTag>
publicPath: string
outputName: string
compilationId: number
}

export interface JsAlterAssetTagsData {
assetTags: JsHtmlPluginAssetTags
outputName: string
publicPath: string
compilationId: number
}

export interface JsAsset {
Expand Down Expand Up @@ -509,11 +513,13 @@ export interface JsBannerContentFnCtx {
export interface JsBeforeAssetTagGenerationData {
assets: JsHtmlPluginAssets
outputName: string
compilationId: number
}

export interface JsBeforeEmitData {
html: string
outputName: string
compilationId: number
}

export interface JsBeforeResolveArgs {
Expand Down Expand Up @@ -691,6 +697,8 @@ export interface JsHtmlPluginAssets {
js: Array<string>
css: Array<string>
favicon?: string
jsIntegrity?: Array<string | undefined | null>
cssIntegrity?: Array<string | undefined | null>
}

export interface JsHtmlPluginAssetTags {
Expand Down
25 changes: 25 additions & 0 deletions crates/rspack_binding_values/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::HashMap;
use cow_utils::CowUtils;
use napi::Either;
use napi_derive::napi;
use rspack_core::CompilationId;
use rspack_plugin_html::{
asset::{HtmlPluginAssetTags, HtmlPluginAssets},
tag::{HtmlPluginAttribute, HtmlPluginTag},
Expand Down Expand Up @@ -85,6 +86,8 @@ pub struct JsHtmlPluginAssets {
pub css: Vec<String>,
pub favicon: Option<String>,
// manifest: Option<String>,
pub js_integrity: Option<Vec<Option<String>>>,
pub css_integrity: Option<Vec<Option<String>>>,
}

impl From<HtmlPluginAssets> for JsHtmlPluginAssets {
Expand All @@ -94,6 +97,8 @@ impl From<HtmlPluginAssets> for JsHtmlPluginAssets {
js: value.js,
css: value.css,
favicon: value.favicon,
js_integrity: value.js_integrity,
css_integrity: value.css_integrity,
}
}
}
Expand All @@ -105,6 +110,8 @@ impl From<JsHtmlPluginAssets> for HtmlPluginAssets {
js: value.js,
css: value.css,
favicon: value.favicon,
js_integrity: value.js_integrity,
css_integrity: value.css_integrity,
}
}
}
Expand All @@ -113,13 +120,15 @@ impl From<JsHtmlPluginAssets> for HtmlPluginAssets {
pub struct JsBeforeAssetTagGenerationData {
pub assets: JsHtmlPluginAssets,
pub output_name: String,
pub compilation_id: u32,
}

impl From<JsBeforeAssetTagGenerationData> for BeforeAssetTagGenerationData {
fn from(value: JsBeforeAssetTagGenerationData) -> Self {
Self {
assets: value.assets.into(),
output_name: value.output_name,
compilation_id: CompilationId(value.compilation_id),
}
}
}
Expand All @@ -129,6 +138,7 @@ impl From<BeforeAssetTagGenerationData> for JsBeforeAssetTagGenerationData {
Self {
assets: value.assets.into(),
output_name: value.output_name,
compilation_id: value.compilation_id.0,
}
}
}
Expand Down Expand Up @@ -189,6 +199,7 @@ pub struct JsAlterAssetTagsData {
pub asset_tags: JsHtmlPluginAssetTags,
pub output_name: String,
pub public_path: String,
pub compilation_id: u32,
}

impl From<AlterAssetTagsData> for JsAlterAssetTagsData {
Expand All @@ -197,6 +208,7 @@ impl From<AlterAssetTagsData> for JsAlterAssetTagsData {
asset_tags: value.asset_tags.into(),
output_name: value.output_name,
public_path: value.public_path,
compilation_id: value.compilation_id.0,
}
}
}
Expand All @@ -207,6 +219,7 @@ impl From<JsAlterAssetTagsData> for AlterAssetTagsData {
asset_tags: value.asset_tags.into(),
output_name: value.output_name,
public_path: value.public_path,
compilation_id: CompilationId(value.compilation_id),
}
}
}
Expand All @@ -217,6 +230,7 @@ pub struct JsAlterAssetTagGroupsData {
pub body_tags: Vec<JsHtmlPluginTag>,
pub public_path: String,
pub output_name: String,
pub compilation_id: u32,
}

impl From<AlterAssetTagGroupsData> for JsAlterAssetTagGroupsData {
Expand All @@ -234,6 +248,7 @@ impl From<AlterAssetTagGroupsData> for JsAlterAssetTagGroupsData {
.collect::<Vec<_>>(),
public_path: value.public_path,
output_name: value.output_name,
compilation_id: value.compilation_id.0,
}
}
}
Expand All @@ -253,6 +268,7 @@ impl From<JsAlterAssetTagGroupsData> for AlterAssetTagGroupsData {
.collect::<Vec<_>>(),
public_path: value.public_path,
output_name: value.output_name,
compilation_id: CompilationId(value.compilation_id),
}
}
}
Expand All @@ -263,6 +279,7 @@ pub struct JsAfterTemplateExecutionData {
pub head_tags: Vec<JsHtmlPluginTag>,
pub body_tags: Vec<JsHtmlPluginTag>,
pub output_name: String,
pub compilation_id: u32,
}

impl From<AfterTemplateExecutionData> for JsAfterTemplateExecutionData {
Expand All @@ -280,6 +297,7 @@ impl From<AfterTemplateExecutionData> for JsAfterTemplateExecutionData {
.map(JsHtmlPluginTag::from)
.collect::<Vec<_>>(),
output_name: value.output_name,
compilation_id: value.compilation_id.0,
}
}
}
Expand All @@ -299,6 +317,7 @@ impl From<JsAfterTemplateExecutionData> for AfterTemplateExecutionData {
.map(HtmlPluginTag::from)
.collect::<Vec<_>>(),
output_name: value.output_name,
compilation_id: CompilationId(value.compilation_id),
}
}
}
Expand All @@ -307,13 +326,15 @@ impl From<JsAfterTemplateExecutionData> for AfterTemplateExecutionData {
pub struct JsBeforeEmitData {
pub html: String,
pub output_name: String,
pub compilation_id: u32,
}

impl From<BeforeEmitData> for JsBeforeEmitData {
fn from(value: BeforeEmitData) -> Self {
Self {
html: value.html,
output_name: value.output_name,
compilation_id: value.compilation_id.0,
}
}
}
Expand All @@ -323,19 +344,22 @@ impl From<JsBeforeEmitData> for BeforeEmitData {
Self {
html: value.html,
output_name: value.output_name,
compilation_id: CompilationId(value.compilation_id),
}
}
}

#[napi(object)]
pub struct JsAfterEmitData {
pub output_name: String,
pub compilation_id: u32,
}

impl From<AfterEmitData> for JsAfterEmitData {
fn from(value: AfterEmitData) -> Self {
Self {
output_name: value.output_name,
compilation_id: value.compilation_id.0,
}
}
}
Expand All @@ -344,6 +368,7 @@ impl From<JsAfterEmitData> for AfterEmitData {
fn from(value: JsAfterEmitData) -> Self {
Self {
output_name: value.output_name,
compilation_id: CompilationId(value.compilation_id),
}
}
}
2 changes: 1 addition & 1 deletion crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub struct CompilationHooks {

#[cacheable]
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct CompilationId(u32);
pub struct CompilationId(pub u32);

impl CompilationId {
pub fn new() -> Self {
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/options/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl From<&str> for WasmLoadingType {
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum CrossOriginLoading {
Disable,
Enable(String),
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_html/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ rspack_paths = { workspace = true }
rspack_util = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
sha2 = "0.10.8"
sha2 = { workspace = true }
sugar_path = { workspace = true }
swc_core = { workspace = true }
swc_html = { workspace = true }
Expand Down
4 changes: 3 additions & 1 deletion crates/rspack_plugin_html/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub struct HtmlPluginAssets {
pub css: Vec<String>,
pub favicon: Option<String>,
// manifest: Option<String>,
pub js_integrity: Option<Vec<Option<String>>>,
pub css_integrity: Option<Vec<Option<String>>>,
}

impl HtmlPluginAssets {
Expand All @@ -45,7 +47,7 @@ impl HtmlPluginAssets {
output_path: &Utf8PathBuf,
html_file_name: &Filename<NoFilenameFn>,
) -> (HtmlPluginAssets, HashMap<String, &'a CompilationAsset>) {
let mut assets = HtmlPluginAssets::default();
let mut assets: HtmlPluginAssets = HtmlPluginAssets::default();
let mut asset_map = HashMap::new();
assets.public_path = public_path.to_string();

Expand Down
7 changes: 7 additions & 0 deletions crates/rspack_plugin_html/src/drive.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rspack_core::CompilationId;
use rspack_hook::define_hook;

use crate::{
Expand All @@ -9,13 +10,15 @@ use crate::{
pub struct BeforeAssetTagGenerationData {
pub assets: HtmlPluginAssets,
pub output_name: String,
pub compilation_id: CompilationId,
}

#[derive(Clone, Debug)]
pub struct AlterAssetTagsData {
pub asset_tags: HtmlPluginAssetTags,
pub output_name: String,
pub public_path: String,
pub compilation_id: CompilationId,
}

#[derive(Clone, Debug)]
Expand All @@ -24,6 +27,7 @@ pub struct AlterAssetTagGroupsData {
pub body_tags: Vec<HtmlPluginTag>,
pub public_path: String,
pub output_name: String,
pub compilation_id: CompilationId,
}

#[derive(Clone, Debug)]
Expand All @@ -32,17 +36,20 @@ pub struct AfterTemplateExecutionData {
pub head_tags: Vec<HtmlPluginTag>,
pub body_tags: Vec<HtmlPluginTag>,
pub output_name: String,
pub compilation_id: CompilationId,
}

#[derive(Clone, Debug)]
pub struct BeforeEmitData {
pub html: String,
pub output_name: String,
pub compilation_id: CompilationId,
}

#[derive(Clone, Debug)]
pub struct AfterEmitData {
pub output_name: String,
pub compilation_id: CompilationId,
}

define_hook!(HtmlPluginBeforeAssetTagGeneration: AsyncSeriesWaterfall(data: BeforeAssetTagGenerationData) -> BeforeAssetTagGenerationData);
Expand Down
6 changes: 6 additions & 0 deletions crates/rspack_plugin_html/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ async fn generate_html(
.call(BeforeAssetTagGenerationData {
assets: assets_info.0,
output_name: html_file_name.as_str().to_string(),
compilation_id: compilation.id(),
})
.await?;

Expand All @@ -99,6 +100,7 @@ async fn generate_html(
asset_tags,
public_path: public_path.clone(),
output_name: html_file_name.as_str().to_string(),
compilation_id: compilation.id(),
})
.await?;

Expand All @@ -112,6 +114,7 @@ async fn generate_html(
body_tags,
public_path: public_path.clone(),
output_name: html_file_name.as_str().to_string(),
compilation_id: compilation.id(),
})
.await?;

Expand All @@ -135,6 +138,7 @@ async fn generate_html(
head_tags: alter_asset_tag_groups_data.head_tags,
body_tags: alter_asset_tag_groups_data.body_tags,
output_name: html_file_name.as_str().to_string(),
compilation_id: compilation.id(),
})
.await?;

Expand Down Expand Up @@ -234,6 +238,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
.call(BeforeEmitData {
html,
output_name: output_file_name.as_str().to_string(),
compilation_id: compilation.id(),
})
.await?;

Expand Down Expand Up @@ -261,6 +266,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
.after_emit
.call(AfterEmitData {
output_name: html_asset.0.to_string(),
compilation_id: compilation.id(),
})
.await?;
}
Expand Down
Loading

2 comments on commit 1e74e33

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on 1e74e33 Feb 11, 2025

Choose a reason for hiding this comment

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

📝 Benchmark detail: Open

Name Base (2025-02-11 9d82335) Current Change
10000_big_production-mode_disable-minimize + exec 37.7 s ± 637 ms 40 s ± 1.1 s +6.11 %
10000_development-mode + exec 1.88 s ± 89 ms 1.88 s ± 18 ms -0.01 %
10000_development-mode_hmr + exec 685 ms ± 7.4 ms 686 ms ± 3.3 ms +0.14 %
10000_production-mode + exec 2.28 s ± 51 ms 2.35 s ± 124 ms +3.25 %
10000_production-mode_persistent-cold + exec 2.47 s ± 72 ms 2.52 s ± 92 ms +2.27 %
10000_production-mode_persistent-hot + exec 1.65 s ± 61 ms 1.67 s ± 63 ms +1.46 %
arco-pro_development-mode + exec 1.78 s ± 145 ms 1.72 s ± 111 ms -3.25 %
arco-pro_development-mode_hmr + exec 389 ms ± 2.1 ms 387 ms ± 1.9 ms -0.31 %
arco-pro_production-mode + exec 3.59 s ± 217 ms 3.69 s ± 147 ms +2.86 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.72 s ± 229 ms 3.7 s ± 208 ms -0.61 %
arco-pro_production-mode_persistent-cold + exec 3.85 s ± 164 ms 3.75 s ± 73 ms -2.62 %
arco-pro_production-mode_persistent-hot + exec 2.36 s ± 99 ms 2.41 s ± 88 ms +2.43 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.63 s ± 69 ms 3.68 s ± 183 ms +1.26 %
large-dyn-imports_development-mode + exec 2.09 s ± 24 ms 2.11 s ± 46 ms +1.13 %
large-dyn-imports_production-mode + exec 2.15 s ± 71 ms 2.17 s ± 76 ms +0.93 %
threejs_development-mode_10x + exec 1.54 s ± 25 ms 1.55 s ± 21 ms +1.16 %
threejs_development-mode_10x_hmr + exec 779 ms ± 7 ms 793 ms ± 33 ms +1.79 %
threejs_production-mode_10x + exec 5.2 s ± 32 ms 5.31 s ± 357 ms +2.05 %
threejs_production-mode_10x_persistent-cold + exec 5.27 s ± 65 ms 5.4 s ± 388 ms +2.41 %
threejs_production-mode_10x_persistent-hot + exec 4.53 s ± 220 ms 4.6 s ± 333 ms +1.55 %
10000_big_production-mode_disable-minimize + rss memory 8706 MiB ± 32.4 MiB 8712 MiB ± 135 MiB +0.07 %
10000_development-mode + rss memory 649 MiB ± 12.6 MiB 661 MiB ± 20.8 MiB +1.89 %
10000_development-mode_hmr + rss memory 1344 MiB ± 112 MiB 1285 MiB ± 179 MiB -4.37 %
10000_production-mode + rss memory 621 MiB ± 21.8 MiB 661 MiB ± 33.4 MiB +6.51 %
10000_production-mode_persistent-cold + rss memory 746 MiB ± 24.7 MiB 736 MiB ± 16.2 MiB -1.37 %
10000_production-mode_persistent-hot + rss memory 717 MiB ± 23.3 MiB 761 MiB ± 27.8 MiB +6.07 %
arco-pro_development-mode + rss memory 569 MiB ± 17.5 MiB 587 MiB ± 24.8 MiB +3.18 %
arco-pro_development-mode_hmr + rss memory 643 MiB ± 49.1 MiB 673 MiB ± 61.5 MiB +4.68 %
arco-pro_production-mode + rss memory 714 MiB ± 28.8 MiB 735 MiB ± 19.8 MiB +2.94 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 732 MiB ± 22.4 MiB 743 MiB ± 32.5 MiB +1.47 %
arco-pro_production-mode_persistent-cold + rss memory 853 MiB ± 45.5 MiB 835 MiB ± 35.3 MiB -2.06 %
arco-pro_production-mode_persistent-hot + rss memory 708 MiB ± 27.8 MiB 752 MiB ± 27.9 MiB +6.28 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 727 MiB ± 48.2 MiB 727 MiB ± 14.2 MiB -0.08 %
large-dyn-imports_development-mode + rss memory 644 MiB ± 5.26 MiB 645 MiB ± 12.9 MiB +0.23 %
large-dyn-imports_production-mode + rss memory 526 MiB ± 7.84 MiB 534 MiB ± 7.26 MiB +1.39 %
threejs_development-mode_10x + rss memory 553 MiB ± 16.3 MiB 549 MiB ± 31.7 MiB -0.67 %
threejs_development-mode_10x_hmr + rss memory 1145 MiB ± 140 MiB 1110 MiB ± 141 MiB -3.00 %
threejs_production-mode_10x + rss memory 828 MiB ± 50.8 MiB 833 MiB ± 34.4 MiB +0.63 %
threejs_production-mode_10x_persistent-cold + rss memory 960 MiB ± 14.5 MiB 952 MiB ± 31.7 MiB -0.80 %
threejs_production-mode_10x_persistent-hot + rss memory 877 MiB ± 54.8 MiB 861 MiB ± 61.5 MiB -1.91 %

Threshold exceeded: ["10000_big_production-mode_disable-minimize + exec"]

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on 1e74e33 Feb 11, 2025

Choose a reason for hiding this comment

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

📝 Ecosystem CI detail: Open

suite result
modernjs ❌ failure
rspress ✅ success
rslib ✅ success
rsbuild ❌ failure
rsdoctor ❌ failure
examples ✅ success
devserver ✅ success
nuxt ✅ success

Please sign in to comment.