Skip to content

Commit

Permalink
chore: test import src and import.raw
Browse files Browse the repository at this point in the history
  • Loading branch information
ErKeLost committed Jan 25, 2025
1 parent 69e8057 commit 6699e15
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/lib/index.ts
Original file line number Diff line number Diff line change
@@ -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;

// 我是奥特曼
Expand Down
30 changes: 17 additions & 13 deletions rust-plugins/dts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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());
}
}
}
Expand Down

0 comments on commit 6699e15

Please sign in to comment.