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

fix: remove unnecessary path-to-URL conversion in html plugin #9348

Merged
merged 1 commit into from
Feb 19, 2025
Merged
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
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion crates/rspack_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ swc_core = { workspace = true, features = [
swc_node_comments = { workspace = true }
tokio = { workspace = true, features = ["rt", "rt-multi-thread", "macros", "test-util", "parking_lot"] }
tracing = { workspace = true }
url = { workspace = true }
ustr = { workspace = true }

[dev-dependencies]
Expand Down
15 changes: 0 additions & 15 deletions crates/rspack_core/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,6 @@ pub use self::source::*;
pub use self::template::*;
pub use self::to_path::to_path;

pub fn parse_to_url(url: &str) -> url::Url {
if !url.contains(':') {
let mut construct_string = String::with_capacity("specifier:".len() + url.len());
construct_string += "specifier:";
construct_string += url;
url::Url::parse(&construct_string).unwrap_or_else(|_| {
panic!("Invalid specifier: {url}, please use a valid specifier or a valid url")
})
} else {
url::Url::parse(url).unwrap_or_else(|_| {
panic!("Invalid specifier: {url}, please use a valid specifier or a valid url")
})
}
}

/// join string component in a more human readable way
/// e.g.
/// ```
Expand Down
4 changes: 1 addition & 3 deletions crates/rspack_plugin_html/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use cow_utils::CowUtils;
use itertools::Itertools;
use rayon::prelude::*;
use rspack_core::{
parse_to_url,
rspack_sources::{RawBufferSource, RawStringSource, SourceExt},
AssetInfo, Compilation, CompilationAsset, Filename, NoFilenameFn, PathData,
};
Expand Down Expand Up @@ -328,14 +327,13 @@ pub fn create_favicon_asset(
config: &HtmlRspackPluginOptions,
compilation: &Compilation,
) -> Result<(String, CompilationAsset), miette::Error> {
let url = parse_to_url(favicon);
let favicon_file_path = PathBuf::from(config.get_relative_path(compilation, favicon))
.file_name()
.expect("Should have favicon file name")
.to_string_lossy()
.to_string();

let resolved_favicon = AsRef::<Path>::as_ref(&compilation.options.context).join(url.path());
let resolved_favicon = AsRef::<Path>::as_ref(&compilation.options.context).join(favicon);

fs::read(resolved_favicon)
.context(format!(
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_plugin_html/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{fs, path::PathBuf};

use anyhow::Context;
use itertools::Itertools;
use rspack_core::{parse_to_url, Compilation, CrossOriginLoading, Mode};
use rspack_core::{Compilation, CrossOriginLoading, Mode};
use rspack_dojang::{dojang::DojangOptions, Dojang, Operand};
use rspack_error::{miette, AnyhowError};
use rspack_paths::AssertUtf8;
Expand Down Expand Up @@ -41,7 +41,7 @@ impl HtmlTemplate {
} else {
TemplateRender::Template(content.clone())
},
url: parse_to_url("template_content.html").path().to_string(),
url: "template_content.html".to_string(),
filename: "template_content.html".to_string(),
file_dependencies: vec![],
parameters: None,
Expand Down Expand Up @@ -97,7 +97,7 @@ impl HtmlTemplate {
} else {
Ok(Self {
render: TemplateRender::Template(default_template().to_owned()),
url: parse_to_url("default.html").path().to_string(),
url: "default.html".to_string(),
filename: "default.html".to_string(),
file_dependencies: vec![],
parameters: None,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const fs = require("fs");
const path = require("path");

it("html favicon with absolute path", () => {
const faviconPath = path.join(__dirname, "./favicon-图标.ico");
expect(fs.existsSync(faviconPath)).toBe(true);

const htmlPath = path.join(__dirname, "./index.html");
const htmlContent = fs.readFileSync(htmlPath, "utf-8");
expect(htmlContent).toContain('<link href="/favicon-图标.ico" rel="icon">');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const path = require("path");
const { rspack } = require("@rspack/core");

/** @type {import("@rspack/core").Configuration} */
module.exports = {
plugins: [
new rspack.HtmlRspackPlugin({
publicPath: "/",
favicon: "./资源/favicon-图标.ico"
})
]
};
Binary file not shown.
Loading