Skip to content

Commit 83ccf1a

Browse files
committed
module: support pattern trailers
1 parent f2d02ab commit 83ccf1a

File tree

4 files changed

+98
-28
lines changed

4 files changed

+98
-28
lines changed

doc/api/esm.md

+41-14
Original file line numberDiff line numberDiff line change
@@ -1176,25 +1176,36 @@ The resolver can throw the following errors:
11761176
**PACKAGE_IMPORTS_EXPORTS_RESOLVE**(_matchKey_, _matchObj_, _packageURL_,
11771177
_isImports_, _conditions_)
11781178

1179-
> 1. If _matchKey_ is a key of _matchObj_, and does not end in _"*"_, then
1179+
> 1. If _matchKey_ is a key of _matchObj_ and does not end in _"/"_ or contain
1180+
> _"*"_, then
11801181
> 1. Let _target_ be the value of _matchObj_\[_matchKey_\].
11811182
> 1. Let _resolved_ be the result of **PACKAGE_TARGET_RESOLVE**(
11821183
> _packageURL_, _target_, _""_, **false**, _isImports_, _conditions_).
11831184
> 1. Return the object _{ resolved, exact: **true** }_.
1184-
> 1. Let _expansionKeys_ be the list of keys of _matchObj_ ending in _"/"_
1185-
> or _"*"_, sorted by length descending.
1185+
> 1. Let _expansionKeys_ be the list of keys of _matchObj_ either ending in
1186+
> _"/"_ or containing only a single _"*"_, sorted by the sorting function
1187+
> **PATTERN_KEY_COMPARE** which orders in descending order of specificity.
11861188
> 1. For each key _expansionKey_ in _expansionKeys_, do
1187-
> 1. If _expansionKey_ ends in _"*"_ and _matchKey_ starts with but is
1188-
> not equal to the substring of _expansionKey_ excluding the last _"*"_
1189-
> character, then
1190-
> 1. Let _target_ be the value of _matchObj_\[_expansionKey_\].
1191-
> 1. Let _subpath_ be the substring of _matchKey_ starting at the
1192-
> index of the length of _expansionKey_ minus one.
1193-
> 1. Let _resolved_ be the result of **PACKAGE_TARGET_RESOLVE**(
1194-
> _packageURL_, _target_, _subpath_, **true**, _isImports_,
1195-
> _conditions_).
1196-
> 1. Return the object _{ resolved, exact: **true** }_.
1197-
> 1. If _matchKey_ starts with _expansionKey_, then
1189+
> 1. Let _patternBase_ be **null**.
1190+
> 1. If _expansionKey_ contains _"*"_, set _patternBase_ to the substring of
1191+
> _expansionKey_ up to but excluding the first _"*"_ character.
1192+
> 1. If _patternBase_ is not **null** and _matchKey_ starts with but is not
1193+
> equal to _patternBase_, then
1194+
> 1. Let _patternTrailer_ be the substring of _expansionKey_ from the
1195+
> index after the first _"*"_ character.
1196+
> 1. If _patternTrailer_ has zero length, or if _matchKey_ ends with
1197+
> _patternTrailer_ and the length of _matchKey_ is greater than or
1198+
> equal to the length of _expansionKey_, then
1199+
> 1. Let _target_ be the value of _matchObj_\[_expansionKey_\].
1200+
> 1. Let _subpath_ be the substring of _matchKey_ starting at the
1201+
> index of the length of _patternBase_ up to the length of
1202+
> _matchKey_ minus the length of _patternTrailer_.
1203+
> 1. Let _resolved_ be the result of **PACKAGE_TARGET_RESOLVE**(
1204+
> _packageURL_, _target_, _subpath_, **true**, _isImports_,
1205+
> _conditions_).
1206+
> 1. Return the object _{ resolved, exact: **true** }_.
1207+
> 1. Otherwise if _patternBase_ is **null** and _matchKey_ starts with
1208+
> but is not equal to _expansionKey_, then
11981209
> 1. Let _target_ be the value of _matchObj_\[_expansionKey_\].
11991210
> 1. Let _subpath_ be the substring of _matchKey_ starting at the
12001211
> index of the length of _expansionKey_.
@@ -1204,6 +1215,22 @@ _isImports_, _conditions_)
12041215
> 1. Return the object _{ resolved, exact: **false** }_.
12051216
> 1. Return the object _{ resolved: **null**, exact: **true** }_.
12061217

1218+
**PATTERN_KEY_COMPARE**(_keyA_, _keyB_)
1219+
1220+
> 1. Assert: _keyA_ ends with _"/"_ or contains only a single _"*"_.
1221+
> 1. Assert: _keyB_ ends with _"/"_ or contains only a single _"*"_.
1222+
> 1. Let _baseLengthA_ be the index of _"*"_ in _keyA_ plus one, if _keyA_
1223+
> contains _"*"_, or the length of _keyA_ otherwise.
1224+
> 1. Let _baseLengthB_ be the index of _"*"_ in _keyB_ plus one, if _keyB_
1225+
> contains _"*"_, or the length of _keyB_ otherwise.
1226+
> 1. If _baseLengthA_ is greater than _baseLengthB_, return -1.
1227+
> 1. If _baseLengthB_ is greater than _baseLengthA_, return 1.
1228+
> 1. If _keyA_ does not contain _"*"_, return 1.
1229+
> 1. If _keyB_ does not contain _"*"_, return -1.
1230+
> 1. If the length of _keyA_ is greater than the length of _keyB_, return -1.
1231+
> 1. If the length of _keyB_ is greater than the length of _keyA_, return 1.
1232+
> 1. Return 0.
1233+
12071234
**PACKAGE_TARGET_RESOLVE**(_packageURL_, _target_, _subpath_, _pattern_,
12081235
_internal_, _conditions_)
12091236

lib/internal/modules/esm/resolve.js

+40-14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const {
1616
String,
1717
StringPrototypeEndsWith,
1818
StringPrototypeIndexOf,
19+
StringPrototypeLastIndexOf,
1920
StringPrototypeReplace,
2021
StringPrototypeSlice,
2122
StringPrototypeSplit,
@@ -114,7 +115,7 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
114115
`Package ${pkgPath} has a "main" field set to ${JSONStringify(main)}, ` +
115116
`excluding the full filename and extension to the resolved file at "${
116117
StringPrototypeSlice(path, pkgPath.length)}", imported from ${
117-
basePath}.\n Automatic extension resolution of the "main" field is` +
118+
basePath}.\n Automatic extension resolution of the "main" field is ` +
118119
'deprecated for ES modules.',
119120
'DeprecationWarning',
120121
'DEP0151'
@@ -607,7 +608,9 @@ function packageExportsResolve(
607608
if (isConditionalExportsMainSugar(exports, packageJSONUrl, base))
608609
exports = { '.': exports };
609610

610-
if (ObjectPrototypeHasOwnProperty(exports, packageSubpath)) {
611+
if (ObjectPrototypeHasOwnProperty(exports, packageSubpath) &&
612+
StringPrototypeIndexOf(packageSubpath, '*') === -1 &&
613+
!StringPrototypeEndsWith(packageSubpath, '/')) {
611614
const target = exports[packageSubpath];
612615
const resolved = resolvePackageTarget(
613616
packageJSONUrl, target, '', packageSubpath, base, false, false, conditions
@@ -618,30 +621,39 @@ function packageExportsResolve(
618621
}
619622

620623
let bestMatch = '';
624+
let bestMatchSubpath;
621625
const keys = ObjectGetOwnPropertyNames(exports);
622626
for (let i = 0; i < keys.length; i++) {
623627
const key = keys[i];
624-
if (key[key.length - 1] === '*' &&
628+
const patternIndex = StringPrototypeIndexOf(key, '*');
629+
if (patternIndex !== -1 &&
625630
StringPrototypeStartsWith(packageSubpath,
626-
StringPrototypeSlice(key, 0, -1)) &&
627-
packageSubpath.length >= key.length &&
628-
key.length > bestMatch.length) {
629-
bestMatch = key;
631+
StringPrototypeSlice(key, 0, patternIndex))) {
632+
const patternTrailer = StringPrototypeSlice(key, patternIndex + 1);
633+
if (packageSubpath.length >= key.length &&
634+
StringPrototypeEndsWith(packageSubpath, patternTrailer) &&
635+
patternKeyCompare(bestMatch, key) === 1 &&
636+
StringPrototypeLastIndexOf(key, '*') === patternIndex) {
637+
bestMatch = key;
638+
bestMatchSubpath = StringPrototypeSlice(
639+
packageSubpath, patternIndex,
640+
packageSubpath.length - patternTrailer.length);
641+
}
630642
} else if (key[key.length - 1] === '/' &&
631643
StringPrototypeStartsWith(packageSubpath, key) &&
632-
key.length > bestMatch.length) {
644+
packageSubpath.length > key.length &&
645+
patternKeyCompare(bestMatch, key) === 1) {
633646
bestMatch = key;
647+
bestMatchSubpath = StringPrototypeSlice(packageSubpath, key.length);
634648
}
635649
}
636650

637651
if (bestMatch) {
638652
const target = exports[bestMatch];
639-
const pattern = bestMatch[bestMatch.length - 1] === '*';
640-
const subpath = StringPrototypeSubstr(packageSubpath, bestMatch.length -
641-
(pattern ? 1 : 0));
642-
const resolved = resolvePackageTarget(packageJSONUrl, target, subpath,
643-
bestMatch, base, pattern, false,
644-
conditions);
653+
const pattern = bestMatch.indexOf('*') !== -1;
654+
const resolved = resolvePackageTarget(packageJSONUrl, target,
655+
bestMatchSubpath, bestMatch, base,
656+
pattern, false, conditions);
645657
if (resolved === null || resolved === undefined)
646658
throwExportsNotFound(packageSubpath, packageJSONUrl, base);
647659
if (!pattern)
@@ -652,6 +664,20 @@ function packageExportsResolve(
652664
throwExportsNotFound(packageSubpath, packageJSONUrl, base);
653665
}
654666

667+
function patternKeyCompare(a, b) {
668+
const aPatternIndex = StringPrototypeIndexOf(a, '*');
669+
const bPatternIndex = StringPrototypeIndexOf(b, '*');
670+
const baseLenA = aPatternIndex === -1 ? a.length : aPatternIndex + 1;
671+
const baseLenB = bPatternIndex === -1 ? b.length : bPatternIndex + 1;
672+
if (baseLenA > baseLenB) return -1;
673+
if (baseLenB > baseLenA) return 1;
674+
if (aPatternIndex === -1) return 1;
675+
if (bPatternIndex === -1) return -1;
676+
if (a.length > b.length) return -1;
677+
if (b.length > a.length) return 1;
678+
return 0;
679+
}
680+
655681
/**
656682
* @param {string} name
657683
* @param {string | URL | undefined} base

test/es-module/test-esm-exports.mjs

+9
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
3535
['pkgexports-sugar', { default: 'main' }],
3636
// Path patterns
3737
['pkgexports/subpath/sub-dir1', { default: 'main' }],
38+
['pkgexports/subpath/sub-dir1.js', { default: 'main' }],
3839
['pkgexports/features/dir1', { default: 'main' }],
40+
['pkgexports/dir1/dir1/trailer', { default: 'main' }],
41+
['pkgexports/dir2/dir2/trailer', { default: 'index' }],
42+
['pkgexports/a/dir1/dir1', { default: 'main' }],
43+
['pkgexports/a/b/dir1/dir1', { default: 'main' }],
3944
]);
4045

4146
if (isRequire) {
@@ -77,6 +82,8 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
7782
['pkgexports/null/subpath', './null/subpath'],
7883
// Empty fallback
7984
['pkgexports/nofallback1', './nofallback1'],
85+
// Non pattern matches
86+
['pkgexports/trailer', './trailer'],
8087
]);
8188

8289
const invalidExports = new Map([
@@ -147,6 +154,8 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
147154
['pkgexports/sub/not-a-file.js', `pkgexports${sep}not-a-file.js`],
148155
// No extension lookups
149156
['pkgexports/no-ext', `pkgexports${sep}asdf`],
157+
// Pattern specificity
158+
['pkgexports/dir2/trailer', 'subpath/dir2.js'],
150159
]);
151160

152161
if (!isRequire) {

test/fixtures/node_modules/pkgexports/package.json

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)