Skip to content

Commit 7ed43d2

Browse files
committed
Dedupe source even with sub path
1 parent 9819185 commit 7ed43d2

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

crates/next-core/src/next_edge/unsupported.rs

+28-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use anyhow::Result;
22
use indoc::formatdoc;
3+
use turbo_rcstr::RcStr;
34
use turbo_tasks::{ResolvedVc, Vc};
45
use turbo_tasks_fs::{File, FileSystemPath};
56
use turbopack_core::{
@@ -56,27 +57,32 @@ impl ImportMappingReplacement for NextEdgeUnsupportedModuleReplacer {
5657
) -> Result<Vc<ImportMapResult>> {
5758
let request = &*request.await?;
5859
if let Request::Module { module, .. } = request {
59-
// packages/next/src/server/web/globals.ts augments global with
60-
// `__import_unsupported` and necessary functions.
61-
let code = formatdoc! {
62-
r#"
63-
{TURBOPACK_EXPORT_NAMESPACE}(__import_unsupported(`{module}`));
64-
"#
65-
};
66-
let content = AssetContent::file(File::from(code).into());
67-
let source = VirtualSource::new_with_ident(
68-
AssetIdent::from_path(root_path).with_modifier(Vc::cell(
69-
format!("unsupported edge import {}", module).into(),
70-
)),
71-
content,
72-
)
73-
.to_resolved()
74-
.await?;
75-
return Ok(
76-
ImportMapResult::Result(ResolveResult::source(ResolvedVc::upcast(source))).cell(),
77-
);
78-
};
79-
80-
Ok(ImportMapResult::NoEntry.cell())
60+
// Call out to separate `unsupported_module_source` to only have a single Source cell
61+
// for requests with different subpaths: `fs` and `fs/promises`.
62+
let source = unsupported_module_source(root_path, module.clone())
63+
.to_resolved()
64+
.await?;
65+
Ok(ImportMapResult::Result(ResolveResult::source(ResolvedVc::upcast(source))).cell())
66+
} else {
67+
Ok(ImportMapResult::NoEntry.cell())
68+
}
8169
}
8270
}
71+
72+
#[turbo_tasks::function]
73+
fn unsupported_module_source(root_path: Vc<FileSystemPath>, module: RcStr) -> Vc<VirtualSource> {
74+
// packages/next/src/server/web/globals.ts augments global with
75+
// `__import_unsupported` and necessary functions.
76+
let code = formatdoc! {
77+
r#"
78+
{TURBOPACK_EXPORT_NAMESPACE}(__import_unsupported(`{module}`));
79+
"#
80+
};
81+
let content = AssetContent::file(File::from(code).into());
82+
VirtualSource::new_with_ident(
83+
AssetIdent::from_path(root_path).with_modifier(Vc::cell(
84+
format!("unsupported edge import {}", module).into(),
85+
)),
86+
content,
87+
)
88+
}

0 commit comments

Comments
 (0)