Skip to content

Commit ed2bf3d

Browse files
committed
Allow subpath imports that start with #/
This aligns with Node.js PR nodejs/node#60864, which allows defining symmetric `exports` and `imports` fields in package.json: { "exports": { "./*": "./src/*" }, "imports": { "#/*": "./src/*" } }
1 parent 38d95c8 commit ed2bf3d

21 files changed

+2062
-1
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2646,7 +2646,7 @@ function loadModuleFromExports(scope: PackageJsonInfo, extensions: Extensions, s
26462646
}
26472647

26482648
function loadModuleFromImports(extensions: Extensions, moduleName: string, directory: string, state: ModuleResolutionState, cache: ModuleResolutionCache | undefined, redirectedReference: ResolvedProjectReference | undefined): SearchResult<Resolved> {
2649-
if (moduleName === "#" || startsWith(moduleName, "#/")) {
2649+
if (moduleName === "#") {
26502650
if (state.traceEnabled) {
26512651
trace(state.host, Diagnostics.Invalid_import_specifier_0_has_no_possible_resolutions, moduleName);
26522652
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
index.cts(2,21): error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("#/foo.js")' call instead.
2+
index.cts(3,21): error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("#/features/bar.js")' call instead.
3+
index.cts(4,21): error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("#/nested/deep/baz.js")' call instead.
4+
5+
6+
==== package.json (0 errors) ====
7+
{
8+
"name": "package",
9+
"private": true,
10+
"type": "module",
11+
"imports": {
12+
"#/*": "./src/*"
13+
}
14+
}
15+
==== src/foo.ts (0 errors) ====
16+
export const foo = "foo";
17+
==== src/features/bar.ts (0 errors) ====
18+
export const bar = "bar";
19+
==== src/nested/deep/baz.ts (0 errors) ====
20+
export const baz = "baz";
21+
==== index.ts (0 errors) ====
22+
// esm format file
23+
import { foo } from "#/foo.js";
24+
import { bar } from "#/features/bar.js";
25+
import { baz } from "#/nested/deep/baz.js";
26+
foo;
27+
bar;
28+
baz;
29+
==== index.mts (0 errors) ====
30+
// esm format file
31+
import { foo } from "#/foo.js";
32+
import { bar } from "#/features/bar.js";
33+
import { baz } from "#/nested/deep/baz.js";
34+
foo;
35+
bar;
36+
baz;
37+
==== index.cts (3 errors) ====
38+
// cjs format file
39+
import { foo } from "#/foo.js";
40+
~~~~~~~~~~
41+
!!! error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("#/foo.js")' call instead.
42+
import { bar } from "#/features/bar.js";
43+
~~~~~~~~~~~~~~~~~~~
44+
!!! error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("#/features/bar.js")' call instead.
45+
import { baz } from "#/nested/deep/baz.js";
46+
~~~~~~~~~~~~~~~~~~~~~~
47+
!!! error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("#/nested/deep/baz.js")' call instead.
48+
foo;
49+
bar;
50+
baz;
51+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//// [tests/cases/conformance/node/nodeModulesPackageImportsRootWildcard.ts] ////
2+
3+
//// [package.json]
4+
{
5+
"name": "package",
6+
"private": true,
7+
"type": "module",
8+
"imports": {
9+
"#/*": "./src/*"
10+
}
11+
}
12+
//// [foo.ts]
13+
export const foo = "foo";
14+
//// [bar.ts]
15+
export const bar = "bar";
16+
//// [baz.ts]
17+
export const baz = "baz";
18+
//// [index.ts]
19+
// esm format file
20+
import { foo } from "#/foo.js";
21+
import { bar } from "#/features/bar.js";
22+
import { baz } from "#/nested/deep/baz.js";
23+
foo;
24+
bar;
25+
baz;
26+
//// [index.mts]
27+
// esm format file
28+
import { foo } from "#/foo.js";
29+
import { bar } from "#/features/bar.js";
30+
import { baz } from "#/nested/deep/baz.js";
31+
foo;
32+
bar;
33+
baz;
34+
//// [index.cts]
35+
// cjs format file
36+
import { foo } from "#/foo.js";
37+
import { bar } from "#/features/bar.js";
38+
import { baz } from "#/nested/deep/baz.js";
39+
foo;
40+
bar;
41+
baz;
42+
43+
44+
//// [foo.js]
45+
export const foo = "foo";
46+
//// [bar.js]
47+
export const bar = "bar";
48+
//// [baz.js]
49+
export const baz = "baz";
50+
//// [index.js]
51+
// esm format file
52+
import { foo } from "#/foo.js";
53+
import { bar } from "#/features/bar.js";
54+
import { baz } from "#/nested/deep/baz.js";
55+
foo;
56+
bar;
57+
baz;
58+
//// [index.mjs]
59+
// esm format file
60+
import { foo } from "#/foo.js";
61+
import { bar } from "#/features/bar.js";
62+
import { baz } from "#/nested/deep/baz.js";
63+
foo;
64+
bar;
65+
baz;
66+
//// [index.cjs]
67+
"use strict";
68+
Object.defineProperty(exports, "__esModule", { value: true });
69+
// cjs format file
70+
const foo_js_1 = require("#/foo.js");
71+
const bar_js_1 = require("#/features/bar.js");
72+
const baz_js_1 = require("#/nested/deep/baz.js");
73+
foo_js_1.foo;
74+
bar_js_1.bar;
75+
baz_js_1.baz;
76+
77+
78+
//// [foo.d.ts]
79+
export declare const foo = "foo";
80+
//// [bar.d.ts]
81+
export declare const bar = "bar";
82+
//// [baz.d.ts]
83+
export declare const baz = "baz";
84+
//// [index.d.ts]
85+
export {};
86+
//// [index.d.mts]
87+
export {};
88+
//// [index.d.cts]
89+
export {};
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//// [tests/cases/conformance/node/nodeModulesPackageImportsRootWildcard.ts] ////
2+
3+
=== src/foo.ts ===
4+
export const foo = "foo";
5+
>foo : Symbol(foo, Decl(foo.ts, 0, 12))
6+
7+
=== src/features/bar.ts ===
8+
export const bar = "bar";
9+
>bar : Symbol(bar, Decl(bar.ts, 0, 12))
10+
11+
=== src/nested/deep/baz.ts ===
12+
export const baz = "baz";
13+
>baz : Symbol(baz, Decl(baz.ts, 0, 12))
14+
15+
=== index.ts ===
16+
// esm format file
17+
import { foo } from "#/foo.js";
18+
>foo : Symbol(foo, Decl(index.ts, 1, 8))
19+
20+
import { bar } from "#/features/bar.js";
21+
>bar : Symbol(bar, Decl(index.ts, 2, 8))
22+
23+
import { baz } from "#/nested/deep/baz.js";
24+
>baz : Symbol(baz, Decl(index.ts, 3, 8))
25+
26+
foo;
27+
>foo : Symbol(foo, Decl(index.ts, 1, 8))
28+
29+
bar;
30+
>bar : Symbol(bar, Decl(index.ts, 2, 8))
31+
32+
baz;
33+
>baz : Symbol(baz, Decl(index.ts, 3, 8))
34+
35+
=== index.mts ===
36+
// esm format file
37+
import { foo } from "#/foo.js";
38+
>foo : Symbol(foo, Decl(index.mts, 1, 8))
39+
40+
import { bar } from "#/features/bar.js";
41+
>bar : Symbol(bar, Decl(index.mts, 2, 8))
42+
43+
import { baz } from "#/nested/deep/baz.js";
44+
>baz : Symbol(baz, Decl(index.mts, 3, 8))
45+
46+
foo;
47+
>foo : Symbol(foo, Decl(index.mts, 1, 8))
48+
49+
bar;
50+
>bar : Symbol(bar, Decl(index.mts, 2, 8))
51+
52+
baz;
53+
>baz : Symbol(baz, Decl(index.mts, 3, 8))
54+
55+
=== index.cts ===
56+
// cjs format file
57+
import { foo } from "#/foo.js";
58+
>foo : Symbol(foo, Decl(index.cts, 1, 8))
59+
60+
import { bar } from "#/features/bar.js";
61+
>bar : Symbol(bar, Decl(index.cts, 2, 8))
62+
63+
import { baz } from "#/nested/deep/baz.js";
64+
>baz : Symbol(baz, Decl(index.cts, 3, 8))
65+
66+
foo;
67+
>foo : Symbol(foo, Decl(index.cts, 1, 8))
68+
69+
bar;
70+
>bar : Symbol(bar, Decl(index.cts, 2, 8))
71+
72+
baz;
73+
>baz : Symbol(baz, Decl(index.cts, 3, 8))
74+

0 commit comments

Comments
 (0)