turbopack_ecmascript_runtime/
embed_js.rs

1use anyhow::Result;
2use turbo_rcstr::RcStr;
3use turbo_tasks::Vc;
4use turbo_tasks_fs::{FileContent, FileSystem, FileSystemPath, embed_directory};
5use turbopack_core::{code_builder::Code, context::AssetContext};
6use turbopack_ecmascript::StaticEcmascriptCode;
7
8#[turbo_tasks::function]
9pub fn embed_fs() -> Vc<Box<dyn FileSystem>> {
10    embed_directory!("turbopack", "$CARGO_MANIFEST_DIR/js/src")
11}
12
13#[turbo_tasks::function]
14pub async fn embed_file(path: RcStr) -> Result<Vc<FileContent>> {
15    Ok(embed_fs().root().await?.join(&path)?.read())
16}
17
18#[turbo_tasks::function]
19pub async fn embed_file_path(path: RcStr) -> Result<Vc<FileSystemPath>> {
20    Ok(embed_fs().root().await?.join(&path)?.cell())
21}
22
23#[turbo_tasks::function]
24pub async fn embed_static_code(
25    asset_context: Vc<Box<dyn AssetContext>>,
26    path: RcStr,
27    generate_source_map: bool,
28) -> Result<Vc<Code>> {
29    Ok(StaticEcmascriptCode::new(
30        asset_context,
31        embed_file_path(path).await?.clone_value(),
32        generate_source_map,
33    )
34    .code())
35}