index.js
3.5 KB
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
140
141
142
143
144
145
146
147
148
149
/**
* @param {object} exports
* @param {Set<string>} keys
*/
function loop(exports, keys) {
if (typeof exports === 'string') {
return exports;
}
if (exports) {
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);
}
}
}
}
}
/**
* @param {string} name The package name
* @param {string} entry The target entry, eg "."
* @param {number} [condition] Unmatched condition?
*/
function bail(name, entry, condition) {
throw new Error(
condition
? `No known conditions for "${entry}" entry in "${name}" package`
: `Missing "${entry}" export in "${name}" package`
);
}
/**
* @param {string} name the package name
* @param {string} entry the target path/import
*/
function toName(name, entry) {
return entry === name ? '.'
: entry[0] === '.' ? entry
: entry.replace(new RegExp('^' + name + '\/'), './');
}
/**
* @param {object} pkg package.json contents
* @param {string} [entry] entry name or import path
* @param {object} [options]
* @param {boolean} [options.browser]
* @param {boolean} [options.require]
* @param {string[]} [options.conditions]
* @param {boolean} [options.unsafe]
*/
function resolve(pkg, entry='.', options={}) {
let { name, exports } = pkg;
if (exports) {
let { browser, require, unsafe, conditions=[] } = options;
let target = toName(name, entry);
if (target[0] !== '.') target = './' + target;
if (typeof exports === 'string') {
return target === '.' ? exports : bail(name, target);
}
let allows = new Set(['default', ...conditions]);
unsafe || allows.add(require ? 'require' : 'import');
unsafe || allows.add(browser ? 'browser' : 'node');
let key, tmp, isSingle=false;
for (key in exports) {
isSingle = key[0] !== '.';
break;
}
if (isSingle) {
return target === '.'
? loop(exports, allows) || bail(name, target, 1)
: bail(name, target);
}
if (tmp = exports[target]) {
return loop(tmp, allows) || bail(name, target, 1);
}
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);
}
}
}
return bail(name, target);
}
}
/**
* @param {object} pkg
* @param {object} [options]
* @param {string|boolean} [options.browser]
* @param {string[]} [options.fields]
*/
function legacy(pkg, options={}) {
let i=0, value,
browser = options.browser,
fields = options.fields || ['module', 'main'];
if (browser && !fields.includes('browser')) {
fields.unshift('browser');
}
for (; i < fields.length; i++) {
if (value = pkg[fields[i]]) {
if (typeof value == 'string') {
//
} else if (typeof value == 'object' && fields[i] == 'browser') {
if (typeof browser == 'string') {
value = value[browser=toName(pkg.name, browser)];
if (value == null) return browser;
}
} else {
continue;
}
return typeof value == 'string'
? ('./' + value.replace(/^\.?\//, ''))
: value;
}
}
}
exports.legacy = legacy;
exports.resolve = resolve;