Skip to content

Commit

Permalink
add FixedStaticAsset and TextContentSourceAsset (#4692)
Browse files Browse the repository at this point in the history
### Description

needed for metadata support in next.js
(vercel/next.js#48823)
  • Loading branch information
sokra committed Apr 25, 2023
1 parent 73ee47c commit f56af36
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/turbopack-ecmascript/src/lib.rs
Expand Up @@ -17,6 +17,7 @@ mod path_visitor;
pub(crate) mod references;
pub mod resolve;
pub(crate) mod special_cases;
pub mod text;
pub(crate) mod transform;
pub mod tree_shake;
pub mod typescript;
Expand Down
49 changes: 49 additions & 0 deletions crates/turbopack-ecmascript/src/text/mod.rs
@@ -0,0 +1,49 @@
use anyhow::Result;
use turbo_tasks::primitives::StringVc;
use turbo_tasks_fs::FileContent;
use turbopack_core::{
asset::{Asset, AssetContentVc, AssetVc},
ident::AssetIdentVc,
};

use crate::utils::StringifyJs;

#[turbo_tasks::function]
fn modifier() -> StringVc {
StringVc::cell("text content".to_string())
}

/// A source asset that exports the string content of an asset as the default
/// export of a JS module.
#[turbo_tasks::value]
pub struct TextContentSourceAsset {
pub source: AssetVc,
}

#[turbo_tasks::value_impl]
impl TextContentSourceAssetVc {
#[turbo_tasks::function]
pub fn new(source: AssetVc) -> Self {
TextContentSourceAsset { source }.cell()
}
}

#[turbo_tasks::value_impl]
impl Asset for TextContentSourceAsset {
#[turbo_tasks::function]
fn ident(&self) -> AssetIdentVc {
self.source.ident().with_modifier(modifier())
}

#[turbo_tasks::function]
async fn content(&self) -> Result<AssetContentVc> {
let source = self.source.content().file_content();
let FileContent::Content(content) = &*source.await? else {
return Ok(FileContent::NotFound.cell().into());
};
let text = content.content().to_str()?;
let code = format!("export default {};", StringifyJs(&text));
let content = FileContent::Content(code.into()).cell();
Ok(content.into())
}
}
39 changes: 39 additions & 0 deletions crates/turbopack-static/src/fixed.rs
@@ -0,0 +1,39 @@
use anyhow::Result;
use turbo_tasks_fs::FileSystemPathVc;
use turbopack_core::{
asset::{Asset, AssetContentVc, AssetVc},
ident::AssetIdentVc,
};

/// A static asset that is served at a fixed output path. It won't use
/// content hashing to generate a long term cacheable URL.
#[turbo_tasks::value]
pub struct FixedStaticAsset {
output_path: FileSystemPathVc,
source: AssetVc,
}

#[turbo_tasks::value_impl]
impl FixedStaticAssetVc {
#[turbo_tasks::function]
pub fn new(output_path: FileSystemPathVc, source: AssetVc) -> Self {
FixedStaticAsset {
output_path,
source,
}
.cell()
}
}

#[turbo_tasks::value_impl]
impl Asset for FixedStaticAsset {
#[turbo_tasks::function]
async fn ident(&self) -> Result<AssetIdentVc> {
Ok(AssetIdentVc::from_path(self.output_path))
}

#[turbo_tasks::function]
fn content(&self) -> AssetContentVc {
self.source.content()
}
}
2 changes: 2 additions & 0 deletions crates/turbopack-static/src/lib.rs
Expand Up @@ -10,6 +10,8 @@

#![feature(min_specialization)]

pub mod fixed;

use anyhow::{anyhow, Result};
use turbo_tasks::{primitives::StringVc, Value, ValueToString};
use turbo_tasks_fs::FileContent;
Expand Down

0 comments on commit f56af36

Please sign in to comment.