Skip to content

Commit

Permalink
feat: support mixed path/conditions (array)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Jan 9, 2021
1 parent 32dc6ab commit f74e3c5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
1 change: 0 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export interface Options {
fields?: string[];
}

// TODO: arrayable
export function resolve<T=any>(pkg: T, entry: string, options?: Options): string;

// TODO
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
- [x] exports object (single entry)
- [x] exports object (multi entry)
- [x] nested conditions
- [ ] exports arrayable
- [x] exports arrayable
- [x] directory mapping (`./foobar/` => `/foobar/`)
- [x] directory mapping (`./foobar/*` => `./other/*.js`)
- [ ] legacy fields
Expand Down
14 changes: 10 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/**
* @todo arrayable
* @param {object} exports
* @param {Set<string>} keys
*/
Expand All @@ -9,9 +8,16 @@ function loop(exports, keys) {
}

if (exports) {
for (let key in exports) {
if (keys.has(key)) {
return loop(exports[key], keys);
let idx, tmp;
if (Array.isArray(exports)) {
for (idx=0; idx < exports.length; idx++) {
if (tmp = loop(exports[idx], keys)) return tmp;
}
} else {
for (idx in exports) {
if (keys.has(idx)) {
return loop(exports[idx], keys);
}
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,37 @@ resolve('exports["./features/*"] :: with "./" key', () => {
fail(pkg, '.', 'foobar');
});

resolve('should handle mixed path/conditions', () => {
let pkg = {
"name": "foobar",
"exports": {
".": [
{
"import": "$root.import",
},
"$root.string"
],
"./foo": [
{
"require": "$foo.require"
},
"$foo.string"
]
}
}

pass(pkg, '$root.import');
pass(pkg, '$root.import', 'foobar');

pass(pkg, '$foo.string', 'foo');
pass(pkg, '$foo.string', 'foobar/foo');
pass(pkg, '$foo.string', './foo');

pass(pkg, '$foo.require', 'foo', { requires: true });
pass(pkg, '$foo.require', 'foobar/foo', { requires: true });
pass(pkg, '$foo.require', './foo', { requires: true });
});

resolve.run();

// ---
Expand Down

0 comments on commit f74e3c5

Please sign in to comment.