| 1 |
import type * as t from 'resolve.exports'; |
| 2 |
|
| 3 |
export type Entry = t.Exports.Entry | t.Imports.Entry; |
| 4 |
export type Value = t.Exports.Value | t.Imports.Value; |
| 5 |
export type Mapping = Record<Entry, Value>; |
| 6 |
|
| 7 |
export function throws(name: string, entry: Entry, condition?: number): never { |
| 8 |
throw new Error( |
| 9 |
condition |
| 10 |
? `No known conditions for "${entry}" specifier in "${name}" package` |
| 11 |
: `Missing "${entry}" specifier in "${name}" package` |
| 12 |
); |
| 13 |
} |
| 14 |
|
| 15 |
export function conditions(options: t.Options): Set<t.Condition> { |
| 16 |
let out = new Set([ 'default', ...options.conditions || [] ]); |
| 17 |
options.unsafe || out.add(options.require ? 'require' : 'import'); |
| 18 |
options.unsafe || out.add(options.browser ? 'browser' : 'node'); |
| 19 |
return out; |
| 20 |
} |
| 21 |
|
| 22 |
export function walk(name: string, mapping: Mapping, input: string, options?: t.Options): string[] { |
| 23 |
let entry = toEntry(name, input); |
| 24 |
let c = conditions(options || {}); |
| 25 |
|
| 26 |
let m: Value|void = mapping[entry]; |
| 27 |
let v: string[]|void, replace: string|void; |
| 28 |
|
| 29 |
if (m === void 0) { |
| 30 |
// loop for longest key match |
| 31 |
let match: RegExpExecArray|null; |
| 32 |
let longest: Entry|undefined; |
| 33 |
let tmp: string|number; |
| 34 |
let key: Entry; |
| 35 |
|
| 36 |
for (key in mapping) { |
| 37 |
if (longest && key.length < longest.length) { |
| 38 |
// do not allow "./" to match if already matched "./foo*" key |
| 39 |
} else if (key[key.length - 1] === '/' && entry.startsWith(key)) { |
| 40 |
replace = entry.substring(key.length); |
| 41 |
longest = key; |
| 42 |
} else if (key.length > 1) { |
| 43 |
tmp = key.indexOf('*', 1); |
| 44 |
|
| 45 |
if (!!~tmp) { |
| 46 |
match = RegExp( |
| 47 |
'^' + key.substring(0, tmp) + '(.*)' + key.substring(1+tmp) + '$' |
| 48 |
).exec(entry); |
| 49 |
|
| 50 |
if (match && match[1]) { |
| 51 |
replace = match[1]; |
| 52 |
longest = key; |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
m = mapping[longest!]; |
| 59 |
} |
| 60 |
|
| 61 |
if (!m) { |
| 62 |
// missing export |
| 63 |
throws(name, entry); |
| 64 |
} |
| 65 |
|
| 66 |
v = loop(m, c); |
| 67 |
|
| 68 |
// unknown condition(s) |
| 69 |
if (!v) throws(name, entry, 1); |
| 70 |
if (replace) injects(v, replace); |
| 71 |
|
| 72 |
return v; |
| 73 |
} |
| 74 |
|
| 75 |
/** @note: mutates! */ |
| 76 |
export function injects(items: string[], value: string): void { |
| 77 |
let i=0, len=items.length, tmp: string; |
| 78 |
let rgx1=/[*]/g, rgx2 = /[/]$/; |
| 79 |
|
| 80 |
for (; i < len; i++) { |
| 81 |
items[i] = rgx1.test(tmp = items[i]) |
| 82 |
? tmp.replace(rgx1, value) |
| 83 |
: rgx2.test(tmp) |
| 84 |
? (tmp+value) |
| 85 |
: tmp; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* @param name package name |
| 91 |
* @param ident entry identifier |
| 92 |
* @param externals allow non-path (external) result |
| 93 |
* @see https://esbench.com/bench/59fa3e6799634800a0349382 |
| 94 |
*/ |
| 95 |
export function toEntry(name: string, ident: string, externals?: false): Entry; |
| 96 |
export function toEntry(name: string, ident: string, externals: true): Entry | string; |
| 97 |
export function toEntry(name: string, ident: string, externals?: boolean): Entry | string { |
| 98 |
if (name === ident || ident === '.') return '.'; |
| 99 |
|
| 100 |
let root = name+'/', len = root.length; |
| 101 |
let bool = ident.slice(0, len) === root; |
| 102 |
|
| 103 |
let output = bool ? ident.slice(len) : ident; |
| 104 |
if (output[0] === '#') return output as t.Imports.Entry; |
| 105 |
|
| 106 |
return (bool || !externals) |
| 107 |
? (output.slice(0,2) === './' ? output : './' + output) as t.Path |
| 108 |
: output as string | t.Exports.Entry; |
| 109 |
} |
| 110 |
|
| 111 |
export function loop(m: Value, keys: Set<t.Condition>, result?: Set<string>): string[] | void { |
| 112 |
if (m) { |
| 113 |
if (typeof m === 'string') { |
| 114 |
if (result) result.add(m); |
| 115 |
return [m]; |
| 116 |
} |
| 117 |
|
| 118 |
let |
| 119 |
idx: number | string, |
| 120 |
arr: Set<string>; |
| 121 |
|
| 122 |
if (Array.isArray(m)) { |
| 123 |
arr = result || new Set; |
| 124 |
|
| 125 |
for (idx=0; idx < m.length; idx++) { |
| 126 |
loop(m[idx], keys, arr); |
| 127 |
} |
| 128 |
|
| 129 |
// return if initialized set |
| 130 |
if (!result && arr.size) { |
| 131 |
return [...arr]; |
| 132 |
} |
| 133 |
} else for (idx in m) { |
| 134 |
if (keys.has(idx)) { |
| 135 |
return loop(m[idx], keys, result); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |