Skip to content

Commit 39fb1ea

Browse files
authored
Turbopack: support requests with dynamic part in module name (#82401)
Turbopack fails to resolve this ```js function requireOrgPkg(s) { return require(`@org/pkg-${s}`) } ``` which is needed for the case of ```js require(`@img/sharp-${currentArch()}/sharp.node`) ```
1 parent 48ea532 commit 39fb1ea

File tree

30 files changed

+480
-195
lines changed

30 files changed

+480
-195
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use anyhow::Result;
22
use indoc::formatdoc;
3-
use turbo_rcstr::RcStr;
43
use turbo_tasks::{ResolvedVc, Vc};
54
use turbo_tasks_fs::{File, FileSystemPath};
65
use turbopack_core::{
@@ -61,18 +60,20 @@ impl ImportMappingReplacement for NextEdgeUnsupportedModuleReplacer {
6160
}
6261

6362
#[turbo_tasks::function]
64-
fn unsupported_module_source(root_path: FileSystemPath, module: RcStr) -> Vc<VirtualSource> {
63+
fn unsupported_module_source(root_path: FileSystemPath, module: Pattern) -> Vc<VirtualSource> {
6564
// packages/next/src/server/web/globals.ts augments global with
6665
// `__import_unsupported` and necessary functions.
6766
let code = formatdoc! {
6867
r#"
6968
{TURBOPACK_EXPORT_NAMESPACE}(__import_unsupported(`{module}`));
70-
"#
69+
"#,
70+
module = module.as_constant_string().map(ToString::to_string).unwrap_or_else(|| module.describe_as_string()),
7171
};
7272
let content = AssetContent::file(File::from(code).into());
7373
VirtualSource::new_with_ident(
74-
AssetIdent::from_path(root_path)
75-
.with_modifier(format!("unsupported edge import {module}").into()),
74+
AssetIdent::from_path(root_path).with_modifier(
75+
format!("unsupported edge import {}", module.describe_as_string()).into(),
76+
),
7677
content,
7778
)
7879
}

crates/next-core/src/next_server/resolve.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin {
100100
Regex::new("^(?:private-next-pages\\/|next\\/(?:dist\\/pages\\/|(?:app|cache|document|link|form|head|image|legacy\\/image|constants|dynamic|script|navigation|headers|router|compat\\/router|server)$)|string-hash|private-next-rsc-action-validate|private-next-rsc-action-client-wrapper|private-next-rsc-server-reference|private-next-rsc-cache-wrapper$)").unwrap()
101101
});
102102

103-
let Pattern::Constant(package_subpath) = package_subpath else {
103+
let (Pattern::Constant(package), Pattern::Constant(package_subpath)) =
104+
(package, package_subpath)
105+
else {
104106
return Ok(ResolveResultOption::none());
105107
};
106108
let request_str: RcStr = format!("{package}{package_subpath}").into();

crates/next-core/src/next_shared/resolve.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use turbopack_core::{
1313
resolve::{
1414
ExternalTraced, ExternalType, ResolveResult, ResolveResultItem, ResolveResultOption,
1515
parse::Request,
16+
pattern::Pattern,
1617
plugin::{
1718
AfterResolvePlugin, AfterResolvePluginCondition, BeforeResolvePlugin,
1819
BeforeResolvePluginCondition,
@@ -39,7 +40,7 @@ static FEATURE_MODULES: LazyLock<FxHashMap<&'static str, Vec<&'static str>>> =
3940
"/font/local",
4041
],
4142
),
42-
("@next", vec!["/font/google", "/font/local"]),
43+
("@next/font", vec!["/google", "/local"]),
4344
])
4445
});
4546

@@ -360,7 +361,7 @@ impl BeforeResolvePlugin for ModuleFeatureReportResolvePlugin {
360361
request: Vc<Request>,
361362
) -> Result<Vc<ResolveResultOption>> {
362363
if let Request::Module {
363-
module,
364+
module: Pattern::Constant(module),
364365
path,
365366
query: _,
366367
fragment: _,

turbopack/crates/turbopack-cli/src/build/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ async fn build_internal(
268268
false,
269269
),
270270
EntryRequest::Module(m, p) => Request::module(
271-
m.clone(),
271+
m.clone().into(),
272272
p.clone().into(),
273273
Default::default(),
274274
Default::default(),

turbopack/crates/turbopack-cli/src/dev/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ async fn source(
305305
false,
306306
),
307307
EntryRequest::Module(m, p) => Request::module(
308-
m.clone(),
308+
m.clone().into(),
309309
p.clone().into(),
310310
Default::default(),
311311
Default::default(),

turbopack/crates/turbopack-core/src/resolve/alias_map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,12 +497,12 @@ where
497497
}
498498
AliasKey::Wildcard { suffix } => {
499499
let mut remaining = self.request.clone();
500-
remaining.strip_prefix(prefix.len());
500+
remaining.strip_prefix_len(prefix.len());
501501
let remaining_suffix = remaining.constant_suffix();
502502
if !remaining_suffix.ends_with(&**suffix) {
503503
continue;
504504
}
505-
remaining.strip_suffix(suffix.len());
505+
remaining.strip_suffix_len(suffix.len());
506506

507507
let output = template.replace(&remaining);
508508
return Some(AliasMatch::Replaced(output));

0 commit comments

Comments
 (0)