|
| 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};
|
@@ -183,6 +187,16 @@ impl BrowserChunkingContextBuilder {
|
183 | 187 | self
|
184 | 188 | }
|
185 | 189 |
|
| 190 | + pub fn filename(mut self, filename: RcStr) -> Self { |
| 191 | + self.chunking_context.filename = Some(filename); |
| 192 | + self |
| 193 | + } |
| 194 | + |
| 195 | + pub fn chunk_filename(mut self, chunk_filename: RcStr) -> Self { |
| 196 | + self.chunking_context.chunk_filename = Some(chunk_filename); |
| 197 | + self |
| 198 | + } |
| 199 | + |
186 | 200 | pub fn build(self) -> Vc<BrowserChunkingContext> {
|
187 | 201 | BrowserChunkingContext::cell(self.chunking_context)
|
188 | 202 | }
|
@@ -249,6 +263,10 @@ pub struct BrowserChunkingContext {
|
249 | 263 | export_usage: Option<ResolvedVc<ExportUsageInfo>>,
|
250 | 264 | /// The chunking configs
|
251 | 265 | chunking_configs: Vec<(ResolvedVc<Box<dyn ChunkType>>, ChunkingConfig)>,
|
| 266 | + /// Evaluate chunk filename template |
| 267 | + filename: Option<RcStr>, |
| 268 | + /// Non evaluate chunk filename template |
| 269 | + chunk_filename: Option<RcStr>, |
252 | 270 | }
|
253 | 271 |
|
254 | 272 | impl BrowserChunkingContext {
|
@@ -289,6 +307,8 @@ impl BrowserChunkingContext {
|
289 | 307 | module_id_strategy: ResolvedVc::upcast(DevModuleIdStrategy::new_resolved()),
|
290 | 308 | export_usage: None,
|
291 | 309 | chunking_configs: Default::default(),
|
| 310 | + filename: Default::default(), |
| 311 | + chunk_filename: Default::default(), |
292 | 312 | },
|
293 | 313 | }
|
294 | 314 | }
|
@@ -438,36 +458,76 @@ impl ChunkingContext for BrowserChunkingContext {
|
438 | 458 | extension.starts_with("."),
|
439 | 459 | "`extension` should include the leading '.', got '{extension}'"
|
440 | 460 | );
|
441 |
| - let root_path = self.chunk_root_path.clone(); |
442 |
| - let name = match self.content_hashing { |
443 |
| - None => { |
444 |
| - ident |
445 |
| - .output_name(self.root_path.clone(), prefix, extension) |
446 |
| - .owned() |
447 |
| - .await? |
448 |
| - } |
449 |
| - Some(ContentHashing::Direct { length }) => { |
450 |
| - let Some(asset) = asset else { |
451 |
| - bail!("chunk_path requires an asset when content hashing is enabled"); |
452 |
| - }; |
453 |
| - let content = asset.content().await?; |
454 |
| - if let AssetContent::File(file) = &*content { |
455 |
| - let hash = hash_xxh3_hash64(&file.await?); |
456 |
| - let length = length as usize; |
457 |
| - if let Some(prefix) = prefix { |
458 |
| - format!("{prefix}-{hash:0length$x}{extension}").into() |
459 |
| - } else { |
460 |
| - format!("{hash:0length$x}{extension}").into() |
| 461 | + |
| 462 | + let output_name = ident |
| 463 | + .output_name(self.root_path.clone(), prefix, extension.clone()) |
| 464 | + .owned() |
| 465 | + .await?; |
| 466 | + |
| 467 | + let mut filename = match asset { |
| 468 | + Some(asset) => { |
| 469 | + let ident = ident.await?; |
| 470 | + |
| 471 | + let mut evaluate = false; |
| 472 | + let mut dev_chunk_list = false; |
| 473 | + ident.modifiers.iter().for_each(|m| { |
| 474 | + if m.contains("evaluate") { |
| 475 | + evaluate = true; |
461 | 476 | }
|
| 477 | + if m.contains("dev chunk list") { |
| 478 | + dev_chunk_list = true; |
| 479 | + } |
| 480 | + }); |
| 481 | + let query = QString::from(ident.query.as_str()); |
| 482 | + let name = if dev_chunk_list { |
| 483 | + output_name.as_str() |
462 | 484 | } else {
|
463 |
| - bail!( |
464 |
| - "chunk_path requires an asset with file content when content hashing is \ |
465 |
| - enabled" |
466 |
| - ); |
| 485 | + query.get("name").unwrap_or(output_name.as_str()) |
| 486 | + }; |
| 487 | + |
| 488 | + let filename_template = if evaluate { |
| 489 | + &self.filename |
| 490 | + } else { |
| 491 | + &self.chunk_filename |
| 492 | + }; |
| 493 | + |
| 494 | + match filename_template { |
| 495 | + Some(filename) => { |
| 496 | + let mut filename = filename.to_string(); |
| 497 | + |
| 498 | + if match_name_placeholder(&filename) { |
| 499 | + filename = replace_name_placeholder(&filename, name); |
| 500 | + } |
| 501 | + |
| 502 | + if match_content_hash_placeholder(&filename) { |
| 503 | + let content = asset.content().await?; |
| 504 | + if let AssetContent::File(file) = &*content { |
| 505 | + let content_hash = hash_xxh3_hash64(&file.await?); |
| 506 | + filename = replace_content_hash_placeholder( |
| 507 | + &filename, |
| 508 | + &format!("{content_hash:016x}"), |
| 509 | + ); |
| 510 | + } else { |
| 511 | + bail!( |
| 512 | + "chunk_path requires an asset with file content when content \ |
| 513 | + hashing is enabled" |
| 514 | + ); |
| 515 | + } |
| 516 | + }; |
| 517 | + |
| 518 | + filename |
| 519 | + } |
| 520 | + None => name.to_string(), |
467 | 521 | }
|
468 | 522 | }
|
| 523 | + None => output_name.to_string(), |
469 | 524 | };
|
470 |
| - Ok(root_path.join(&name)?.cell()) |
| 525 | + |
| 526 | + if !filename.ends_with(extension.as_str()) { |
| 527 | + filename.push_str(&extension); |
| 528 | + } |
| 529 | + |
| 530 | + self.chunk_root_path.join(&filename).map(|p| p.cell()) |
471 | 531 | }
|
472 | 532 |
|
473 | 533 | #[turbo_tasks::function]
|
@@ -766,3 +826,40 @@ impl ChunkingContext for BrowserChunkingContext {
|
766 | 826 | }
|
767 | 827 | }
|
768 | 828 | }
|
| 829 | + |
| 830 | +pub fn clean_separators(s: &str) -> String { |
| 831 | + static SEPARATOR_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r".*[/#?]").unwrap()); |
| 832 | + SEPARATOR_REGEX.replace_all(s, "").to_string() |
| 833 | +} |
| 834 | + |
| 835 | +static NAME_PLACEHOLDER_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\[name\]").unwrap()); |
| 836 | + |
| 837 | +pub fn match_name_placeholder(s: &str) -> bool { |
| 838 | + NAME_PLACEHOLDER_REGEX.is_match(s) |
| 839 | +} |
| 840 | + |
| 841 | +pub fn replace_name_placeholder(s: &str, name: &str) -> String { |
| 842 | + NAME_PLACEHOLDER_REGEX.replace_all(s, name).to_string() |
| 843 | +} |
| 844 | + |
| 845 | +static CONTENT_HASH_PLACEHOLDER_REGEX: LazyLock<Regex> = |
| 846 | + LazyLock::new(|| Regex::new(r"\[contenthash(?::(?P<len>\d+))?\]").unwrap()); |
| 847 | + |
| 848 | +pub fn match_content_hash_placeholder(s: &str) -> bool { |
| 849 | + CONTENT_HASH_PLACEHOLDER_REGEX.is_match(s) |
| 850 | +} |
| 851 | + |
| 852 | +pub fn replace_content_hash_placeholder(s: &str, hash: &str) -> String { |
| 853 | + CONTENT_HASH_PLACEHOLDER_REGEX |
| 854 | + .replace_all(s, |caps: ®ex::Captures| { |
| 855 | + let len = caps.name("len").map(|m| m.as_str()).unwrap_or(""); |
| 856 | + let len = if len.is_empty() { |
| 857 | + hash.len() |
| 858 | + } else { |
| 859 | + len.parse().unwrap_or(hash.len()) |
| 860 | + }; |
| 861 | + let len = min(len, hash.len()); |
| 862 | + hash[..len].to_string() |
| 863 | + }) |
| 864 | + .to_string() |
| 865 | +} |
0 commit comments