Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: take longest matching path instead of first match #8

Closed
wants to merge 1 commit into from
Closed
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
27 changes: 15 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function resolve(pkg, entry='.', options={}) {
allows.add(require ? 'require' : 'import');
allows.add(browser ? 'browser' : 'node');

let key, tmp, isSingle=false;
let key, tmp, isSingle=false, longestMatch='';

for (key in exports) {
isSingle = key[0] !== '.';
Expand All @@ -90,18 +90,21 @@ export function resolve(pkg, entry='.', options={}) {

for (key in exports) {
tmp = key[key.length - 1];
if (tmp === '/' && target.startsWith(key)) {
return (tmp = loop(exports[key], allows))
? (tmp + target.substring(key.length))
: bail(name, target, 1);
if (key.length <= target.length && key.length > longestMatch.length && (
tmp === '/' && target.startsWith(key) ||
tmp === '*' && target.startsWith(key.slice(0, -1))
)) {
longestMatch = key;
}
if (tmp === '*' && target.startsWith(key.slice(0, -1))) {
// do not trigger if no *content* to inject
if (target.substring(key.length - 1).length > 0) {
return (tmp = loop(exports[key], allows))
? tmp.replace('*', target.substring(key.length - 1))
: bail(name, target, 1);
}
}

if (longestMatch) {
if (tmp = loop(exports[longestMatch], allows)) {
return (longestMatch[longestMatch.length - 1] === '/')
? (tmp + target.slice(longestMatch.length))
: tmp.replace('*', target.slice(longestMatch.length - 1));
} else {
return bail(name, target, 1);
}
}

Expand Down
27 changes: 27 additions & 0 deletions test/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,33 @@ resolve('exports["./features/*"] :: with "./" key', () => {
fail(pkg, '.', 'foobar');
});

resolve('exports["./features/*"] :: with unsorted keys', () => {
let pkg = {
"name": "foobar",
"exports": {
"./features/*": "./features/*.js",
"./": "./",
"./features/data/*": "./features/data/*.json"
}
};

pass(pkg, './features', 'features'); // via "./"
pass(pkg, './features', 'foobar/features'); // via "./"

pass(pkg, './features/', 'features/'); // via "./"
pass(pkg, './features/', 'foobar/features/'); // via "./"

pass(pkg, './features/hello.js', 'foobar/features/hello'); // via "./features/*"
pass(pkg, './features/world.js', 'foobar/features/world'); // via "./features/*""

pass(pkg, './features/data/hello.json', 'foobar/features/data/hello'); // via "./features/data/*"
pass(pkg, './features/data/world.json', 'foobar/features/data/world'); // via "./features/data/*"

pass(pkg, './package.json', 'package.json'); // via "./"
pass(pkg, './package.json', 'foobar/package.json'); // via "./"
pass(pkg, './package.json', './package.json'); // via "./"
});

resolve('exports["./features/*"] :: conditions', () => {
let pkg = {
"name": "foobar",
Expand Down