File size: 1,939 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::path::PathBuf;

use anyhow::{Context, Result};
use dunce::canonicalize;
use turbo_rcstr::RcStr;
use turbo_tasks::Vc;

use crate::{DiskFileSystem, File, FileContent, FileSystem};

#[turbo_tasks::function]
pub async fn content_from_relative_path(
    package_path: RcStr,
    path: RcStr,
) -> Result<Vc<FileContent>> {
    let package_path = PathBuf::from(package_path);
    let resolved_path = package_path.join(path);
    let resolved_path =
        canonicalize(&resolved_path).context("failed to canonicalize embedded file path")?;
    let root_path = resolved_path.parent().unwrap();
    let path = resolved_path.file_name().unwrap().to_str().unwrap();

    let disk_fs = DiskFileSystem::new(
        root_path.to_string_lossy().into(),
        root_path.to_string_lossy().into(),
        vec![],
    );
    disk_fs.await?.start_watching(None).await?;

    let fs_path = disk_fs.root().await?.join(path)?;
    Ok(fs_path.read())
}

#[turbo_tasks::function]
pub fn content_from_str(string: RcStr) -> Vc<FileContent> {
    File::from(string).into()
}

/// Loads a file's content from disk and invalidates on change (debug builds).
///
/// In production, this will embed a file's content into the binary directly.
#[cfg(feature = "dynamic_embed_contents")]
#[macro_export]
macro_rules! embed_file {
    ($path:expr) => {{
        // check that the file exists at compile time
        let _ = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $path));

        turbo_tasks_fs::embed::content_from_relative_path(
            env!("CARGO_MANIFEST_DIR").into(),
            $path.into(),
        )
    }};
}

/// Embeds a file's content into the binary (production).
#[cfg(not(feature = "dynamic_embed_contents"))]
#[macro_export]
macro_rules! embed_file {
    ($path:expr) => {
        turbo_tasks_fs::embed::content_from_str(
            include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $path)).into(),
        )
    };
}