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