Skip to content

Commit e9260a8

Browse files
authored
add FixedStaticAsset and TextContentSourceAsset (vercel/turborepo#4692)
### Description needed for metadata support in next.js (#48823)
1 parent dfcdf84 commit e9260a8

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

crates/turbopack-ecmascript/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod path_visitor;
1717
pub(crate) mod references;
1818
pub mod resolve;
1919
pub(crate) mod special_cases;
20+
pub mod text;
2021
pub(crate) mod transform;
2122
pub mod tree_shake;
2223
pub mod typescript;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use anyhow::Result;
2+
use turbo_tasks::primitives::StringVc;
3+
use turbo_tasks_fs::FileContent;
4+
use turbopack_core::{
5+
asset::{Asset, AssetContentVc, AssetVc},
6+
ident::AssetIdentVc,
7+
};
8+
9+
use crate::utils::StringifyJs;
10+
11+
#[turbo_tasks::function]
12+
fn modifier() -> StringVc {
13+
StringVc::cell("text content".to_string())
14+
}
15+
16+
/// A source asset that exports the string content of an asset as the default
17+
/// export of a JS module.
18+
#[turbo_tasks::value]
19+
pub struct TextContentSourceAsset {
20+
pub source: AssetVc,
21+
}
22+
23+
#[turbo_tasks::value_impl]
24+
impl TextContentSourceAssetVc {
25+
#[turbo_tasks::function]
26+
pub fn new(source: AssetVc) -> Self {
27+
TextContentSourceAsset { source }.cell()
28+
}
29+
}
30+
31+
#[turbo_tasks::value_impl]
32+
impl Asset for TextContentSourceAsset {
33+
#[turbo_tasks::function]
34+
fn ident(&self) -> AssetIdentVc {
35+
self.source.ident().with_modifier(modifier())
36+
}
37+
38+
#[turbo_tasks::function]
39+
async fn content(&self) -> Result<AssetContentVc> {
40+
let source = self.source.content().file_content();
41+
let FileContent::Content(content) = &*source.await? else {
42+
return Ok(FileContent::NotFound.cell().into());
43+
};
44+
let text = content.content().to_str()?;
45+
let code = format!("export default {};", StringifyJs(&text));
46+
let content = FileContent::Content(code.into()).cell();
47+
Ok(content.into())
48+
}
49+
}

crates/turbopack-static/src/fixed.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use anyhow::Result;
2+
use turbo_tasks_fs::FileSystemPathVc;
3+
use turbopack_core::{
4+
asset::{Asset, AssetContentVc, AssetVc},
5+
ident::AssetIdentVc,
6+
};
7+
8+
/// A static asset that is served at a fixed output path. It won't use
9+
/// content hashing to generate a long term cacheable URL.
10+
#[turbo_tasks::value]
11+
pub struct FixedStaticAsset {
12+
output_path: FileSystemPathVc,
13+
source: AssetVc,
14+
}
15+
16+
#[turbo_tasks::value_impl]
17+
impl FixedStaticAssetVc {
18+
#[turbo_tasks::function]
19+
pub fn new(output_path: FileSystemPathVc, source: AssetVc) -> Self {
20+
FixedStaticAsset {
21+
output_path,
22+
source,
23+
}
24+
.cell()
25+
}
26+
}
27+
28+
#[turbo_tasks::value_impl]
29+
impl Asset for FixedStaticAsset {
30+
#[turbo_tasks::function]
31+
async fn ident(&self) -> Result<AssetIdentVc> {
32+
Ok(AssetIdentVc::from_path(self.output_path))
33+
}
34+
35+
#[turbo_tasks::function]
36+
fn content(&self) -> AssetContentVc {
37+
self.source.content()
38+
}
39+
}

crates/turbopack-static/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
1111
#![feature(min_specialization)]
1212

13+
pub mod fixed;
14+
1315
use anyhow::{anyhow, Result};
1416
use turbo_tasks::{primitives::StringVc, Value, ValueToString};
1517
use turbo_tasks_fs::FileContent;

0 commit comments

Comments
 (0)