Unverified Commit 39fb1ea8 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

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`)
```
parent 48ea532b
use anyhow::Result;
use indoc::formatdoc;
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::{File, FileSystemPath};
use turbopack_core::{
......@@ -61,18 +60,20 @@ impl ImportMappingReplacement for NextEdgeUnsupportedModuleReplacer {
}
#[turbo_tasks::function]
fn unsupported_module_source(root_path: FileSystemPath, module: RcStr) -> Vc<VirtualSource> {
fn unsupported_module_source(root_path: FileSystemPath, module: Pattern) -> Vc<VirtualSource> {
// packages/next/src/server/web/globals.ts augments global with
// `__import_unsupported` and necessary functions.
let code = formatdoc! {
r#"
{TURBOPACK_EXPORT_NAMESPACE}(__import_unsupported(`{module}`));
"#
"#,
module = module.as_constant_string().map(ToString::to_string).unwrap_or_else(|| module.describe_as_string()),
};
let content = AssetContent::file(File::from(code).into());
VirtualSource::new_with_ident(
AssetIdent::from_path(root_path)
.with_modifier(format!("unsupported edge import {module}").into()),
AssetIdent::from_path(root_path).with_modifier(
format!("unsupported edge import {}", module.describe_as_string()).into(),
),
content,
)
}
......@@ -100,7 +100,9 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin {
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()
});
let Pattern::Constant(package_subpath) = package_subpath else {
let (Pattern::Constant(package), Pattern::Constant(package_subpath)) =
(package, package_subpath)
else {
return Ok(ResolveResultOption::none());
};
let request_str: RcStr = format!("{package}{package_subpath}").into();
......
......@@ -13,6 +13,7 @@ use turbopack_core::{
resolve::{
ExternalTraced, ExternalType, ResolveResult, ResolveResultItem, ResolveResultOption,
parse::Request,
pattern::Pattern,
plugin::{
AfterResolvePlugin, AfterResolvePluginCondition, BeforeResolvePlugin,
BeforeResolvePluginCondition,
......@@ -39,7 +40,7 @@ static FEATURE_MODULES: LazyLock<FxHashMap<&'static str, Vec<&'static str>>> =
"/font/local",
],
),
("@next", vec!["/font/google", "/font/local"]),
("@next/font", vec!["/google", "/local"]),
])
});
......@@ -360,7 +361,7 @@ impl BeforeResolvePlugin for ModuleFeatureReportResolvePlugin {
request: Vc<Request>,
) -> Result<Vc<ResolveResultOption>> {
if let Request::Module {
module,
module: Pattern::Constant(module),
path,
query: _,
fragment: _,
......
......@@ -268,7 +268,7 @@ async fn build_internal(
false,
),
EntryRequest::Module(m, p) => Request::module(
m.clone(),
m.clone().into(),
p.clone().into(),
Default::default(),
Default::default(),
......
......@@ -305,7 +305,7 @@ async fn source(
false,
),
EntryRequest::Module(m, p) => Request::module(
m.clone(),
m.clone().into(),
p.clone().into(),
Default::default(),
Default::default(),
......
......@@ -497,12 +497,12 @@ where
}
AliasKey::Wildcard { suffix } => {
let mut remaining = self.request.clone();
remaining.strip_prefix(prefix.len());
remaining.strip_prefix_len(prefix.len());
let remaining_suffix = remaining.constant_suffix();
if !remaining_suffix.ends_with(&**suffix) {
continue;
}
remaining.strip_suffix(suffix.len());
remaining.strip_suffix_len(suffix.len());
let output = template.replace(&remaining);
return Some(AliasMatch::Replaced(output));
......
......@@ -234,7 +234,7 @@ impl AliasTemplate for Vc<ImportMapping> {
ImportMapping::External(name, ty, traced) => {
if let Some(name) = name {
ReplacedImportMapping::External(
capture.spread_into_star(name).as_string().map(|s| s.into()),
capture.spread_into_star(name).as_constant_string().cloned(),
*ty,
*traced,
)
......@@ -250,7 +250,7 @@ impl AliasTemplate for Vc<ImportMapping> {
} => {
if let Some(name) = name {
ReplacedImportMapping::PrimaryAlternativeExternal {
name: capture.spread_into_star(name).as_string().map(|s| s.into()),
name: capture.spread_into_star(name).as_constant_string().cloned(),
ty: *ty,
traced: *traced,
lookup_dir: lookup_dir.clone(),
......
......@@ -71,7 +71,7 @@ impl BeforeResolvePluginCondition {
},
BeforeResolvePluginCondition::Modules(modules) => {
if let Request::Module { module, .. } = &*request.await? {
modules.contains(module)
modules.iter().any(|m| module.is_match(m))
} else {
false
}
......
......@@ -419,7 +419,7 @@ impl ModuleReference for EsmAssetReference {
}
if let Request::Module { module, .. } = &*self.request.await?
&& module == TURBOPACK_PART_IMPORT_SOURCE
&& module.is_match(TURBOPACK_PART_IMPORT_SOURCE)
{
if let Some(part) = &self.export_name {
let module: ResolvedVc<crate::EcmascriptModuleAsset> =
......
......@@ -128,7 +128,7 @@ impl ModuleReference for TsReferenceTypeAssetReference {
type_resolve(
*self.origin,
Request::module(
self.module.clone(),
self.module.clone().into(),
RcStr::default().into(),
RcStr::default(),
RcStr::default(),
......
......@@ -160,7 +160,7 @@ impl Module for TsConfigModuleAsset {
TsConfigTypesReference::new(
*self.origin,
Request::module(
name,
name.into(),
RcStr::default().into(),
RcStr::default(),
RcStr::default(),
......
......@@ -426,13 +426,15 @@ pub async fn type_resolve(
fragment: _,
} = &*request.await?
{
let m = if let Some(stripped) = m.strip_prefix('@') {
stripped.replace('/', "__").into()
let mut m = if let Some(mut stripped) = m.strip_prefix("@") {
stripped.replace_constants(&|c| Some(Pattern::Constant(c.replace("/", "__").into())));
stripped
} else {
m.clone()
};
m.push_front(rcstr!("@types/").into());
Some(Request::module(
format!("@types/{m}").into(),
m,
p.clone(),
RcStr::default(),
RcStr::default(),
......
function requirePkg(s) {
return require(`pkg-${s}`)
}
function requireOrgPkg(s) {
return require(`@org/pkg-${s}`)
}
it('should correctly handle dynamic parts in regular package name', () => {
expect(requirePkg('a').default).toBe('pkg-a')
expect(requirePkg('b').default).toBe('pkg-b')
expect(requirePkg('c').default).toBe('pkg-c')
})
it('should correctly handle dynamic parts in namespaced package name', () => {
expect(requireOrgPkg('a').default).toBe('org/pkg-a')
expect(requireOrgPkg('b').default).toBe('org/pkg-b')
expect(requireOrgPkg('c').default).toBe('org/pkg-c')
})
......@@ -10,7 +10,7 @@ error - [resolve] /turbopack/crates/turbopack-tests/tests/snapshot/imports/resol
| It was not possible to find the requested file.
| Parsed request as written in source code: module "does-not-exist" with subpath '/path'
| Parsed request as written in source code: module 'does-not-exist' with subpath '/path'
| Path where resolving has started: [project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/input/index.js
| Type of request: commonjs request
|
\ No newline at end of file
......@@ -11,7 +11,7 @@ error - [resolve] /turbopack/crates/turbopack-tests/tests/snapshot/imports/resol
| It was not possible to find the requested file.
| Parsed request as written in source code: module "does-not-exist" with subpath '/path'
| Parsed request as written in source code: module 'does-not-exist' with subpath '/path'
| Path where resolving has started: [project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/resolve_error_esm/input/index.js
| Type of request: EcmaScript Modules (part) request
|
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment