|
| 1 | +use std::{cmp::min, sync::LazyLock}; |
| 2 | + |
1 | 3 | use anyhow::{Context, Result, bail}; |
| 4 | +use qstring::QString; |
| 5 | +use regex::Regex; |
2 | 6 | use serde::{Deserialize, Serialize}; |
3 | 7 | use tracing::Instrument; |
4 | 8 | use turbo_rcstr::{RcStr, rcstr}; |
@@ -188,6 +192,16 @@ impl BrowserChunkingContextBuilder { |
188 | 192 | self |
189 | 193 | } |
190 | 194 |
|
| 195 | + pub fn filename(mut self, filename: RcStr) -> Self { |
| 196 | + self.chunking_context.filename = Some(filename); |
| 197 | + self |
| 198 | + } |
| 199 | + |
| 200 | + pub fn chunk_filename(mut self, chunk_filename: RcStr) -> Self { |
| 201 | + self.chunking_context.chunk_filename = Some(chunk_filename); |
| 202 | + self |
| 203 | + } |
| 204 | + |
191 | 205 | pub fn build(self) -> Vc<BrowserChunkingContext> { |
192 | 206 | BrowserChunkingContext::cell(self.chunking_context) |
193 | 207 | } |
@@ -256,6 +270,10 @@ pub struct BrowserChunkingContext { |
256 | 270 | export_usage: Option<ResolvedVc<ExportUsageInfo>>, |
257 | 271 | /// The chunking configs |
258 | 272 | chunking_configs: Vec<(ResolvedVc<Box<dyn ChunkType>>, ChunkingConfig)>, |
| 273 | + /// Evaluate chunk filename template |
| 274 | + filename: Option<RcStr>, |
| 275 | + /// Non evaluate chunk filename template |
| 276 | + chunk_filename: Option<RcStr>, |
259 | 277 | } |
260 | 278 |
|
261 | 279 | impl BrowserChunkingContext { |
@@ -297,6 +315,8 @@ impl BrowserChunkingContext { |
297 | 315 | module_id_strategy: ResolvedVc::upcast(DevModuleIdStrategy::new_resolved()), |
298 | 316 | export_usage: None, |
299 | 317 | chunking_configs: Default::default(), |
| 318 | + filename: Default::default(), |
| 319 | + chunk_filename: Default::default(), |
300 | 320 | }, |
301 | 321 | } |
302 | 322 | } |
@@ -460,40 +480,76 @@ impl ChunkingContext for BrowserChunkingContext { |
460 | 480 | extension.starts_with("."), |
461 | 481 | "`extension` should include the leading '.', got '{extension}'" |
462 | 482 | ); |
463 | | - let ChunkPathInfo { |
464 | | - chunk_root_path, |
465 | | - content_hashing, |
466 | | - root_path, |
467 | | - } = &*self.chunk_path_info().await?; |
468 | | - let name = match *content_hashing { |
469 | | - None => { |
470 | | - ident |
471 | | - .output_name(root_path.clone(), prefix, extension) |
472 | | - .owned() |
473 | | - .await? |
474 | | - } |
475 | | - Some(ContentHashing::Direct { length }) => { |
476 | | - let Some(asset) = asset else { |
477 | | - bail!("chunk_path requires an asset when content hashing is enabled"); |
478 | | - }; |
479 | | - let content = asset.content().await?; |
480 | | - if let AssetContent::File(file) = &*content { |
481 | | - let hash = hash_xxh3_hash64(&file.await?); |
482 | | - let length = length as usize; |
483 | | - if let Some(prefix) = prefix { |
484 | | - format!("{prefix}-{hash:0length$x}{extension}").into() |
485 | | - } else { |
486 | | - format!("{hash:0length$x}{extension}").into() |
| 483 | + |
| 484 | + let output_name = ident |
| 485 | + .output_name(self.root_path.clone(), prefix, extension.clone()) |
| 486 | + .owned() |
| 487 | + .await?; |
| 488 | + |
| 489 | + let mut filename = match asset { |
| 490 | + Some(asset) => { |
| 491 | + let ident = ident.await?; |
| 492 | + |
| 493 | + let mut evaluate = false; |
| 494 | + let mut dev_chunk_list = false; |
| 495 | + ident.modifiers.iter().for_each(|m| { |
| 496 | + if m.contains("evaluate") { |
| 497 | + evaluate = true; |
487 | 498 | } |
| 499 | + if m.contains("dev chunk list") { |
| 500 | + dev_chunk_list = true; |
| 501 | + } |
| 502 | + }); |
| 503 | + let query = QString::from(ident.query.as_str()); |
| 504 | + let name = if dev_chunk_list { |
| 505 | + output_name.as_str() |
488 | 506 | } else { |
489 | | - bail!( |
490 | | - "chunk_path requires an asset with file content when content hashing is \ |
491 | | - enabled" |
492 | | - ); |
| 507 | + query.get("name").unwrap_or(output_name.as_str()) |
| 508 | + }; |
| 509 | + |
| 510 | + let filename_template = if evaluate { |
| 511 | + &self.filename |
| 512 | + } else { |
| 513 | + &self.chunk_filename |
| 514 | + }; |
| 515 | + |
| 516 | + match filename_template { |
| 517 | + Some(filename) => { |
| 518 | + let mut filename = filename.to_string(); |
| 519 | + |
| 520 | + if match_name_placeholder(&filename) { |
| 521 | + filename = replace_name_placeholder(&filename, name); |
| 522 | + } |
| 523 | + |
| 524 | + if match_content_hash_placeholder(&filename) { |
| 525 | + let content = asset.content().await?; |
| 526 | + if let AssetContent::File(file) = &*content { |
| 527 | + let content_hash = hash_xxh3_hash64(&file.await?); |
| 528 | + filename = replace_content_hash_placeholder( |
| 529 | + &filename, |
| 530 | + &format!("{content_hash:016x}"), |
| 531 | + ); |
| 532 | + } else { |
| 533 | + bail!( |
| 534 | + "chunk_path requires an asset with file content when content \ |
| 535 | + hashing is enabled" |
| 536 | + ); |
| 537 | + } |
| 538 | + }; |
| 539 | + |
| 540 | + filename |
| 541 | + } |
| 542 | + None => name.to_string(), |
493 | 543 | } |
494 | 544 | } |
| 545 | + None => output_name.to_string(), |
495 | 546 | }; |
496 | | - Ok(chunk_root_path.join(&name)?.cell()) |
| 547 | + |
| 548 | + if !filename.ends_with(extension.as_str()) { |
| 549 | + filename.push_str(&extension); |
| 550 | + } |
| 551 | + |
| 552 | + self.chunk_root_path.join(&filename).map(|p| p.cell()) |
497 | 553 | } |
498 | 554 |
|
499 | 555 | #[turbo_tasks::function] |
@@ -820,3 +876,40 @@ struct ChunkPathInfo { |
820 | 876 | chunk_root_path: FileSystemPath, |
821 | 877 | content_hashing: Option<ContentHashing>, |
822 | 878 | } |
| 879 | + |
| 880 | +pub fn clean_separators(s: &str) -> String { |
| 881 | + static SEPARATOR_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r".*[/#?]").unwrap()); |
| 882 | + SEPARATOR_REGEX.replace_all(s, "").to_string() |
| 883 | +} |
| 884 | + |
| 885 | +static NAME_PLACEHOLDER_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\[name\]").unwrap()); |
| 886 | + |
| 887 | +pub fn match_name_placeholder(s: &str) -> bool { |
| 888 | + NAME_PLACEHOLDER_REGEX.is_match(s) |
| 889 | +} |
| 890 | + |
| 891 | +pub fn replace_name_placeholder(s: &str, name: &str) -> String { |
| 892 | + NAME_PLACEHOLDER_REGEX.replace_all(s, name).to_string() |
| 893 | +} |
| 894 | + |
| 895 | +static CONTENT_HASH_PLACEHOLDER_REGEX: LazyLock<Regex> = |
| 896 | + LazyLock::new(|| Regex::new(r"\[contenthash(?::(?P<len>\d+))?\]").unwrap()); |
| 897 | + |
| 898 | +pub fn match_content_hash_placeholder(s: &str) -> bool { |
| 899 | + CONTENT_HASH_PLACEHOLDER_REGEX.is_match(s) |
| 900 | +} |
| 901 | + |
| 902 | +pub fn replace_content_hash_placeholder(s: &str, hash: &str) -> String { |
| 903 | + CONTENT_HASH_PLACEHOLDER_REGEX |
| 904 | + .replace_all(s, |caps: ®ex::Captures| { |
| 905 | + let len = caps.name("len").map(|m| m.as_str()).unwrap_or(""); |
| 906 | + let len = if len.is_empty() { |
| 907 | + hash.len() |
| 908 | + } else { |
| 909 | + len.parse().unwrap_or(hash.len()) |
| 910 | + }; |
| 911 | + let len = min(len, hash.len()); |
| 912 | + hash[..len].to_string() |
| 913 | + }) |
| 914 | + .to_string() |
| 915 | +} |
0 commit comments