Skip to content

Commit 8558033

Browse files
committed
Allow export map entries to remap back to input files for a program
1 parent 67172e4 commit 8558033

22 files changed

+577
-8
lines changed

src/compiler/moduleNameResolver.ts

+139-8
Large diffs are not rendered by default.

src/compiler/utilities.ts

+10
Original file line numberDiff line numberDiff line change
@@ -4355,6 +4355,16 @@ namespace ts {
43554355
Extension.Dts;
43564356
}
43574357

4358+
/**
4359+
* This function is an inverse of `getDeclarationEmitExtensionForPath`.
4360+
*/
4361+
export function getPossibleOriginalInputExtensionForExtension(path: string) {
4362+
return fileExtensionIsOneOf(path, [Extension.Dmts, Extension.Mjs, Extension.Mts]) ? [Extension.Mts, Extension.Mjs] :
4363+
fileExtensionIsOneOf(path, [Extension.Dcts, Extension.Cjs, Extension.Cts]) ? [Extension.Cts, Extension.Cjs]:
4364+
fileExtensionIsOneOf(path, [`.json.d.ts`]) ? [Extension.Json] :
4365+
[Extension.Tsx, Extension.Ts, Extension.Jsx, Extension.Js];
4366+
}
4367+
43584368
export function outFile(options: CompilerOptions) {
43594369
return options.outFile || options.out;
43604370
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [tests/cases/compiler/nodeNextPackageSelfNameWithOutDir.ts] ////
2+
3+
//// [package.json]
4+
{
5+
"name": "@this/package",
6+
"type": "module",
7+
"exports": {
8+
".": "./dist/index.js"
9+
}
10+
}
11+
//// [index.ts]
12+
import * as me from "@this/package";
13+
14+
me.thing();
15+
16+
export function thing(): void {}
17+
18+
19+
//// [index.js]
20+
import * as me from "@this/package";
21+
me.thing();
22+
export function thing() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/index.ts ===
2+
import * as me from "@this/package";
3+
>me : Symbol(me, Decl(index.ts, 0, 6))
4+
5+
me.thing();
6+
>me.thing : Symbol(thing, Decl(index.ts, 2, 11))
7+
>me : Symbol(me, Decl(index.ts, 0, 6))
8+
>thing : Symbol(thing, Decl(index.ts, 2, 11))
9+
10+
export function thing(): void {}
11+
>thing : Symbol(thing, Decl(index.ts, 2, 11))
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/index.ts ===
2+
import * as me from "@this/package";
3+
>me : typeof me
4+
5+
me.thing();
6+
>me.thing() : void
7+
>me.thing : () => void
8+
>me : typeof me
9+
>thing : () => void
10+
11+
export function thing(): void {}
12+
>thing : () => void
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//// [tests/cases/compiler/nodeNextPackageSelfNameWithOutDirDeclDir.ts] ////
2+
3+
//// [package.json]
4+
{
5+
"name": "@this/package",
6+
"type": "module",
7+
"exports": {
8+
".": {
9+
"default": "./dist/index.js",
10+
"types": "./types/index.d.ts"
11+
}
12+
}
13+
}
14+
//// [index.ts]
15+
import * as me from "@this/package";
16+
17+
me.thing();
18+
19+
export function thing(): void {}
20+
21+
22+
//// [index.js]
23+
import * as me from "@this/package";
24+
me.thing();
25+
export function thing() { }
26+
27+
28+
//// [index.d.ts]
29+
export declare function thing(): void;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/index.ts ===
2+
import * as me from "@this/package";
3+
>me : Symbol(me, Decl(index.ts, 0, 6))
4+
5+
me.thing();
6+
>me.thing : Symbol(thing, Decl(index.ts, 2, 11))
7+
>me : Symbol(me, Decl(index.ts, 0, 6))
8+
>thing : Symbol(thing, Decl(index.ts, 2, 11))
9+
10+
export function thing(): void {}
11+
>thing : Symbol(thing, Decl(index.ts, 2, 11))
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/index.ts ===
2+
import * as me from "@this/package";
3+
>me : typeof me
4+
5+
me.thing();
6+
>me.thing() : void
7+
>me.thing : () => void
8+
>me : typeof me
9+
>thing : () => void
10+
11+
export function thing(): void {}
12+
>thing : () => void
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//// [tests/cases/compiler/nodeNextPackageSelfNameWithOutDirDeclDirComposite.ts] ////
2+
3+
//// [package.json]
4+
{
5+
"name": "@this/package",
6+
"type": "module",
7+
"exports": {
8+
".": {
9+
"default": "./dist/index.js",
10+
"types": "./types/index.d.ts"
11+
}
12+
}
13+
}
14+
//// [index.ts]
15+
import * as me from "@this/package";
16+
17+
me.thing();
18+
19+
export function thing(): void {}
20+
21+
22+
//// [index.js]
23+
import * as me from "@this/package";
24+
me.thing();
25+
export function thing() { }
26+
27+
28+
//// [index.d.ts]
29+
export declare function thing(): void;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/index.ts ===
2+
import * as me from "@this/package";
3+
>me : Symbol(me, Decl(index.ts, 0, 6))
4+
5+
me.thing();
6+
>me.thing : Symbol(thing, Decl(index.ts, 2, 11))
7+
>me : Symbol(me, Decl(index.ts, 0, 6))
8+
>thing : Symbol(thing, Decl(index.ts, 2, 11))
9+
10+
export function thing(): void {}
11+
>thing : Symbol(thing, Decl(index.ts, 2, 11))
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/index.ts ===
2+
import * as me from "@this/package";
3+
>me : typeof me
4+
5+
me.thing();
6+
>me.thing() : void
7+
>me.thing : () => void
8+
>me : typeof me
9+
>thing : () => void
10+
11+
export function thing(): void {}
12+
>thing : () => void
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//// [tests/cases/compiler/nodeNextPackageSelfNameWithOutDirDeclDirCompositeNestedDirs.ts] ////
2+
3+
//// [package.json]
4+
{
5+
"name": "@this/package",
6+
"type": "module",
7+
"exports": {
8+
".": {
9+
"default": "./dist/index.js",
10+
"types": "./types/index.d.ts"
11+
}
12+
}
13+
}
14+
//// [index.ts]
15+
export {srcthing as thing} from "./src/thing.js";
16+
//// [thing.ts]
17+
// The following import should cause `index.ts`
18+
// to be included in the build, which will,
19+
// in turn, cause the common src directory to not be `src`
20+
// (the harness is wierd here in that noImplicitReferences makes only
21+
// this file get loaded as an entrypoint and emitted, while on the
22+
// real command-line we'll crawl the imports for that set - a limitation
23+
// of the harness, I suppose)
24+
import * as me from "@this/package";
25+
26+
me.thing();
27+
28+
export function srcthing(): void {}
29+
30+
31+
32+
//// [thing.js]
33+
// The following import should cause `index.ts`
34+
// to be included in the build, which will,
35+
// in turn, cause the common src directory to not be `src`
36+
// (the harness is wierd here in that noImplicitReferences makes only
37+
// this file get loaded as an entrypoint and emitted, while on the
38+
// real command-line we'll crawl the imports for that set - a limitation
39+
// of the harness, I suppose)
40+
import * as me from "@this/package";
41+
me.thing();
42+
export function srcthing() { }
43+
44+
45+
//// [thing.d.ts]
46+
export declare function srcthing(): void;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/src/thing.ts ===
2+
// The following import should cause `index.ts`
3+
// to be included in the build, which will,
4+
// in turn, cause the common src directory to not be `src`
5+
// (the harness is wierd here in that noImplicitReferences makes only
6+
// this file get loaded as an entrypoint and emitted, while on the
7+
// real command-line we'll crawl the imports for that set - a limitation
8+
// of the harness, I suppose)
9+
import * as me from "@this/package";
10+
>me : Symbol(me, Decl(thing.ts, 7, 6))
11+
12+
me.thing();
13+
>me.thing : Symbol(me.thing, Decl(index.ts, 0, 8))
14+
>me : Symbol(me, Decl(thing.ts, 7, 6))
15+
>thing : Symbol(me.thing, Decl(index.ts, 0, 8))
16+
17+
export function srcthing(): void {}
18+
>srcthing : Symbol(srcthing, Decl(thing.ts, 9, 11))
19+
20+
21+
=== tests/cases/compiler/index.ts ===
22+
export {srcthing as thing} from "./src/thing.js";
23+
>srcthing : Symbol(srcthing, Decl(thing.ts, 9, 11))
24+
>thing : Symbol(thing, Decl(index.ts, 0, 8))
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=== tests/cases/compiler/src/thing.ts ===
2+
// The following import should cause `index.ts`
3+
// to be included in the build, which will,
4+
// in turn, cause the common src directory to not be `src`
5+
// (the harness is wierd here in that noImplicitReferences makes only
6+
// this file get loaded as an entrypoint and emitted, while on the
7+
// real command-line we'll crawl the imports for that set - a limitation
8+
// of the harness, I suppose)
9+
import * as me from "@this/package";
10+
>me : typeof me
11+
12+
me.thing();
13+
>me.thing() : void
14+
>me.thing : () => void
15+
>me : typeof me
16+
>thing : () => void
17+
18+
export function srcthing(): void {}
19+
>srcthing : () => void
20+
21+
22+
=== tests/cases/compiler/index.ts ===
23+
export {srcthing as thing} from "./src/thing.js";
24+
>srcthing : () => void
25+
>thing : () => void
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//// [tests/cases/compiler/nodeNextPackageSelfNameWithOutDirDeclDirRootDir.ts] ////
2+
3+
//// [package.json]
4+
{
5+
"name": "@this/package",
6+
"type": "module",
7+
"exports": {
8+
".": {
9+
"default": "./dist/index.js",
10+
"types": "./types/index.d.ts"
11+
}
12+
}
13+
}
14+
//// [index.ts]
15+
import * as me from "@this/package";
16+
17+
me.thing();
18+
19+
export function thing(): void {}
20+
21+
22+
//// [index.js]
23+
import * as me from "@this/package";
24+
me.thing();
25+
export function thing() { }
26+
27+
28+
//// [index.d.ts]
29+
export declare function thing(): void;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== /pkg/src/index.ts ===
2+
import * as me from "@this/package";
3+
>me : Symbol(me, Decl(index.ts, 0, 6))
4+
5+
me.thing();
6+
>me.thing : Symbol(thing, Decl(index.ts, 2, 11))
7+
>me : Symbol(me, Decl(index.ts, 0, 6))
8+
>thing : Symbol(thing, Decl(index.ts, 2, 11))
9+
10+
export function thing(): void {}
11+
>thing : Symbol(thing, Decl(index.ts, 2, 11))
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== /pkg/src/index.ts ===
2+
import * as me from "@this/package";
3+
>me : typeof me
4+
5+
me.thing();
6+
>me.thing() : void
7+
>me.thing : () => void
8+
>me : typeof me
9+
>thing : () => void
10+
11+
export function thing(): void {}
12+
>thing : () => void
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @module: nodenext
2+
// @outDir: ./dist
3+
// @filename: package.json
4+
{
5+
"name": "@this/package",
6+
"type": "module",
7+
"exports": {
8+
".": "./dist/index.js"
9+
}
10+
}
11+
// @filename: index.ts
12+
import * as me from "@this/package";
13+
14+
me.thing();
15+
16+
export function thing(): void {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// @module: nodenext
2+
// @outDir: ./dist
3+
// @declarationDir: ./types
4+
// @declaration: true
5+
// @filename: package.json
6+
{
7+
"name": "@this/package",
8+
"type": "module",
9+
"exports": {
10+
".": {
11+
"default": "./dist/index.js",
12+
"types": "./types/index.d.ts"
13+
}
14+
}
15+
}
16+
// @filename: index.ts
17+
import * as me from "@this/package";
18+
19+
me.thing();
20+
21+
export function thing(): void {}

0 commit comments

Comments
 (0)