Skip to content

Commit fde945d

Browse files
authored
Merge branch 'main' into chore/fix-github-casing
2 parents ba064d2 + 53678af commit fde945d

File tree

7 files changed

+71
-3
lines changed

7 files changed

+71
-3
lines changed

crates/rolldown_plugin/src/utils/resolve_id_with_plugins.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use crate::{
22
HookResolveIdArgs, PluginDriver,
33
types::{custom_field::CustomField, hook_resolve_id_skipped::HookResolveIdSkipped},
44
};
5-
use rolldown_common::{ImportKind, ModuleDefFormat, ResolvedId, is_existing_node_builtin_modules};
5+
use rolldown_common::{
6+
ImportKind, ModuleDefFormat, PackageJson, ResolvedId, is_existing_node_builtin_modules,
7+
};
68
use rolldown_resolver::{ResolveError, Resolver};
79
use std::{
810
path::{Path, PathBuf},
@@ -17,6 +19,36 @@ pub fn is_data_url(s: &str) -> bool {
1719
s.trim_start().starts_with("data:")
1820
}
1921

22+
/// Infers ModuleDefFormat from file path and optional package.json.
23+
/// This matches the logic used in the internal resolver's `infer_module_def_format`.
24+
fn infer_module_def_format(path: &str, package_json: Option<&Arc<PackageJson>>) -> ModuleDefFormat {
25+
let fmt = ModuleDefFormat::from_path(path);
26+
27+
// If the extension is specific (.mjs/.cjs/.cts/.mts), use it
28+
if !matches!(fmt, ModuleDefFormat::Unknown) {
29+
return fmt;
30+
}
31+
32+
// Check if it's a js-like extension (.js/.jsx/.ts/.tsx)
33+
let is_js_like_extension = Path::new(path)
34+
.extension()
35+
.is_some_and(|ext| matches!(ext.to_str(), Some("js" | "jsx" | "ts" | "tsx")));
36+
37+
if is_js_like_extension {
38+
if let Some(pkg) = package_json {
39+
if let Some(type_field) = pkg.r#type() {
40+
return match type_field {
41+
"module" => ModuleDefFormat::EsmPackageJson,
42+
"commonjs" => ModuleDefFormat::CjsPackageJson,
43+
_ => ModuleDefFormat::Unknown,
44+
};
45+
}
46+
}
47+
}
48+
49+
ModuleDefFormat::Unknown
50+
}
51+
2052
#[expect(clippy::too_many_arguments)]
2153
pub async fn resolve_id_with_plugins(
2254
resolver: &Resolver,
@@ -49,7 +81,7 @@ pub async fn resolve_id_with_plugins(
4981
Some(package_json)
5082
});
5183
return Ok(Ok(ResolvedId {
52-
module_def_format: ModuleDefFormat::from_path(r.id.as_str()),
84+
module_def_format: infer_module_def_format(r.id.as_str(), package_json.as_ref()),
5385
id: r.id,
5486
external: r.external.unwrap_or_default(),
5587
normalize_external_id: r.normalize_external_id,
@@ -79,7 +111,7 @@ pub async fn resolve_id_with_plugins(
79111
Some(package_json)
80112
});
81113
return Ok(Ok(ResolvedId {
82-
module_def_format: ModuleDefFormat::from_path(r.id.as_str()),
114+
module_def_format: infer_module_def_format(r.id.as_str(), package_json.as_ref()),
83115
id: r.id,
84116
external: r.external.unwrap_or_default(),
85117
normalize_external_id: r.normalize_external_id,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!node_modules
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { defineTest } from 'rolldown-tests'
2+
3+
export default defineTest({
4+
config: {
5+
external: ['node:assert'],
6+
plugins: [{
7+
name: 'test',
8+
async resolveId(specifier, importer, extraArgs) {
9+
if (specifier === 'dep') {
10+
return await this.resolve(specifier, importer)
11+
}
12+
}
13+
}]
14+
},
15+
async afterTest() {
16+
// @ts-ignore
17+
await import('./dist/main')
18+
},
19+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import nodeAssert from 'node:assert'
2+
import { cjsDepStar } from 'dep'
3+
4+
// If `lib/lib.js` is marked as `package.json#type: "module"` by rolldown,
5+
// then `cjsDepStar.default` should point to `cjsDepStar` itself
6+
nodeAssert.equal(cjsDepStar.value, 'cjs-dep')
7+
nodeAssert.equal(cjsDepStar.default.value, 'cjs-dep')

packages/rolldown/tests/fixtures/issues/6398/node_modules/dep/cjs-dep.cjs

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/rolldown/tests/fixtures/issues/6398/node_modules/dep/lib.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/rolldown/tests/fixtures/issues/6398/node_modules/dep/package.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)