turbo_tasks_fs/embed/
file.rs1use std::path::PathBuf;
2
3use anyhow::{Context, Result};
4use dunce::canonicalize;
5use turbo_rcstr::RcStr;
6use turbo_tasks::Vc;
7
8use crate::{DiskFileSystem, File, FileContent, FileSystem};
9
10#[turbo_tasks::function]
11pub async fn content_from_relative_path(
12 package_path: RcStr,
13 path: RcStr,
14) -> Result<Vc<FileContent>> {
15 let package_path = PathBuf::from(package_path);
16 let resolved_path = package_path.join(path);
17 let resolved_path =
18 canonicalize(&resolved_path).context("failed to canonicalize embedded file path")?;
19 let root_path = resolved_path.parent().unwrap();
20 let path = resolved_path.file_name().unwrap().to_str().unwrap();
21
22 let disk_fs = DiskFileSystem::new(
23 root_path.to_string_lossy().into(),
24 root_path.to_string_lossy().into(),
25 vec![],
26 );
27 disk_fs.await?.start_watching(None).await?;
28
29 let fs_path = disk_fs.root().join(path.into());
30 Ok(fs_path.read())
31}
32
33#[turbo_tasks::function]
34pub fn content_from_str(string: RcStr) -> Vc<FileContent> {
35 File::from(string).into()
36}
37
38#[cfg(feature = "dynamic_embed_contents")]
42#[macro_export]
43macro_rules! embed_file {
44 ($path:expr) => {{
45 let _ = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $path));
47
48 turbo_tasks_fs::embed::content_from_relative_path(
49 env!("CARGO_MANIFEST_DIR").into(),
50 $path.into(),
51 )
52 }};
53}
54
55#[cfg(not(feature = "dynamic_embed_contents"))]
57#[macro_export]
58macro_rules! embed_file {
59 ($path:expr) => {
60 turbo_tasks_fs::embed::content_from_str(
61 include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $path)).into(),
62 )
63 };
64}