Skip to content

Commit 1ca6b68

Browse files
authored
fix: correct favicon path when output.publicPath is set to auto (#8415)
1 parent 3c74282 commit 1ca6b68

File tree

7 files changed

+60
-14
lines changed

7 files changed

+60
-14
lines changed

crates/rspack_plugin_html/src/asset.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,17 @@ impl HtmlPluginAssets {
111111
favicon_relative_path.to_string_lossy().to_string().as_str(),
112112
));
113113

114-
let fake_html_file_name = compilation
115-
.get_path(
116-
html_file_name,
117-
PathData::default().filename(output_path.as_str()),
118-
)
119-
.always_ok();
120-
121114
if favicon_path.to_str().unwrap_or_default().is_empty() {
122-
favicon_path = compilation
123-
.options
124-
.output
125-
.path
126-
.as_std_path()
127-
.join(favicon_relative_path)
128-
.relative(PathBuf::from(fake_html_file_name).join(".."));
115+
let fake_html_file_name = compilation
116+
.get_path(
117+
html_file_name,
118+
PathData::default().filename(output_path.as_str()),
119+
)
120+
.always_ok();
121+
let output_path = compilation.options.output.path.as_std_path();
122+
favicon_path = output_path
123+
.relative(output_path.join(fake_html_file_name).join(".."))
124+
.join(favicon_relative_path);
129125
} else {
130126
favicon_path.push(favicon_relative_path);
131127
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
it("html favicon with absolute path and public path", () => {
5+
const faviconPath = path.join(__dirname, "./favicon.ico");
6+
expect(fs.existsSync(faviconPath)).toBe(true);
7+
8+
const htmlPath = path.join(__dirname, "./index.html");
9+
const htmlContent = fs.readFileSync(htmlPath, "utf-8");
10+
expect(htmlContent).toContain('<link href="favicon.ico" rel="icon">');
11+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const path = require("path");
2+
const { rspack } = require("@rspack/core");
3+
4+
/** @type {import("@rspack/core").Configuration} */
5+
module.exports = {
6+
output: {
7+
publicPath: "auto",
8+
},
9+
plugins: [
10+
new rspack.HtmlRspackPlugin({
11+
favicon: path.resolve(__dirname, "favicon.ico")
12+
})
13+
]
14+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
it("html favicon with absolute path and public path", () => {
5+
const faviconPath = path.join(__dirname, "./favicon.ico");
6+
expect(fs.existsSync(faviconPath)).toBe(true);
7+
8+
const htmlPath = path.join(__dirname, "./index.html");
9+
const htmlContent = fs.readFileSync(htmlPath, "utf-8");
10+
expect(htmlContent).toContain('<link href="favicon.ico" rel="icon">');
11+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const path = require("path");
2+
const { rspack } = require("@rspack/core");
3+
4+
/** @type {import("@rspack/core").Configuration} */
5+
module.exports = {
6+
output: {
7+
publicPath: "auto",
8+
},
9+
plugins: [
10+
new rspack.HtmlRspackPlugin({
11+
favicon: "favicon.ico"
12+
})
13+
]
14+
};

0 commit comments

Comments
 (0)