turbo_tasks_fs/embed/
dir.rs

1pub use ::include_dir::{
2    include_dir, {self},
3};
4use anyhow::Result;
5use turbo_rcstr::RcStr;
6use turbo_tasks::Vc;
7
8use crate::{DiskFileSystem, FileSystem, embed::EmbeddedFileSystem};
9
10#[turbo_tasks::function]
11pub async fn directory_from_relative_path(
12    name: RcStr,
13    path: RcStr,
14) -> Result<Vc<Box<dyn FileSystem>>> {
15    let disk_fs = DiskFileSystem::new(name, path, vec![]);
16    disk_fs.await?.start_watching(None).await?;
17
18    Ok(Vc::upcast(disk_fs))
19}
20
21pub fn directory_from_include_dir(
22    name: RcStr,
23    dir: &'static include_dir::Dir<'static>,
24) -> Vc<Box<dyn FileSystem>> {
25    Vc::upcast(EmbeddedFileSystem::new(name, dir))
26}
27
28/// Returns an embedded [Vc<Box<dyn FileSystem>>] for the given path.
29///
30/// This will embed a directory's content into the binary and
31/// create an [Vc<EmbeddedFileSystem>].
32///
33/// If you enable the `dynamic_embed_contents` feature, calling
34/// the macro will return a [Vc<DiskFileSystem>].
35///
36/// This enables dynamic linking (and hot reloading) of embedded files/dirs.
37/// A binary built with `dynamic_embed_contents` enabled is **is not portable**,
38/// only the directory path will be embedded into the binary.
39#[macro_export]
40macro_rules! embed_directory {
41    ($name:tt, $path:tt) => {{        // make sure the path contains `$CARGO_MANIFEST_DIR`
42        assert!($path.contains("$CARGO_MANIFEST_DIR"));
43        // make sure `CARGO_MANIFEST_DIR` is the only env variable in the path
44        assert!(!$path.replace("$CARGO_MANIFEST_DIR", "").contains('$'));
45
46        turbo_tasks_fs::embed_directory_internal!($name, $path)
47    }};
48}
49
50#[cfg(feature = "dynamic_embed_contents")]
51#[macro_export]
52#[doc(hidden)]
53macro_rules! embed_directory_internal {
54    ($name:tt, $path:tt) => {{
55        // make sure the types the `include_dir!` proc macro refers to are in scope
56        use turbo_tasks_fs::embed::include_dir;
57
58        let path = $path.replace("$CARGO_MANIFEST_DIR", env!("CARGO_MANIFEST_DIR"));
59
60        turbo_tasks_fs::embed::directory_from_relative_path($name.into(), path.into())
61    }};
62}
63
64#[cfg(not(feature = "dynamic_embed_contents"))]
65#[macro_export]
66#[doc(hidden)]
67macro_rules! embed_directory_internal {
68    ($name:tt, $path:tt) => {{
69        // make sure the types the `include_dir!` proc macro refers to are in scope
70        use turbo_tasks_fs::embed::include_dir;
71
72        static DIR: include_dir::Dir<'static> = turbo_tasks_fs::embed::include_dir!($path);
73
74        turbo_tasks_fs::embed::directory_from_include_dir($name.into(), &DIR)
75    }};
76}