Skip to content

Commit 65fa7e2

Browse files
committed
fix(core): resolve path in node_modules
1 parent 41f20fe commit 65fa7e2

File tree

5 files changed

+60
-14
lines changed

5 files changed

+60
-14
lines changed

packages/integrate-module-bundler/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@types/react": "^19.0.13",
2424
"@types/react-dom": "^19.0.5",
2525
"express": "^5.1.0",
26+
"core-js": "^3.41.0",
2627
"react": "^19.1.0",
2728
"react-dom": "^19.1.0",
2829
"simple-git": "^3.27.0",

packages/integrate-module-bundler/src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import 'core-js/modules/esnext.symbol.async-dispose'
2+
import 'core-js/modules/esnext.symbol.dispose'
3+
14
import assert from 'node:assert'
25
import test from 'node:test'
36
import { createRequire } from 'node:module'
@@ -76,12 +79,18 @@ await test('resolve mts in type commonjs package', () => {
7679
})
7780

7881
await test('resolve nestjs', async () => {
79-
const app = await bootstrap()
82+
const { app } = await bootstrap()
8083
const service = app.get(AppService)
8184
assert.equal(service.getHello(), 'Hello World!')
8285
await app.close()
8386
})
8487

88+
await test('using syntax', async () => {
89+
await using nest = await bootstrap()
90+
const service = nest.app.get(AppService)
91+
assert.equal(service.getHello(), 'Hello World!')
92+
})
93+
8594
if (!process.versions.node.startsWith('18')) {
8695
await test('resolve typescript cjs', () => {
8796
const require = createRequire(import.meta.url)

packages/integrate-module-bundler/src/nestjs/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import { AppModule } from './app.module'
55
export async function bootstrap() {
66
const app = await NestFactory.create(AppModule)
77
await app.listen(3000)
8-
return app
8+
return { app, [Symbol.asyncDispose]: async () => app.close() }
99
}

pnpm-lock.yaml

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

src/lib.rs

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use oxc::{
2121
},
2222
};
2323
use oxc_resolver::{
24-
CompilerOptionsSerde, EnforceExtension, PackageType, ResolveOptions, Resolver, TsConfigSerde,
25-
TsconfigOptions, TsconfigReferences,
24+
Cache, CompilerOptionsSerde, EnforceExtension, PackageType, Resolution, ResolveOptions,
25+
Resolver, TsConfigSerde, TsconfigOptions, TsconfigReferences,
2626
};
2727
use phf::Set;
2828

@@ -486,16 +486,7 @@ pub fn create_resolve<'env>(
486486
.map(|p| p.contains(NODE_MODULES_PATH))
487487
.unwrap_or(false)
488488
{
489-
#[cfg_attr(not(target_os = "windows"), allow(unused_mut))]
490-
let mut url = if resolution.query().is_some() || resolution.fragment().is_some() {
491-
format!("{PATH_PREFIX}{}", resolution.full_path().to_string_lossy())
492-
} else {
493-
format!("{PATH_PREFIX}{}", resolution.path().to_string_lossy())
494-
};
495-
#[cfg(target_os = "windows")]
496-
{
497-
url = url.replace("\\", "/");
498-
}
489+
let url = oxc_resolved_path_to_url(&resolution);
499490
return Ok(Either::A(ResolveFnOutput {
500491
short_circuit: Some(true),
501492
format: {
@@ -522,6 +513,29 @@ pub fn create_resolve<'env>(
522513
url,
523514
import_attributes: Some(Either::A(context.import_attributes.clone())),
524515
}));
516+
} else {
517+
let format = resolution
518+
.package_json()
519+
.and_then(|p| {
520+
p.r#type.map(|p| match p {
521+
PackageType::CommonJs => "commonjs",
522+
PackageType::Module => "module",
523+
})
524+
})
525+
.or_else(|| match p.extension().and_then(|ext| ext.to_str()) {
526+
Some("cjs" | "cts" | "node") => Some("commonjs"),
527+
Some("mts" | "mjs") => Some("module"),
528+
_ => None,
529+
})
530+
.unwrap_or("commonjs")
531+
.to_string();
532+
tracing::debug!("oxc resolved path node_modules: {} {}", p.display(), format);
533+
return Ok(Either::A(ResolveFnOutput {
534+
format: Either3::A(format),
535+
short_circuit: Some(true),
536+
url: oxc_resolved_path_to_url(&resolution),
537+
import_attributes: Some(Either::A(context.import_attributes.clone())),
538+
}));
525539
}
526540
}
527541

@@ -809,3 +823,17 @@ fn join_errors(errors: Vec<OxcDiagnostic>, source_str: &str) -> String {
809823
.collect::<Vec<_>>()
810824
.join("\n")
811825
}
826+
827+
fn oxc_resolved_path_to_url<C: Cache>(resolution: &Resolution<C>) -> String {
828+
#[cfg_attr(not(target_os = "windows"), allow(unused_mut))]
829+
let mut url = if resolution.query().is_some() || resolution.fragment().is_some() {
830+
format!("{PATH_PREFIX}{}", resolution.full_path().to_string_lossy())
831+
} else {
832+
format!("{PATH_PREFIX}{}", resolution.path().to_string_lossy())
833+
};
834+
#[cfg(target_os = "windows")]
835+
{
836+
url = url.replace("\\", "/");
837+
}
838+
url
839+
}

0 commit comments

Comments
 (0)