From 304dc6bf44d8513f98df61779f85a9db22a8f5d6 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sun, 14 Jul 2024 23:56:08 +0800 Subject: [PATCH] feat(core): handle TypeScript resolveJsonModule option --- packages/integrate-module/src/index.ts | 5 +++ packages/integrate-module/tsconfig.json | 1 + src/lib.rs | 57 ++++++++++++++----------- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/packages/integrate-module/src/index.ts b/packages/integrate-module/src/index.ts index 6a93753..b123472 100644 --- a/packages/integrate-module/src/index.ts +++ b/packages/integrate-module/src/index.ts @@ -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()}`) @@ -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') +}) diff --git a/packages/integrate-module/tsconfig.json b/packages/integrate-module/tsconfig.json index 4a08f83..912956c 100644 --- a/packages/integrate-module/tsconfig.json +++ b/packages/integrate-module/tsconfig.json @@ -5,6 +5,7 @@ "composite": true, "jsx": "react-jsx", "outDir": "dist", + "resolveJsonModule": true, "baseUrl": "./", "paths": { "@subdirectory/*": ["./src/subdirectory/*"] diff --git a/src/lib.rs b/src/lib.rs index 046cb09..b45e270 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -260,7 +260,7 @@ pub struct ResolveFnOutput { #[napi(ts_return_type = "ResolveFnOutput | Promise")] pub fn resolve( specifier: String, - context: ResolveContext, + mut context: ResolveContext, next_resolve: Function< (String, Option), Either>, @@ -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())),