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 );
26 disk_fs.await?.start_watching(None).await?;
27
28 let fs_path = disk_fs.root().await?.join(path)?;
29 Ok(fs_path.read())
30}
31
32#[turbo_tasks::function]
33pub fn content_from_str(string: RcStr) -> Vc<FileContent> {
34 File::from(string).into()
35}
36
37#[cfg(feature = "dynamic_embed_contents")]
41#[macro_export]
42macro_rules! embed_file {
43 ($path:expr) => {{
44 let _ = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $path));
46
47 turbo_tasks_fs::embed::content_from_relative_path(
48 env!("CARGO_MANIFEST_DIR").into(),
49 $path.into(),
50 )
51 }};
52}
53
54#[cfg(not(feature = "dynamic_embed_contents"))]
56#[macro_export]
57macro_rules! embed_file {
58 ($path:expr) => {
59 turbo_tasks_fs::embed::content_from_str(
60 include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $path)).into(),
61 )
62 };
63}