Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add FixedStaticAsset and TextContentSourceAsset #4692

Merged
merged 2 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/turbopack-ecmascript/src/lib.rs
Original file line number Diff line number Diff line change
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
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())
}
}
39 changes: 39 additions & 0 deletions crates/turbopack-static/src/fixed.rs
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()
}
}
2 changes: 2 additions & 0 deletions crates/turbopack-static/src/lib.rs
Original file line number Diff line number Diff line change
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