Skip to content

Commit

Permalink
feat: 回退禁止用户配置不以 / 开头的 publicPath的变更
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanglanxiao.zlx committed Sep 18, 2023
1 parent edea548 commit 001c95f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
8 changes: 3 additions & 5 deletions crates/mako/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(
Expand Down
13 changes: 6 additions & 7 deletions crates/mako/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" => {
Expand All @@ -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();
Expand Down

0 comments on commit 001c95f

Please sign in to comment.