Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1eca9bc

Browse filesBrowse files
guybedfordBethGriggs
authored andcommitted
module: support pattern trailers for imports field
PR-URL: #40041 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Geoffrey Booth <webmaster@geoffreybooth.com>
1 parent ec6de11 commit 1eca9bc
Copy full SHA for 1eca9bc

File tree

Expand file treeCollapse file tree

3 files changed

+27
-14
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+27
-14
lines changed
Open diff view settings
Collapse file

‎lib/internal/modules/esm/resolve.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/resolve.js
+24-14Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const {
2222
StringPrototypeSlice,
2323
StringPrototypeSplit,
2424
StringPrototypeStartsWith,
25-
StringPrototypeSubstr,
2625
} = primordials;
2726
const internalFS = require('internal/fs/utils');
2827
const { NativeModule } = require('internal/bootstrap/loaders');
@@ -724,38 +723,49 @@ function packageImportsResolve(name, base, conditions) {
724723
packageJSONUrl = pathToFileURL(packageConfig.pjsonPath);
725724
const imports = packageConfig.imports;
726725
if (imports) {
727-
if (ObjectPrototypeHasOwnProperty(imports, name)) {
726+
if (ObjectPrototypeHasOwnProperty(imports, name) &&
727+
!StringPrototypeIncludes(name, '*') &&
728+
!StringPrototypeEndsWith(name, '/')) {
728729
const resolved = resolvePackageTarget(
729730
packageJSONUrl, imports[name], '', name, base, false, true, conditions
730731
);
731732
if (resolved !== null)
732733
return { resolved, exact: true };
733734
} else {
734735
let bestMatch = '';
736+
let bestMatchSubpath;
735737
const keys = ObjectGetOwnPropertyNames(imports);
736738
for (let i = 0; i < keys.length; i++) {
737739
const key = keys[i];
738-
if (key[key.length - 1] === '*' &&
740+
const patternIndex = StringPrototypeIndexOf(key, '*');
741+
if (patternIndex !== -1 &&
739742
StringPrototypeStartsWith(name,
740-
StringPrototypeSlice(key, 0, -1)) &&
741-
name.length >= key.length &&
742-
key.length > bestMatch.length) {
743-
bestMatch = key;
743+
StringPrototypeSlice(key, 0,
744+
patternIndex))) {
745+
const patternTrailer = StringPrototypeSlice(key, patternIndex + 1);
746+
if (name.length >= key.length &&
747+
StringPrototypeEndsWith(name, patternTrailer) &&
748+
patternKeyCompare(bestMatch, key) === 1 &&
749+
StringPrototypeLastIndexOf(key, '*') === patternIndex) {
750+
bestMatch = key;
751+
bestMatchSubpath = StringPrototypeSlice(
752+
name, patternIndex, name.length - patternTrailer.length);
753+
}
744754
} else if (key[key.length - 1] === '/' &&
745755
StringPrototypeStartsWith(name, key) &&
746-
key.length > bestMatch.length) {
756+
patternKeyCompare(bestMatch, key) === 1) {
747757
bestMatch = key;
758+
bestMatchSubpath = StringPrototypeSlice(name, key.length);
748759
}
749760
}
750761

751762
if (bestMatch) {
752763
const target = imports[bestMatch];
753-
const pattern = bestMatch[bestMatch.length - 1] === '*';
754-
const subpath = StringPrototypeSubstr(name, bestMatch.length -
755-
(pattern ? 1 : 0));
756-
const resolved = resolvePackageTarget(
757-
packageJSONUrl, target, subpath, bestMatch, base, pattern, true,
758-
conditions);
764+
const pattern = StringPrototypeIncludes(bestMatch, '*');
765+
const resolved = resolvePackageTarget(packageJSONUrl, target,
766+
bestMatchSubpath, bestMatch,
767+
base, pattern, true,
768+
conditions);
759769
if (resolved !== null) {
760770
if (!pattern)
761771
emitFolderMapDeprecation(bestMatch, packageJSONUrl, false, base);
Collapse file

‎test/es-module/test-esm-imports.mjs‎

Copy file name to clipboardExpand all lines: test/es-module/test-esm-imports.mjs
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const { requireImport, importImport } = importer;
2020
['#external', { default: 'asdf' }],
2121
// External subpath imports
2222
['#external/subpath/asdf.js', { default: 'asdf' }],
23+
// Trailing pattern imports
24+
['#subpath/asdf.asdf', { default: 'test' }],
2325
]);
2426

2527
for (const [validSpecifier, expected] of internalImports) {
Collapse file

‎test/fixtures/es-modules/pkgimports/package.json‎

Copy file name to clipboardExpand all lines: test/fixtures/es-modules/pkgimports/package.json
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"require": "./requirebranch.js"
77
},
88
"#subpath/*": "./sub/*",
9+
"#subpath/*.asdf": "./test.js",
910
"#external": "pkgexports/valid-cjs",
1011
"#external/subpath/*": "pkgexports/sub/*",
1112
"#external/invalidsubpath/": "pkgexports/sub",

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.