Skip to content

Commit 225c43c

Browse files
dummdidummmtsgrd
andauthored
fix: respect moduleResolution setting in emitDts (#2845)
see #2837 for more info --------- Co-authored-by: Mattias Granlund <[email protected]>
1 parent 0c059aa commit 225c43c

File tree

12 files changed

+86
-4
lines changed

12 files changed

+86
-4
lines changed

.changeset/witty-kiwis-divide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte2tsx': patch
3+
---
4+
5+
fix: respect moduleResolution setting in emitDts

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ bower_components
3333
build/Release
3434

3535
# Dependency directories
36-
node_modules/
36+
/node_modules/
3737
jspm_packages/
3838

3939
# TypeScript v1 declaration files

packages/svelte2tsx/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.DS_Store
2-
node_modules
2+
/node_modules
3+
/test/emitDts/samples/cross-package-generic-types/node_modules/.svelte2tsx-language-server-files
34
/index.js
45
/index.js.map
56
/index.mjs

packages/svelte2tsx/src/emitDts.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,11 @@ function loadTsconfig(config: EmitDtsConfig, svelteMap: SvelteMap) {
112112
...options,
113113
noEmit: false, // Set to true in case of jsconfig, force false, else nothing is emitted
114114
moduleResolution:
115-
// NodeJS: up to 4.9, Node10: since 5.0
116-
(ts.ModuleResolutionKind as any).NodeJs ?? ts.ModuleResolutionKind.Node10, // Classic if not set, which gives wrong results
115+
options.moduleResolution &&
116+
options.moduleResolution !== ts.ModuleResolutionKind.Classic
117+
? options.moduleResolution
118+
: // NodeJS: up to 4.9, Node10: since 5.0
119+
((ts.ModuleResolutionKind as any).NodeJs ?? ts.ModuleResolutionKind.Node10), // Classic if not set, which gives wrong results
117120
declaration: true, // Needed for d.ts file generation
118121
emitDeclarationOnly: true, // We only want d.ts file generation
119122
declarationDir: config.declarationDir, // Where to put the declarations
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { GenericToken } from 'test-package';
2+
export declare class MyService {
3+
private name;
4+
constructor(name: string);
5+
getName(): string;
6+
}
7+
export declare const SERVICE_TOKEN: GenericToken<MyService>;
8+
export declare const ANNOTATED_TOKEN: GenericToken<MyService>;
9+
export declare const ASSERTION_TOKEN: GenericToken<MyService>;
10+
export declare class AnotherService {
11+
value: number;
12+
}
13+
export declare const ANOTHER_TOKEN: GenericToken<AnotherService>;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './consumer.js';

packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/node_modules/test-package/not-at-root/index.d.ts

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/node_modules/test-package/package.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "test-cross-package-generics",
3+
"version": "0.0.1",
4+
"type": "module",
5+
"dependencies": {
6+
"test-package": "1.0.0"
7+
}
8+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { GenericToken } from 'test-package';
2+
3+
export class MyService {
4+
constructor(private name: string) {}
5+
getName() { return this.name; }
6+
}
7+
8+
// This should preserve the generic type as GenericToken<MyService>
9+
// Before the fix, this would be compiled as 'any' due to module resolution issues
10+
export const SERVICE_TOKEN = new GenericToken<MyService>('MyService');
11+
12+
// These explicit annotations should work regardless
13+
export const ANNOTATED_TOKEN: GenericToken<MyService> = new GenericToken<MyService>('MyService');
14+
export const ASSERTION_TOKEN = new GenericToken<MyService>('MyService') as GenericToken<MyService>;
15+
16+
// Test with different generic types
17+
export class AnotherService {
18+
value = 42;
19+
}
20+
21+
export const ANOTHER_TOKEN = new GenericToken<AnotherService>('AnotherService');

0 commit comments

Comments
 (0)