next_core/next_edge/
entry.rs

1use anyhow::Result;
2use turbo_rcstr::{RcStr, rcstr};
3use turbo_tasks::{ResolvedVc, Vc, fxindexmap};
4use turbo_tasks_fs::FileSystemPath;
5use turbopack_core::{context::AssetContext, module::Module, reference_type::ReferenceType};
6
7use crate::util::load_next_js_template_no_imports;
8
9#[turbo_tasks::function]
10pub async fn wrap_edge_entry(
11    asset_context: Vc<Box<dyn AssetContext>>,
12    project_root: FileSystemPath,
13    entry: ResolvedVc<Box<dyn Module>>,
14    pathname: RcStr,
15) -> Result<Vc<Box<dyn Module>>> {
16    // The actual wrapper lives in the Next.js templates directory as `edge-wrapper.js`.
17    // We use the template expansion helper so this code is kept in sync with other
18    // Next.js runtime templates. This particular template does not have any imports
19    // of its own, so we use the variant that allows templates without relative
20    // imports to be rewritten.
21    let template_source = load_next_js_template_no_imports(
22        "edge-wrapper.js",
23        project_root,
24        &[("VAR_ENTRY_NAME", &format!("middleware_{pathname}"))],
25        &[],
26        &[],
27    )
28    .await?;
29
30    let inner_assets = fxindexmap! {
31        rcstr!("MODULE") => entry
32    };
33
34    Ok(asset_context
35        .process(
36            template_source,
37            ReferenceType::Internal(ResolvedVc::cell(inner_assets)),
38        )
39        .module())
40}