|
| 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 | +} |
0 commit comments