Skip to content

Commit 2c75881

Browse files
committed
feat(html): improve template parameters
1 parent dfc7825 commit 2c75881

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

crates/rspack_plugin_html/src/visitors/asset.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl VisitMut for AssetWriter<'_> {
9898
span: DUMMY_SP,
9999
data: Atom::from(
100100
tag
101-
.content
101+
.inner_html
102102
.as_ref()
103103
.unwrap_or_else(|| panic!("should have title content"))
104104
.to_string(),

crates/rspack_plugin_html/src/visitors/tag.rs

+58-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
use std::collections::HashMap;
1+
use std::{collections::HashMap, fmt};
22

33
use itertools::Itertools;
4-
use serde::{Deserialize, Serialize};
4+
use serde::{
5+
de::{MapAccess, Visitor},
6+
ser::SerializeMap,
7+
Deserialize, Deserializer, Serialize, Serializer,
8+
};
59

610
use super::asset::HtmlPluginAttribute;
711
use crate::config::{HtmlInject, HtmlRspackPluginBaseOptions, HtmlScriptLoading};
@@ -10,14 +14,64 @@ use crate::config::{HtmlInject, HtmlRspackPluginBaseOptions, HtmlScriptLoading};
1014
#[serde(rename_all = "camelCase")]
1115
pub struct HTMLPluginTag {
1216
pub tag_name: String,
17+
#[serde(
18+
serialize_with = "serialize_attributes",
19+
deserialize_with = "deserialize_attributes"
20+
)]
1321
pub attributes: Vec<HtmlPluginAttribute>,
1422
pub void_tag: bool,
15-
pub content: Option<String>,
23+
#[serde(rename = "innerHTML")]
24+
pub inner_html: Option<String>,
1625
// `head`, `body`, `false`
1726
#[serde(skip)]
1827
pub append_to: HtmlInject,
1928
}
2029

30+
fn serialize_attributes<S>(x: &Vec<HtmlPluginAttribute>, s: S) -> Result<S::Ok, S::Error>
31+
where
32+
S: Serializer,
33+
{
34+
let mut map = s.serialize_map(Some(x.len()))?;
35+
for attr in x {
36+
let attr_value = attr.attr_value.to_owned().unwrap_or("true".to_string());
37+
map.serialize_entry(&attr.attr_name, &attr_value)?;
38+
}
39+
map.end()
40+
}
41+
42+
fn deserialize_attributes<'de, D>(d: D) -> Result<Vec<HtmlPluginAttribute>, D::Error>
43+
where
44+
D: Deserializer<'de>,
45+
{
46+
struct DataVisitor;
47+
48+
impl<'de> Visitor<'de> for DataVisitor {
49+
type Value = Vec<HtmlPluginAttribute>;
50+
51+
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
52+
formatter.write_str("html attributes")
53+
}
54+
55+
fn visit_map<A>(self, mut access: A) -> Result<Self::Value, A::Error>
56+
where
57+
A: MapAccess<'de>,
58+
{
59+
let mut res = vec![];
60+
61+
while let Some((k, v)) = access.next_entry::<String, Option<String>>()? {
62+
res.push(HtmlPluginAttribute {
63+
attr_name: k,
64+
attr_value: v.filter(|value| value != "true"),
65+
});
66+
}
67+
68+
Ok(res)
69+
}
70+
}
71+
72+
d.deserialize_map(DataVisitor)
73+
}
74+
2175
impl HTMLPluginTag {
2276
pub fn create_style(href: &str, append_to: HtmlInject) -> HTMLPluginTag {
2377
HTMLPluginTag {
@@ -112,7 +166,7 @@ impl HTMLPluginTag {
112166
HTMLPluginTag {
113167
tag_name: "title".to_string(),
114168
void_tag: true,
115-
content: Some(title.to_string()),
169+
inner_html: Some(title.to_string()),
116170
..Default::default()
117171
}
118172
}

crates/rspack_plugin_html/src/visitors/utils.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,17 @@ pub fn html_tag_object_to_string(tag: &HTMLPluginTag) -> String {
9191
"<{} {}{}>{}{}",
9292
tag.tag_name,
9393
attributes.join(" "),
94-
if tag.void_tag && tag.content.is_none() {
94+
if tag.void_tag && tag.inner_html.is_none() {
9595
"/"
9696
} else {
9797
""
9898
},
99-
if let Some(content) = &tag.content {
100-
content
99+
if let Some(inner_html) = &tag.inner_html {
100+
inner_html
101101
} else {
102102
""
103103
},
104-
if !tag.void_tag || tag.content.is_some() {
104+
if !tag.void_tag || tag.inner_html.is_some() {
105105
format!("</{}>", tag.tag_name)
106106
} else {
107107
String::new()

0 commit comments

Comments
 (0)