Skip to content

Commit 19e04b2

Browse files
authored
Dont use baseURL relative absolute paths in declaration emit, use absolute paths in bundle emit (#26341)
1 parent ef7997c commit 19e04b2

11 files changed

+286
-5
lines changed

src/compiler/checker.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3900,8 +3900,6 @@ namespace ts {
39003900
compilerOptions,
39013901
contextFile,
39023902
context.tracker.moduleResolverHost,
3903-
context.tracker.moduleResolverHost.getSourceFiles!(), // TODO: GH#18217
3904-
{ importModuleSpecifierPreference: "non-relative" },
39053903
host.redirectTargetsMap,
39063904
);
39073905
links.specifierCache = links.specifierCache || createMap();

src/compiler/moduleSpecifiers.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,21 @@ namespace ts.moduleSpecifiers {
2727
compilerOptions: CompilerOptions,
2828
importingSourceFile: SourceFile,
2929
host: ModuleSpecifierResolutionHost,
30-
files: ReadonlyArray<SourceFile>,
31-
preferences: ModuleSpecifierPreferences,
3230
redirectTargetsMap: RedirectTargetsMap,
3331
): string {
34-
return first(first(getModuleSpecifiers(moduleSymbol, compilerOptions, importingSourceFile, host, files, preferences, redirectTargetsMap)));
32+
const isBundle = (compilerOptions.out || compilerOptions.outFile);
33+
if (isBundle && host.getCommonSourceDirectory) {
34+
// For declaration bundles, we need to generate absolute paths relative to the common source dir for imports,
35+
// just like how the declaration emitter does for the ambient module declarations - we can easily accomplish this
36+
// using the `baseUrl` compiler option (which we would otherwise never use in declaration emit) and a non-relative
37+
// specifier preference
38+
compilerOptions = {
39+
...compilerOptions,
40+
baseUrl: host.getCommonSourceDirectory(),
41+
};
42+
}
43+
const preferences: ModuleSpecifierPreferences = { importModuleSpecifierPreference: isBundle ? "non-relative" : "relative" };
44+
return first(first(getModuleSpecifiers(moduleSymbol, compilerOptions, importingSourceFile, host, host.getSourceFiles ? host.getSourceFiles() : [importingSourceFile], preferences, redirectTargetsMap)));
3545
}
3646

3747
// For each symlink/original for a module, returns a list of ways to import that file.

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5336,6 +5336,7 @@ namespace ts {
53365336
fileExists?(path: string): boolean;
53375337
readFile?(path: string): string | undefined;
53385338
getSourceFiles?(): ReadonlyArray<SourceFile>; // Used for cached resolutions to find symlinks without traversing the fs (again)
5339+
getCommonSourceDirectory?(): string;
53395340
}
53405341

53415342
// Note: this used to be deprecated in our public API, but is still used internally
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//// [tests/cases/compiler/declarationEmitPrefersPathKindBasedOnBundling.ts] ////
2+
3+
//// [scalar.ts]
4+
export interface Scalar {
5+
(): string;
6+
value: number;
7+
}
8+
9+
export function scalar(value: string): Scalar {
10+
return null as any;
11+
}
12+
//// [spacing.ts]
13+
import { scalar } from '../lib/operators/scalar';
14+
15+
export default {
16+
get xs() {
17+
return scalar("14px");
18+
}
19+
};
20+
21+
22+
//// [scalar.js]
23+
"use strict";
24+
Object.defineProperty(exports, "__esModule", { value: true });
25+
function scalar(value) {
26+
return null;
27+
}
28+
exports.scalar = scalar;
29+
//// [spacing.js]
30+
"use strict";
31+
Object.defineProperty(exports, "__esModule", { value: true });
32+
var scalar_1 = require("../lib/operators/scalar");
33+
exports.default = {
34+
get xs() {
35+
return scalar_1.scalar("14px");
36+
}
37+
};
38+
39+
40+
//// [scalar.d.ts]
41+
export interface Scalar {
42+
(): string;
43+
value: number;
44+
}
45+
export declare function scalar(value: string): Scalar;
46+
//// [spacing.d.ts]
47+
declare const _default: {
48+
readonly xs: import("../lib/operators/scalar").Scalar;
49+
};
50+
export default _default;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/src/lib/operators/scalar.ts ===
2+
export interface Scalar {
3+
>Scalar : Symbol(Scalar, Decl(scalar.ts, 0, 0))
4+
5+
(): string;
6+
value: number;
7+
>value : Symbol(Scalar.value, Decl(scalar.ts, 1, 12))
8+
}
9+
10+
export function scalar(value: string): Scalar {
11+
>scalar : Symbol(scalar, Decl(scalar.ts, 3, 1))
12+
>value : Symbol(value, Decl(scalar.ts, 5, 23))
13+
>Scalar : Symbol(Scalar, Decl(scalar.ts, 0, 0))
14+
15+
return null as any;
16+
}
17+
=== tests/cases/compiler/src/settings/spacing.ts ===
18+
import { scalar } from '../lib/operators/scalar';
19+
>scalar : Symbol(scalar, Decl(spacing.ts, 0, 8))
20+
21+
export default {
22+
get xs() {
23+
>xs : Symbol(xs, Decl(spacing.ts, 2, 16))
24+
25+
return scalar("14px");
26+
>scalar : Symbol(scalar, Decl(spacing.ts, 0, 8))
27+
}
28+
};
29+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== tests/cases/compiler/src/lib/operators/scalar.ts ===
2+
export interface Scalar {
3+
(): string;
4+
value: number;
5+
>value : number
6+
}
7+
8+
export function scalar(value: string): Scalar {
9+
>scalar : (value: string) => Scalar
10+
>value : string
11+
12+
return null as any;
13+
>null as any : any
14+
>null : null
15+
}
16+
=== tests/cases/compiler/src/settings/spacing.ts ===
17+
import { scalar } from '../lib/operators/scalar';
18+
>scalar : (value: string) => import("tests/cases/compiler/src/lib/operators/scalar").Scalar
19+
20+
export default {
21+
>{ get xs() { return scalar("14px"); }} : { readonly xs: import("tests/cases/compiler/src/lib/operators/scalar").Scalar; }
22+
23+
get xs() {
24+
>xs : import("tests/cases/compiler/src/lib/operators/scalar").Scalar
25+
26+
return scalar("14px");
27+
>scalar("14px") : import("tests/cases/compiler/src/lib/operators/scalar").Scalar
28+
>scalar : (value: string) => import("tests/cases/compiler/src/lib/operators/scalar").Scalar
29+
>"14px" : "14px"
30+
}
31+
};
32+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//// [tests/cases/compiler/declarationEmitPrefersPathKindBasedOnBundling2.ts] ////
2+
3+
//// [scalar.ts]
4+
export interface Scalar {
5+
(): string;
6+
value: number;
7+
}
8+
9+
export function scalar(value: string): Scalar {
10+
return null as any;
11+
}
12+
//// [spacing.ts]
13+
import { scalar } from '../lib/operators/scalar';
14+
15+
export default {
16+
get xs() {
17+
return scalar("14px");
18+
}
19+
};
20+
21+
22+
//// [dist.js]
23+
define("lib/operators/scalar", ["require", "exports"], function (require, exports) {
24+
"use strict";
25+
Object.defineProperty(exports, "__esModule", { value: true });
26+
function scalar(value) {
27+
return null;
28+
}
29+
exports.scalar = scalar;
30+
});
31+
define("settings/spacing", ["require", "exports", "lib/operators/scalar"], function (require, exports, scalar_1) {
32+
"use strict";
33+
Object.defineProperty(exports, "__esModule", { value: true });
34+
exports.default = {
35+
get xs() {
36+
return scalar_1.scalar("14px");
37+
}
38+
};
39+
});
40+
41+
42+
//// [dist.d.ts]
43+
declare module "lib/operators/scalar" {
44+
export interface Scalar {
45+
(): string;
46+
value: number;
47+
}
48+
export function scalar(value: string): Scalar;
49+
}
50+
declare module "settings/spacing" {
51+
const _default: {
52+
readonly xs: import("lib/operators/scalar").Scalar;
53+
};
54+
export default _default;
55+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/src/lib/operators/scalar.ts ===
2+
export interface Scalar {
3+
>Scalar : Symbol(Scalar, Decl(scalar.ts, 0, 0))
4+
5+
(): string;
6+
value: number;
7+
>value : Symbol(Scalar.value, Decl(scalar.ts, 1, 12))
8+
}
9+
10+
export function scalar(value: string): Scalar {
11+
>scalar : Symbol(scalar, Decl(scalar.ts, 3, 1))
12+
>value : Symbol(value, Decl(scalar.ts, 5, 23))
13+
>Scalar : Symbol(Scalar, Decl(scalar.ts, 0, 0))
14+
15+
return null as any;
16+
}
17+
=== tests/cases/compiler/src/settings/spacing.ts ===
18+
import { scalar } from '../lib/operators/scalar';
19+
>scalar : Symbol(scalar, Decl(spacing.ts, 0, 8))
20+
21+
export default {
22+
get xs() {
23+
>xs : Symbol(xs, Decl(spacing.ts, 2, 16))
24+
25+
return scalar("14px");
26+
>scalar : Symbol(scalar, Decl(spacing.ts, 0, 8))
27+
}
28+
};
29+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== tests/cases/compiler/src/lib/operators/scalar.ts ===
2+
export interface Scalar {
3+
(): string;
4+
value: number;
5+
>value : number
6+
}
7+
8+
export function scalar(value: string): Scalar {
9+
>scalar : (value: string) => Scalar
10+
>value : string
11+
12+
return null as any;
13+
>null as any : any
14+
>null : null
15+
}
16+
=== tests/cases/compiler/src/settings/spacing.ts ===
17+
import { scalar } from '../lib/operators/scalar';
18+
>scalar : (value: string) => import("tests/cases/compiler/src/lib/operators/scalar").Scalar
19+
20+
export default {
21+
>{ get xs() { return scalar("14px"); }} : { readonly xs: import("tests/cases/compiler/src/lib/operators/scalar").Scalar; }
22+
23+
get xs() {
24+
>xs : import("tests/cases/compiler/src/lib/operators/scalar").Scalar
25+
26+
return scalar("14px");
27+
>scalar("14px") : import("tests/cases/compiler/src/lib/operators/scalar").Scalar
28+
>scalar : (value: string) => import("tests/cases/compiler/src/lib/operators/scalar").Scalar
29+
>"14px" : "14px"
30+
}
31+
};
32+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// @declaration: true
2+
// @target: es5
3+
// @baseUrl: /.src/tests/cases/compiler
4+
// @outDir: ./dist
5+
// @rootDir: ./tests/cases/compiler/src
6+
// @filename: src/lib/operators/scalar.ts
7+
export interface Scalar {
8+
(): string;
9+
value: number;
10+
}
11+
12+
export function scalar(value: string): Scalar {
13+
return null as any;
14+
}
15+
// @filename: src/settings/spacing.ts
16+
import { scalar } from '../lib/operators/scalar';
17+
18+
export default {
19+
get xs() {
20+
return scalar("14px");
21+
}
22+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @declaration: true
2+
// @target: es5
3+
// @baseUrl: /.src/tests/cases/compiler
4+
// @module: amd
5+
// @outFile: ./dist.js
6+
// @rootDir: ./tests/cases/compiler/src
7+
// @filename: src/lib/operators/scalar.ts
8+
export interface Scalar {
9+
(): string;
10+
value: number;
11+
}
12+
13+
export function scalar(value: string): Scalar {
14+
return null as any;
15+
}
16+
// @filename: src/settings/spacing.ts
17+
import { scalar } from '../lib/operators/scalar';
18+
19+
export default {
20+
get xs() {
21+
return scalar("14px");
22+
}
23+
};

0 commit comments

Comments
 (0)