From 6699e15a6ad436e7acfbda74894114b17edc1fb4 Mon Sep 17 00:00:00 2001 From: erkelost <1256029807@qq.com> Date: Sat, 25 Jan 2025 20:17:27 +0800 Subject: [PATCH] chore: test import src and import.raw --- Cargo.lock | 4 ++-- examples/lib/index.ts | 4 ++-- rust-plugins/dts/src/lib.rs | 30 +++++++++++++++++------------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cab33f357..6d8c9e328 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2519,9 +2519,9 @@ dependencies = [ [[package]] name = "hstr" -version = "0.2.12" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dae404c0c5d4e95d4858876ab02eecd6a196bb8caa42050dfa809938833fc412" +checksum = "63d6824358c0fd9a68bb23999ed2ef76c84f79408a26ef7ae53d5f370c94ad36" dependencies = [ "hashbrown 0.14.5", "new_debug_unreachable", diff --git a/examples/lib/index.ts b/examples/lib/index.ts index 387609365..459da629e 100644 --- a/examples/lib/index.ts +++ b/examples/lib/index.ts @@ -1,5 +1,5 @@ -import { name as names } from './test'; -import type { base } from './test'; +import { name as names } from './test.ts'; +import type { base } from './test.ts'; export const a: number = 1; // 我是奥特曼 diff --git a/rust-plugins/dts/src/lib.rs b/rust-plugins/dts/src/lib.rs index f903cbac3..4a9cd77dc 100644 --- a/rust-plugins/dts/src/lib.rs +++ b/rust-plugins/dts/src/lib.rs @@ -95,7 +95,6 @@ impl Plugin for FarmPluginDts { let mut checker = FastDts::new(filename.clone()); module.visit_mut_with(&mut ImportVariableRemover); let issues = checker.transform(&mut module); - for issue in issues { let _range = issue.range(); } @@ -158,28 +157,33 @@ impl VisitMut for ImportPathRewriter { for item in items.iter_mut() { if let ModuleItem::ModuleDecl(ModuleDecl::Import(import)) = item { let src = &mut import.src; - if src.value.starts_with('.') { + + if src.value.starts_with("./") || src.value.starts_with("../") { let import_path = PathBuf::from(&*src.value); let source_dir = self.source_path.parent().unwrap_or_else(|| Path::new("")); - let full_path = source_dir.join(&import_path); - - let new_path = if full_path.to_string_lossy().ends_with(".ts") { - full_path.with_extension("d.ts") - } else if full_path.to_string_lossy().ends_with(".tsx") { - PathBuf::from(full_path.to_string_lossy().replace(".tsx", ".d.ts")) + let base_path = if let Some(stem) = import_path.file_stem() { + PathBuf::from(stem) } else { - PathBuf::from(format!("{}.d.ts", full_path.to_string_lossy())) + import_path.clone() }; + let full_path = source_dir.join(&base_path); + let new_path = PathBuf::from(format!("{}.d.ts", full_path.to_string_lossy())); + if let Some(rel_path) = pathdiff::diff_paths(&new_path, source_dir) { let new_value = rel_path.to_string_lossy().replace('\\', "/"); - - src.value = if !new_value.ends_with(".d.ts") { - format!("{}.d.ts", new_value).into() + let final_value = if !new_value.ends_with(".d.ts") { + format!( + "./{}.d.ts", + new_value.trim_end_matches(".ts").trim_end_matches(".tsx") + ) } else { - new_value.into() + format!("./{}", new_value) }; + + src.value = final_value.clone().into(); + src.raw = Some(format!("'{}'", final_value).into()); } } }