Skip to content

Commit 2c36596

Browse files
guybedfordrichardlau
authored andcommitted
module: support pattern trailers for imports field
PR-URL: #40041 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]>
1 parent 607bc74 commit 2c36596

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

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

Lines 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
constinternalFS=require('internal/fs/utils');
2827
const{ NativeModule }=require('internal/bootstrap/loaders');
@@ -582,38 +581,49 @@ function packageImportsResolve(name, base, conditions){
582581
packageJSONUrl=pathToFileURL(packageConfig.pjsonPath);
583582
constimports=packageConfig.imports;
584583
if(imports){
585-
if(ObjectPrototypeHasOwnProperty(imports,name)){
584+
if(ObjectPrototypeHasOwnProperty(imports,name)&&
585+
!StringPrototypeIncludes(name,'*')&&
586+
!StringPrototypeEndsWith(name,'/')){
586587
constresolved=resolvePackageTarget(
587588
packageJSONUrl,imports[name],'',name,base,false,true,conditions
588589
);
589590
if(resolved!==null)
590591
return{ resolved,exact: true};
591592
}else{
592593
letbestMatch='';
594+
letbestMatchSubpath;
593595
constkeys=ObjectGetOwnPropertyNames(imports);
594596
for(leti=0;i<keys.length;i++){
595597
constkey=keys[i];
596-
if(key[key.length-1]==='*'&&
598+
constpatternIndex=StringPrototypeIndexOf(key,'*');
599+
if(patternIndex!==-1&&
597600
StringPrototypeStartsWith(name,
598-
StringPrototypeSlice(key,0,-1))&&
599-
name.length>=key.length&&
600-
key.length>bestMatch.length){
601-
bestMatch=key;
601+
StringPrototypeSlice(key,0,
602+
patternIndex))){
603+
constpatternTrailer=StringPrototypeSlice(key,patternIndex+1);
604+
if(name.length>=key.length&&
605+
StringPrototypeEndsWith(name,patternTrailer)&&
606+
patternKeyCompare(bestMatch,key)===1&&
607+
StringPrototypeLastIndexOf(key,'*')===patternIndex){
608+
bestMatch=key;
609+
bestMatchSubpath=StringPrototypeSlice(
610+
name,patternIndex,name.length-patternTrailer.length);
611+
}
602612
}elseif(key[key.length-1]==='/'&&
603613
StringPrototypeStartsWith(name,key)&&
604-
key.length>bestMatch.length){
614+
patternKeyCompare(bestMatch,key)===1){
605615
bestMatch=key;
616+
bestMatchSubpath=StringPrototypeSlice(name,key.length);
606617
}
607618
}
608619

609620
if(bestMatch){
610621
consttarget=imports[bestMatch];
611-
constpattern=bestMatch[bestMatch.length-1]==='*';
612-
constsubpath=StringPrototypeSubstr(name,bestMatch.length-
613-
(pattern ? 1 : 0));
614-
constresolved=resolvePackageTarget(
615-
packageJSONUrl,target,subpath,bestMatch,base,pattern,true,
616-
conditions);
622+
constpattern=StringPrototypeIncludes(bestMatch,'*');
623+
constresolved=resolvePackageTarget(packageJSONUrl,target,
624+
bestMatchSubpath,bestMatch,
625+
base,pattern,true,
626+
conditions);
617627
if(resolved!==null)
618628
return{ resolved,exact: pattern};
619629
}

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

Lines 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]ofinternalImports){

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

Lines 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)