Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): handle TypeScript resolveJsonModule option #12

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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