Skip to content

Commit

Permalink
feat(core): handle TypeScript resolveJsonModule option (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn authored Jul 14, 2024
1 parent 285559b commit 3b0d0a4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
5 changes: 5 additions & 0 deletions packages/integrate-module/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { bar } from './subdirectory/bar.mjs'
import { baz } from './subdirectory/index.mjs'
import { Component } from './component.js'
import './js-module.mjs'
import pkgJson from '../package.json'

const { foo: fooWithQuery } = await import(`./foo.mjs?q=${Date.now()}`)

Expand Down Expand Up @@ -56,3 +57,7 @@ await test('resolve @napi-rs projects', () => {
await test('resolve simple-git', () => {
assert.ok(simpleGit)
})

await test('resolve package.json', () => {
assert.equal(pkgJson.name, 'integrate-module')
})
1 change: 1 addition & 0 deletions packages/integrate-module/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"composite": true,
"jsx": "react-jsx",
"outDir": "dist",
"resolveJsonModule": true,
"baseUrl": "./",
"paths": {
"@subdirectory/*": ["./src/subdirectory/*"]
Expand Down
57 changes: 33 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ pub struct ResolveFnOutput {
#[napi(ts_return_type = "ResolveFnOutput | Promise<ResolveFnOutput>")]
pub fn resolve(
specifier: String,
context: ResolveContext,
mut context: ResolveContext,
next_resolve: Function<
(String, Option<ResolveContext>),
Either<ResolveFnOutput, PromiseRaw<ResolveFnOutput>>,
Expand Down Expand Up @@ -387,29 +387,38 @@ pub fn resolve(
return Ok(Either::A(ResolveFnOutput {
short_circuit: Some(true),
format: {
let format = p
.extension()
.and_then(|ext| ext.to_str())
.and_then(|ext| {
if ext == "cjs" || ext == "cts" {
None
} else {
resolution
.package_json()
.and_then(|p| p.r#type.as_ref())
.and_then(|t| t.as_str())
.and_then(|format| {
if format == "module" {
Some("module".to_owned())
} else {
None
}
})
}
})
.unwrap_or_else(|| "commonjs".to_owned());
tracing::debug!(path = ?p, format = ?format);
Either3::A(format)
let ext = p.extension().and_then(|ext| ext.to_str());
// handle TypeScript `resolveJsonModule` option
if ext.map(|e| e == "json").unwrap_or_default() {
context
.import_attributes
.insert("type".to_owned(), "json".to_owned());
Either3::A("json".to_owned())
} else if ext.map(|e| e == "wasm").unwrap_or_default() {
Either3::A("wasm".to_owned())
} else {
let format = ext
.and_then(|ext| {
if ext == "cjs" || ext == "cts" || ext == "node" {
None
} else {
resolution
.package_json()
.and_then(|p| p.r#type.as_ref())
.and_then(|t| t.as_str())
.and_then(|format| {
if format == "module" {
Some("module".to_owned())
} else {
None
}
})
}
})
.unwrap_or_else(|| "commonjs".to_owned());
tracing::debug!(path = ?p, format = ?format);
Either3::A(format)
}
},
url,
import_attributes: Some(Either::A(context.import_attributes.clone())),
Expand Down

0 comments on commit 3b0d0a4

Please sign in to comment.