Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add FixedStaticAsset and TextContentSourceAsset (#4692)
### Description needed for metadata support in next.js (vercel/next.js#48823)
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters