Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1690,13 +1690,16 @@ export enum NodeResolutionFeatures {
// allowing `*` in the LHS of an export to be followed by more content, eg `"./whatever/*.js"`
// not supported in node 12 - https://github.com/nodejs/Release/issues/690
ExportsPatternTrailers = 1 << 4,
AllFeatures = Imports | SelfName | Exports | ExportsPatternTrailers,
// allowing `#/` root imports in package.json imports field
// not supported until mass adoption - https://github.com/nodejs/node/pull/60864
ImportsPatternRoot = 1 << 6,
AllFeatures = Imports | SelfName | Exports | ExportsPatternTrailers | ImportsPatternRoot,

Node16Default = Imports | SelfName | Exports | ExportsPatternTrailers,

NodeNextDefault = AllFeatures,

BundlerDefault = Imports | SelfName | Exports | ExportsPatternTrailers,
BundlerDefault = Imports | SelfName | Exports | ExportsPatternTrailers | ImportsPatternRoot,

EsmMode = 1 << 5,
}
Expand Down Expand Up @@ -2646,7 +2649,7 @@ function loadModuleFromExports(scope: PackageJsonInfo, extensions: Extensions, s
}

function loadModuleFromImports(extensions: Extensions, moduleName: string, directory: string, state: ModuleResolutionState, cache: ModuleResolutionCache | undefined, redirectedReference: ResolvedProjectReference | undefined): SearchResult<Resolved> {
if (moduleName === "#" || startsWith(moduleName, "#/")) {
if (moduleName === "#" || (startsWith(moduleName, "#/") && !(state.features & NodeResolutionFeatures.ImportsPatternRoot))) {
if (state.traceEnabled) {
trace(state.host, Diagnostics.Invalid_import_specifier_0_has_no_possible_resolutions, moduleName);
}
Expand Down
89 changes: 89 additions & 0 deletions tests/baselines/reference/nodeModulesPackageImportsRootWildcard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//// [tests/cases/conformance/node/nodeModulesPackageImportsRootWildcard.ts] ////

//// [package.json]
{
"name": "package",
"private": true,
"type": "module",
"imports": {
"#/*": "./src/*"
}
}
//// [foo.ts]
export const foo = "foo";
//// [bar.ts]
export const bar = "bar";
//// [baz.ts]
export const baz = "baz";
//// [index.ts]
// esm format file
import { foo } from "#/foo.js";
import { bar } from "#/features/bar.js";
import { baz } from "#/nested/deep/baz.js";
foo;
bar;
baz;
//// [index.mts]
// esm format file
import { foo } from "#/foo.js";
import { bar } from "#/features/bar.js";
import { baz } from "#/nested/deep/baz.js";
foo;
bar;
baz;
//// [index.cts]
// cjs format file
import { foo } from "#/foo.js";
import { bar } from "#/features/bar.js";
import { baz } from "#/nested/deep/baz.js";
foo;
bar;
baz;


//// [foo.js]
export const foo = "foo";
//// [bar.js]
export const bar = "bar";
//// [baz.js]
export const baz = "baz";
//// [index.js]
// esm format file
import { foo } from "#/foo.js";
import { bar } from "#/features/bar.js";
import { baz } from "#/nested/deep/baz.js";
foo;
bar;
baz;
//// [index.mjs]
// esm format file
import { foo } from "#/foo.js";
import { bar } from "#/features/bar.js";
import { baz } from "#/nested/deep/baz.js";
foo;
bar;
baz;
//// [index.cjs]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// cjs format file
const foo_js_1 = require("#/foo.js");
const bar_js_1 = require("#/features/bar.js");
const baz_js_1 = require("#/nested/deep/baz.js");
foo_js_1.foo;
bar_js_1.bar;
baz_js_1.baz;


//// [foo.d.ts]
export declare const foo = "foo";
//// [bar.d.ts]
export declare const bar = "bar";
//// [baz.d.ts]
export declare const baz = "baz";
//// [index.d.ts]
export {};
//// [index.d.mts]
export {};
//// [index.d.cts]
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//// [tests/cases/conformance/node/nodeModulesPackageImportsRootWildcard.ts] ////

=== src/foo.ts ===
export const foo = "foo";
>foo : Symbol(foo, Decl(foo.ts, 0, 12))

=== src/features/bar.ts ===
export const bar = "bar";
>bar : Symbol(bar, Decl(bar.ts, 0, 12))

=== src/nested/deep/baz.ts ===
export const baz = "baz";
>baz : Symbol(baz, Decl(baz.ts, 0, 12))

=== index.ts ===
// esm format file
import { foo } from "#/foo.js";
>foo : Symbol(foo, Decl(index.ts, 1, 8))

import { bar } from "#/features/bar.js";
>bar : Symbol(bar, Decl(index.ts, 2, 8))

import { baz } from "#/nested/deep/baz.js";
>baz : Symbol(baz, Decl(index.ts, 3, 8))

foo;
>foo : Symbol(foo, Decl(index.ts, 1, 8))

bar;
>bar : Symbol(bar, Decl(index.ts, 2, 8))

baz;
>baz : Symbol(baz, Decl(index.ts, 3, 8))

=== index.mts ===
// esm format file
import { foo } from "#/foo.js";
>foo : Symbol(foo, Decl(index.mts, 1, 8))

import { bar } from "#/features/bar.js";
>bar : Symbol(bar, Decl(index.mts, 2, 8))

import { baz } from "#/nested/deep/baz.js";
>baz : Symbol(baz, Decl(index.mts, 3, 8))

foo;
>foo : Symbol(foo, Decl(index.mts, 1, 8))

bar;
>bar : Symbol(bar, Decl(index.mts, 2, 8))

baz;
>baz : Symbol(baz, Decl(index.mts, 3, 8))

=== index.cts ===
// cjs format file
import { foo } from "#/foo.js";
>foo : Symbol(foo, Decl(index.cts, 1, 8))

import { bar } from "#/features/bar.js";
>bar : Symbol(bar, Decl(index.cts, 2, 8))

import { baz } from "#/nested/deep/baz.js";
>baz : Symbol(baz, Decl(index.cts, 3, 8))

foo;
>foo : Symbol(foo, Decl(index.cts, 1, 8))

bar;
>bar : Symbol(bar, Decl(index.cts, 2, 8))

baz;
>baz : Symbol(baz, Decl(index.cts, 3, 8))

Loading