Skip to content

Commit 47468a4

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

File tree

7 files changed

+115
-38
lines changed

7 files changed

+115
-38
lines changed

crates/rspack_plugin_html/src/plugin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
377377
}
378378

379379
if !matches!(self.config.inject, HtmlInject::False) {
380-
let mut visitor = AssetWriter::new(config, &tags);
380+
let mut visitor = AssetWriter::new(&tags);
381381
current_ast.visit_mut_with(&mut visitor);
382382
}
383383

crates/rspack_plugin_html/src/visitors/asset.rs

+4-37
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use serde::{Deserialize, Serialize};
22
use swc_core::{common::DUMMY_SP, ecma::atoms::Atom};
3-
use swc_html::ast::{Child, Element, Namespace, Text};
3+
use swc_html::ast::{Child, Element, Text};
44
use swc_html::visit::{VisitMut, VisitMutWith};
55

66
use super::tag::HTMLPluginTag;
77
use super::utils::create_element;
8-
use crate::config::{HtmlInject, HtmlRspackPluginOptions};
8+
use crate::config::HtmlInject;
99

1010
// attributes are presented as plain string.
1111
// namespace is not supported currently.
@@ -19,13 +19,12 @@ pub struct HtmlPluginAttribute {
1919

2020
#[derive(Debug)]
2121
pub struct AssetWriter<'a> {
22-
config: &'a HtmlRspackPluginOptions,
2322
head_tags: Vec<&'a HTMLPluginTag>,
2423
body_tags: Vec<&'a HTMLPluginTag>,
2524
}
2625

2726
impl<'a> AssetWriter<'a> {
28-
pub fn new(config: &'a HtmlRspackPluginOptions, tags: &'a [HTMLPluginTag]) -> AssetWriter<'a> {
27+
pub fn new(tags: &'a [HTMLPluginTag]) -> AssetWriter<'a> {
2928
let mut head_tags: Vec<&HTMLPluginTag> = vec![];
3029
let mut body_tags: Vec<&HTMLPluginTag> = vec![];
3130
for ele in tags.iter() {
@@ -40,7 +39,6 @@ impl<'a> AssetWriter<'a> {
4039
}
4140
}
4241
AssetWriter {
43-
config,
4442
head_tags,
4543
body_tags,
4644
}
@@ -54,38 +52,6 @@ impl VisitMut for AssetWriter<'_> {
5452

5553
match &*n.tag_name {
5654
"head" => {
57-
// add title
58-
if let Some(title) = &self.config.title {
59-
let title_ele = n.children.iter_mut().find(|child| {
60-
if let Child::Element(ele) = child {
61-
return ele.tag_name.eq("title");
62-
}
63-
false
64-
});
65-
66-
if let Some(Child::Element(title_ele)) = title_ele {
67-
title_ele.children = vec![Child::Text(Text {
68-
span: DUMMY_SP,
69-
data: Atom::from(title.as_str()),
70-
raw: None,
71-
})];
72-
} else {
73-
n.children.push(Child::Element(Element {
74-
tag_name: Atom::from("title"),
75-
children: vec![Child::Text(Text {
76-
span: DUMMY_SP,
77-
data: Atom::from(title.as_str()),
78-
raw: None,
79-
})],
80-
is_self_closing: false,
81-
namespace: Namespace::HTML,
82-
span: DUMMY_SP,
83-
attributes: vec![],
84-
content: None,
85-
}));
86-
}
87-
}
88-
8955
for tag in head_tags.iter() {
9056
if tag.tag_name == "title" {
9157
if let Some(Child::Element(title_ele)) = n.children.iter_mut().find(|child| {
@@ -105,6 +71,7 @@ impl VisitMut for AssetWriter<'_> {
10571
),
10672
raw: None,
10773
})];
74+
continue;
10875
}
10976
}
11077

packages/rspack-test-tools/tests/configCases/builtins/html-template-parameters/index.css

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en" dir="ltr">
3+
4+
<head>
5+
<%= toHtml(htmlRspackPlugin.tags.headTags) %>
6+
7+
<% for tag in htmlRspackPlugin.tags.headTags { %>
8+
<% if tag.tagName=="script" { %>
9+
<%= toHtml(tag) %>
10+
<% } %>
11+
<% } %>
12+
</head>
13+
14+
<body>
15+
<div>favicon: "<%= htmlRspackPlugin.files.favicon %>"</div>
16+
<div>publicpath: "<%= htmlRspackPlugin.files.publicPath %>"</div>
17+
<div>scripts: "<% for script in htmlRspackPlugin.files.js { %><%= script %><% } %>"</div>
18+
<div>styles: "<% for style in htmlRspackPlugin.files.css { %><%= style %><% } %>"</div>
19+
<div>config title: "<%= htmlRspackPlugin.options.title %>"</div>
20+
<div>rspack config publicpath: "<%= rspackConfig.output.publicPath %>"</div>
21+
<div>rspack config mode: "<%= rspackConfig.mode %>"</div>
22+
</body>
23+
24+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require('./index.css');
2+
const fs = require("fs");
3+
const path = require("path");
4+
5+
const htmlPath = path.join(__dirname, "./index.html");
6+
const htmlContent = fs.readFileSync(htmlPath, "utf-8");
7+
8+
it("should inject title", () => {
9+
expect(htmlContent).toContain("<title>i am title</title>");
10+
});
11+
12+
it("should have only one title", () => {
13+
expect(htmlContent.split("<title>").length - 1).toBe(1);
14+
});
15+
16+
it("should inject htmlRspackPlugin.tags.headTags", () => {
17+
expect(htmlContent).toContain(`<title>i am title</title><link rel="icon" href="http://cdn.com/favicon.ico"><meta content="meta-value" name="meta-name"><script defer src="http://cdn.com/bundle0.js"></script><link href="http://cdn.com/main.css" rel="stylesheet">`);
18+
});
19+
20+
it("should inject filtered htmlRspackPlugin.tags.headTags", () => {
21+
expect(htmlContent.split(`<script defer src="http://cdn.com/bundle0.js"></script>`).length - 1).toBe(2);
22+
});
23+
24+
it("should inject htmlRspackPlugin.files.publicPath", () => {
25+
expect(htmlContent).toContain(`publicpath: "http://cdn.com/"`);
26+
});
27+
28+
it("should inject htmlRspackPlugin.files.favicon", () => {
29+
expect(htmlContent).toContain(`favicon: "http://cdn.com/favicon.ico"`);
30+
});
31+
32+
it("should inject htmlRspackPlugin.files.js", () => {
33+
expect(htmlContent).toContain(`scripts: "http://cdn.com/bundle0.js"`);
34+
});
35+
36+
it("should inject htmlRspackPlugin.files.css", () => {
37+
expect(htmlContent).toContain(`styles: "http://cdn.com/main.css"`);
38+
});
39+
40+
it("should inject htmlRspackPlugin.options.title", () => {
41+
expect(htmlContent).toContain(`config title: "i am title"`);
42+
});
43+
44+
it("should inject rspackConfig.mode", () => {
45+
expect(htmlContent).toContain(`rspack config mode: "production"`);
46+
});
47+
48+
it("should inject rspackConfig.output.publicPath", () => {
49+
expect(htmlContent).toContain(`rspack config publicpath: "http://cdn.com/"`);
50+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const { rspack } = require("@rspack/core");
2+
const { CssExtractRspackPlugin, HtmlRspackPlugin } = rspack;
3+
4+
/** @type {import("@rspack/core").Configuration} */
5+
module.exports = {
6+
output: {
7+
publicPath: "http://cdn.com/"
8+
},
9+
module: {
10+
rules: [
11+
{
12+
test: /\.css$/,
13+
use: [CssExtractRspackPlugin.loader, "css-loader"],
14+
},
15+
],
16+
},
17+
experiments: {
18+
css: false,
19+
},
20+
plugins: [
21+
new CssExtractRspackPlugin(),
22+
new HtmlRspackPlugin({
23+
minify: false,
24+
template: "./index.html",
25+
title: "i am title",
26+
meta: {
27+
"meta-name": "meta-value"
28+
},
29+
inject: false,
30+
favicon: "./favicon.ico",
31+
templateParameters: {
32+
foo: "bar",
33+
},
34+
})
35+
]
36+
};

0 commit comments

Comments
 (0)