Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
shulaoda committed Oct 8, 2024
1 parent bf8a64f commit a429391
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
13 changes: 9 additions & 4 deletions crates/rspack_core/src/options/filename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ pub static RUNTIME_PLACEHOLDER: LazyLock<Regex> =
pub static URL_PLACEHOLDER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[url\]").expect("Should generate regex"));

pub static HASH_PLACEHOLDER: &str = "[hash]";
pub static FULL_HASH_PLACEHOLDER: &str = "[fullhash]";
pub static CHUNK_HASH_PLACEHOLDER: &str = "[chunkhash]";
pub static CONTENT_HASH_PLACEHOLDER: &str = "[contenthash]";

static DATA_URI_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^data:([^;,]+)").expect("Invalid regex"));

Expand Down Expand Up @@ -177,7 +182,7 @@ fn hash_len(hash: &str, len: Option<usize>) -> usize {
}

pub fn has_hash_placeholder(template: &str) -> bool {
for key in ["[hash]", "[fullhash]"] {
for key in [HASH_PLACEHOLDER, FULL_HASH_PLACEHOLDER] {
let offset = key.len() - 1;
if let Some(start) = template.find(&key[..offset]) {
if template[start + offset..].find(']').is_some() {
Expand Down Expand Up @@ -288,7 +293,7 @@ fn render_template(
asset_info.version = content_hash.to_string();
}
t = t.map(|t| {
replace_all_hash_pattern(t, "[contenthash]", |len| {
replace_all_hash_pattern(t, CONTENT_HASH_PLACEHOLDER, |len| {
let hash: &str = &content_hash[..hash_len(content_hash, len)];
if let Some(asset_info) = asset_info.as_mut() {
asset_info.set_immutable(Some(true));
Expand All @@ -300,7 +305,7 @@ fn render_template(
});
}
if let Some(hash) = options.hash {
for key in ["[hash]", "[fullhash]"] {
for key in [HASH_PLACEHOLDER, FULL_HASH_PLACEHOLDER] {
t = t.map(|t| {
replace_all_hash_pattern(t, key, |len| {
let hash = &hash[..hash_len(hash, len)];
Expand All @@ -326,7 +331,7 @@ fn render_template(
if let Some(d) = chunk.rendered_hash.as_ref() {
t = t.map(|t| {
let hash = &**d;
replace_all_hash_pattern(t, "[chunkhash]", |len| {
replace_all_hash_pattern(t, CHUNK_HASH_PLACEHOLDER, |len| {
let hash: &str = &hash[..hash_len(hash, len)];
if let Some(asset_info) = asset_info.as_mut() {
asset_info.set_immutable(Some(true));
Expand Down
11 changes: 10 additions & 1 deletion crates/rspack_core/src/utils/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use rustc_hash::FxHashMap as HashMap;
use rustc_hash::FxHashSet as HashSet;

use crate::{merge_runtime, EntryData, EntryOptions, Filename, RuntimeSpec};
use crate::{
CHUNK_HASH_PLACEHOLDER, CONTENT_HASH_PLACEHOLDER, FULL_HASH_PLACEHOLDER, HASH_PLACEHOLDER,
};

pub fn get_entry_runtime(
name: &str,
Expand Down Expand Up @@ -63,6 +66,7 @@ pub fn extract_hash_pattern(pattern: &str, key: &str) -> Option<ExtractedHashPat
})
}

/// Replace all `[hash]` or `[hash:8]` in the pattern
pub fn replace_all_hash_pattern<'a, F, S>(
pattern: &'a str,
key: &'a str,
Expand Down Expand Up @@ -127,7 +131,12 @@ pub fn get_filename_without_hash_length<F: Clone>(
return (filename.clone(), hash_len_map);
};
let mut template = template.to_string();
for key in ["[hash]", "[fullhash]", "[chunkhash]", "[contenthash]"] {
for key in [
HASH_PLACEHOLDER,
FULL_HASH_PLACEHOLDER,
CHUNK_HASH_PLACEHOLDER,
CONTENT_HASH_PLACEHOLDER,
] {
if let Some(p) = extract_hash_pattern(&template, key) {
if let Some(hash_len) = p.len {
hash_len_map.insert((*key).to_string(), hash_len);
Expand Down

0 comments on commit a429391

Please sign in to comment.