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

Advanced Wildcard Handling #21

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 67 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,66 @@ function toName(name, entry) {
: entry.replace(new RegExp('^' + name + '\/'), './');
}

const asterisks = /\*+/;
const asterisksg = /\*+/g;

/**
*
* @param {string} request the request to match
* @param {string} path module template path (ex dir/*.mjs)
*/
function asWildMatch(request, path) {
if (!asterisks.test(path)) {
return;
}

const tokens = path.split(asterisks);
const last = tokens.pop();
if (!request.endsWith(last)) {
return false;
}

const first = tokens.shift();
if (!request.startsWith(first)) {
return false;
}

request = request.substring(first.length, request.length - last.length);

let i, tmp;
const values = [];
while (tmp = tokens.shift()) {
i = request.indexOf(tmp);

// empty values are not allowed.
if (i <= 0) {
return;
}

values.push(request.substring(0, i));
request = request.substring(i + tmp.length);
}

// empty values are not allowed.
if (!request) {
return false;
}

values.push(request);

return values;
}

/**
* @param {string} path module template path (ex dir/*.mjs)
* @param {string[]} values replacements for wildcard placeholders
*/
function replaceWilds(path, values) {
return path.replace(asterisksg, function () {
return values.shift() || "";
});
}

/**
* @param {object} pkg package.json contents
* @param {string} [entry] entry name or import path
Expand All @@ -72,7 +132,7 @@ export function resolve(pkg, entry='.', options={}) {
unsafe || allows.add(require ? 'require' : 'import');
unsafe || allows.add(browser ? 'browser' : 'node');

let key, tmp, isSingle=false;
let key, tmp, wilds, isSingle=false;

for (key in exports) {
isSingle = key[0] !== '.';
Expand All @@ -89,20 +149,18 @@ export function resolve(pkg, entry='.', options={}) {
return loop(tmp, allows) || bail(name, target, 1);
}

for (key in exports) {
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 (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 (wilds = asWildMatch(target, key)) {
return (tmp = loop(exports[key], allows))
? replaceWilds(tmp, wilds)
: bail(name, target, 1);
}
}

Expand Down
28 changes: 28 additions & 0 deletions test/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,34 @@ resolve('exports["./features/*"] :: conditions', () => {
pass(pkg, './browser.require/hello.js', 'foobar/features/hello', { browser: true, require: true });
});

resolve('advanced wildcard handling (x * y)', () => {
let pkg = {
"name": "foobar",
"exports": {
"./features/*/index": {
"import": "./import/*.mjs",
},
}
};

pass(pkg, './import/hello.mjs', './features/hello/index');
pass(pkg, './import/hello.mjs', 'foobar/features/hello/index');
});

resolve('advanced wildcard handling (x * y * z)', () => {
let pkg = {
"name": "foobar",
"exports": {
"./features/*/*/index": {
"import": "./import/*/dist/*.mjs",
},
}
};

pass(pkg, './import/debug/dist/hello.mjs', './features/debug/hello/index');
pass(pkg, './import/debug/dist/hello.mjs', 'foobar/features/debug/hello/index');
});

resolve('should handle mixed path/conditions', () => {
let pkg = {
"name": "foobar",
Expand Down