| 1 | /**
|
|---|
| 2 | * @param {object} exports
|
|---|
| 3 | * @param {Set<string>} keys
|
|---|
| 4 | */
|
|---|
| 5 | function loop(exports, keys) {
|
|---|
| 6 | if (typeof exports === 'string') {
|
|---|
| 7 | return exports;
|
|---|
| 8 | }
|
|---|
| 9 |
|
|---|
| 10 | if (exports) {
|
|---|
| 11 | let idx, tmp;
|
|---|
| 12 | if (Array.isArray(exports)) {
|
|---|
| 13 | for (idx=0; idx < exports.length; idx++) {
|
|---|
| 14 | if (tmp = loop(exports[idx], keys)) return tmp;
|
|---|
| 15 | }
|
|---|
| 16 | } else {
|
|---|
| 17 | for (idx in exports) {
|
|---|
| 18 | if (keys.has(idx)) {
|
|---|
| 19 | return loop(exports[idx], keys);
|
|---|
| 20 | }
|
|---|
| 21 | }
|
|---|
| 22 | }
|
|---|
| 23 | }
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * @param {string} name The package name
|
|---|
| 28 | * @param {string} entry The target entry, eg "."
|
|---|
| 29 | * @param {number} [condition] Unmatched condition?
|
|---|
| 30 | */
|
|---|
| 31 | function bail(name, entry, condition) {
|
|---|
| 32 | throw new Error(
|
|---|
| 33 | condition
|
|---|
| 34 | ? `No known conditions for "${entry}" entry in "${name}" package`
|
|---|
| 35 | : `Missing "${entry}" export in "${name}" package`
|
|---|
| 36 | );
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * @param {string} name the package name
|
|---|
| 41 | * @param {string} entry the target path/import
|
|---|
| 42 | */
|
|---|
| 43 | function toName(name, entry) {
|
|---|
| 44 | return entry === name ? '.'
|
|---|
| 45 | : entry[0] === '.' ? entry
|
|---|
| 46 | : entry.replace(new RegExp('^' + name + '\/'), './');
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * @param {object} pkg package.json contents
|
|---|
| 51 | * @param {string} [entry] entry name or import path
|
|---|
| 52 | * @param {object} [options]
|
|---|
| 53 | * @param {boolean} [options.browser]
|
|---|
| 54 | * @param {boolean} [options.require]
|
|---|
| 55 | * @param {string[]} [options.conditions]
|
|---|
| 56 | * @param {boolean} [options.unsafe]
|
|---|
| 57 | */
|
|---|
| 58 | function resolve(pkg, entry='.', options={}) {
|
|---|
| 59 | let { name, exports } = pkg;
|
|---|
| 60 |
|
|---|
| 61 | if (exports) {
|
|---|
| 62 | let { browser, require, unsafe, conditions=[] } = options;
|
|---|
| 63 |
|
|---|
| 64 | let target = toName(name, entry);
|
|---|
| 65 | if (target !== '.' && !target.startsWith('./')) {
|
|---|
| 66 | target = './' + target; // ".ini" => "./.ini"
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | if (typeof exports === 'string') {
|
|---|
| 70 | return target === '.' ? exports : bail(name, target);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | let allows = new Set(['default', ...conditions]);
|
|---|
| 74 | unsafe || allows.add(require ? 'require' : 'import');
|
|---|
| 75 | unsafe || allows.add(browser ? 'browser' : 'node');
|
|---|
| 76 |
|
|---|
| 77 | let key, m, k, kv, tmp, isSingle=false;
|
|---|
| 78 |
|
|---|
| 79 | for (key in exports) {
|
|---|
| 80 | isSingle = key[0] !== '.';
|
|---|
| 81 | break;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | if (isSingle) {
|
|---|
| 85 | return target === '.'
|
|---|
| 86 | ? loop(exports, allows) || bail(name, target, 1)
|
|---|
| 87 | : bail(name, target);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | if (tmp = exports[target]) {
|
|---|
| 91 | return loop(tmp, allows) || bail(name, target, 1);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | if (target !== '.') {
|
|---|
| 95 | for (key in exports) {
|
|---|
| 96 | if (k && key.length < k.length) {
|
|---|
| 97 | // do not allow "./" to match if already matched "./foo*" key
|
|---|
| 98 | } else if (key[key.length - 1] === '/' && target.startsWith(key)) {
|
|---|
| 99 | kv = target.substring(key.length);
|
|---|
| 100 | k = key;
|
|---|
| 101 | } else {
|
|---|
| 102 | tmp = key.indexOf('*', 2);
|
|---|
| 103 | if (!!~tmp) {
|
|---|
| 104 | m = RegExp(
|
|---|
| 105 | '^\.\/' + key.substring(2, tmp) + '(.*)' + key.substring(1+tmp)
|
|---|
| 106 | ).exec(target);
|
|---|
| 107 |
|
|---|
| 108 | if (m && m[1]) {
|
|---|
| 109 | kv = m[1];
|
|---|
| 110 | k = key;
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | if (k && kv) {
|
|---|
| 117 | // must have value
|
|---|
| 118 | tmp = loop(exports[k], allows);
|
|---|
| 119 | if (!tmp) return bail(name, target);
|
|---|
| 120 |
|
|---|
| 121 | return tmp.includes('*')
|
|---|
| 122 | ? tmp.replace(/[*]/g, kv)
|
|---|
| 123 | : tmp + kv;
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | return bail(name, target);
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /**
|
|---|
| 132 | * @param {object} pkg
|
|---|
| 133 | * @param {object} [options]
|
|---|
| 134 | * @param {string|boolean} [options.browser]
|
|---|
| 135 | * @param {string[]} [options.fields]
|
|---|
| 136 | */
|
|---|
| 137 | function legacy(pkg, options={}) {
|
|---|
| 138 | let i=0, value,
|
|---|
| 139 | browser = options.browser,
|
|---|
| 140 | fields = options.fields || ['module', 'main'];
|
|---|
| 141 |
|
|---|
| 142 | if (browser && !fields.includes('browser')) {
|
|---|
| 143 | fields.unshift('browser');
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | for (; i < fields.length; i++) {
|
|---|
| 147 | if (value = pkg[fields[i]]) {
|
|---|
| 148 | if (typeof value == 'string') {
|
|---|
| 149 | //
|
|---|
| 150 | } else if (typeof value == 'object' && fields[i] == 'browser') {
|
|---|
| 151 | if (typeof browser == 'string') {
|
|---|
| 152 | value = value[browser=toName(pkg.name, browser)];
|
|---|
| 153 | if (value == null) return browser;
|
|---|
| 154 | }
|
|---|
| 155 | } else {
|
|---|
| 156 | continue;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | return typeof value == 'string'
|
|---|
| 160 | ? ('./' + value.replace(/^\.?\//, ''))
|
|---|
| 161 | : value;
|
|---|
| 162 | }
|
|---|
| 163 | }
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | exports.legacy = legacy;
|
|---|
| 167 | exports.resolve = resolve; |
|---|