diff --git a/crates/mako/src/config.rs b/crates/mako/src/config.rs index 8b361f94b..938633973 100644 --- a/crates/mako/src/config.rs +++ b/crates/mako/src/config.rs @@ -220,10 +220,8 @@ impl Config { .entry("NODE_ENV".to_string()) .or_insert_with(|| serde_json::Value::String(mode)); - if config.public_path != "runtime" - && (!config.public_path.ends_with('/') || !config.public_path.starts_with('/')) - { - panic!("publicPath must end with '/' and start with '/' or be 'runtime'"); + if config.public_path != "runtime" && !config.public_path.ends_with('/') { + panic!("public_path must end with '/' or be 'runtime'"); } // let entry_length = cc.entry.len(); @@ -295,7 +293,7 @@ mod tests { } #[test] - #[should_panic(expected = "publicPath must end with '/' and start with '/' or be 'runtime'")] + #[should_panic(expected = "public_path must end with '/' or be 'runtime'")] fn test_config_invalid_public_path() { let current_dir = std::env::current_dir().unwrap(); Config::new( diff --git a/crates/mako/src/dev.rs b/crates/mako/src/dev.rs index 81ceaf650..208e820be 100644 --- a/crates/mako/src/dev.rs +++ b/crates/mako/src/dev.rs @@ -84,10 +84,10 @@ impl DevServer { for_fn.context.root.join("node_modules/.mako/hot_update"), ); + // 去除 publicPath 头尾 / let public_path = for_fn.context.config.public_path.clone(); - let public_path_without_prefix_as_str = public_path - .strip_prefix('/') - .unwrap_or(public_path.as_str()); + let public_path_without_fix = + public_path.trim_start_matches('/').trim_end_matches('/'); match path { "__/hmr-ws" => { @@ -112,11 +112,10 @@ impl DevServer { ) } } - _ if path.starts_with(public_path_without_prefix_as_str) => { + _ if path.starts_with(public_path_without_fix) => { // 如果用户设置了 public_path,修改一下原始 req,手动复制 req 担心掉属性 - if public_path.as_str() != "/" { - let public_path_re = - Regex::new(public_path_without_prefix_as_str).unwrap(); + if !public_path.is_empty() { + let public_path_re = Regex::new(public_path_without_fix).unwrap(); let uri_str = public_path_re .replacen(req.uri().to_string().as_str(), 1, "") .to_string();