|
1 | 1 | use anyhow::Result;
|
2 | 2 | use indoc::formatdoc;
|
| 3 | +use turbo_rcstr::RcStr; |
3 | 4 | use turbo_tasks::{ResolvedVc, Vc};
|
4 | 5 | use turbo_tasks_fs::{File, FileSystemPath};
|
5 | 6 | use turbopack_core::{
|
@@ -56,27 +57,32 @@ impl ImportMappingReplacement for NextEdgeUnsupportedModuleReplacer {
|
56 | 57 | ) -> Result<Vc<ImportMapResult>> {
|
57 | 58 | let request = &*request.await?;
|
58 | 59 | 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 | + } |
81 | 69 | }
|
82 | 70 | }
|
| 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