Skip to content
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
Empty file.
Empty file.

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

1 change: 1 addition & 0 deletions fixtures/pnpm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"axios": "1.8.4",
"decimal.js": "10.5.0",
"ipaddr.js": "2.2.0",
"magic-string": "0.30.17",
"mathjs": "14.4.0",
"minimatch": "10.0.1",
"postcss": "8.5.3",
Expand Down
20 changes: 20 additions & 0 deletions napi/tests/dts.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { assert, test } from "vitest";

import { ResolverFactory } from "../index.js";

const currentDir = join(fileURLToPath(import.meta.url), "..");
const rootDir = join(currentDir, "..", "..");
const pnpmDir = join(rootDir, "fixtures", "pnpm");

const resolver = new ResolverFactory({
conditionNames: ["import", "types"],
});

test("magic-string resolves to .d.mts", () => {
const containingFile = join(pnpmDir, "index.ts");
const result = resolver.resolveDtsSync(containingFile, "magic-string");
assert.isUndefined(result.error);
assert.match(result.path, /magic-string\.es\.d\.mts$/);
});
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

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

11 changes: 7 additions & 4 deletions src/dts_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,13 +593,16 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
}

fn dts_resolve_esm_match(&self, cached_path: &CachedPath, ctx: &mut Ctx) -> Option<CachedPath> {
// Try declaration/TS extension substitution first (e.g. .mjs -> .d.mts)
let extensions = Extensions::TYPESCRIPT.union(Extensions::DECLARATION);
Comment thread
Boshen marked this conversation as resolved.
if let Some(path) = self.dts_resolve_as_file(extensions, cached_path, ctx) {
return Some(path);
}
// Fall back to original file if it exists
if self.cache.is_file(cached_path, ctx) {
return Some(cached_path.clone());
}
// Try as file with TS extensions
let extensions =
Extensions::TYPESCRIPT.union(Extensions::DECLARATION).union(Extensions::JAVASCRIPT);
self.dts_resolve_as_file(extensions, cached_path, ctx)
None
}

// -------- @types name mangling --------
Expand Down
13 changes: 13 additions & 0 deletions src/tests/dts_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@ fn no_types_not_resolved() {
assert_eq!(result.path(), dts_fixture().join("node_modules/no-types/index.js"));
}

// -------- exports: ESM match finds declaration over JS --------

#[test]
fn exports_esm_match_finds_declaration() {
// When exports resolves to .mjs but .d.mts exists, prefer .d.mts
let r = resolver();
let result = r.resolve_dts(containing_file(), "exports-dts-for-mjs").unwrap();
assert_eq!(
result.path(),
dts_fixture().join("node_modules/exports-dts-for-mjs/dist/index.d.mts")
);
}

// -------- @types name mangling --------

#[test]
Expand Down
Loading