Skip to content

Commit 3b9acae

Browse files
authored
Allow subpath imports that start with #/ (#62844)
1 parent ab142be commit 3b9acae

13 files changed

+833
-3
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,13 +1690,16 @@ export enum NodeResolutionFeatures {
16901690
// allowing `*` in the LHS of an export to be followed by more content, eg `"./whatever/*.js"`
16911691
// not supported in node 12 - https://github.com/nodejs/Release/issues/690
16921692
ExportsPatternTrailers = 1 << 4,
1693-
AllFeatures = Imports | SelfName | Exports | ExportsPatternTrailers,
1693+
// allowing `#/` root imports in package.json imports field
1694+
// not supported until mass adoption - https://github.com/nodejs/node/pull/60864
1695+
ImportsPatternRoot = 1 << 6,
1696+
AllFeatures = Imports | SelfName | Exports | ExportsPatternTrailers | ImportsPatternRoot,
16941697

16951698
Node16Default = Imports | SelfName | Exports | ExportsPatternTrailers,
16961699

16971700
NodeNextDefault = AllFeatures,
16981701

1699-
BundlerDefault = Imports | SelfName | Exports | ExportsPatternTrailers,
1702+
BundlerDefault = Imports | SelfName | Exports | ExportsPatternTrailers | ImportsPatternRoot,
17001703

17011704
EsmMode = 1 << 5,
17021705
}
@@ -2646,7 +2649,7 @@ function loadModuleFromExports(scope: PackageJsonInfo, extensions: Extensions, s
26462649
}
26472650

26482651
function loadModuleFromImports(extensions: Extensions, moduleName: string, directory: string, state: ModuleResolutionState, cache: ModuleResolutionCache | undefined, redirectedReference: ResolvedProjectReference | undefined): SearchResult<Resolved> {
2649-
if (moduleName === "#" || startsWith(moduleName, "#/")) {
2652+
if (moduleName === "#" || (startsWith(moduleName, "#/") && !(state.features & NodeResolutionFeatures.ImportsPatternRoot))) {
26502653
if (state.traceEnabled) {
26512654
trace(state.host, Diagnostics.Invalid_import_specifier_0_has_no_possible_resolutions, moduleName);
26522655
}
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)