| 1 |
import * as $ from './utils'; |
| 2 |
import type * as t from 'resolve.exports'; |
| 3 |
|
| 4 |
type LegacyOptions = { |
| 5 |
fields?: string[]; |
| 6 |
browser?: string | boolean; |
| 7 |
} |
| 8 |
|
| 9 |
type BrowserObject = { |
| 10 |
[file: string]: string | undefined; |
| 11 |
} |
| 12 |
|
| 13 |
export function legacy(pkg: t.Package, options: LegacyOptions = {}): t.Path | t.Browser | void { |
| 14 |
let i=0, |
| 15 |
value: string | t.Browser | undefined, |
| 16 |
browser = options.browser, |
| 17 |
fields = options.fields || ['module', 'main'], |
| 18 |
isSTRING = typeof browser == 'string'; |
| 19 |
|
| 20 |
if (browser && !fields.includes('browser')) { |
| 21 |
fields.unshift('browser'); |
| 22 |
// "module-a" -> "module-a" |
| 23 |
// "./path/file.js" -> "./path/file.js" |
| 24 |
// "foobar/path/file.js" -> "./path/file.js" |
| 25 |
if (isSTRING) browser = $.toEntry(pkg.name, browser as string, true); |
| 26 |
} |
| 27 |
|
| 28 |
for (; i < fields.length; i++) { |
| 29 |
if (value = pkg[fields[i]]) { |
| 30 |
if (typeof value == 'string') { |
| 31 |
// |
| 32 |
} else if (typeof value == 'object' && fields[i] == 'browser') { |
| 33 |
if (isSTRING) { |
| 34 |
value = (value as BrowserObject)[browser as string]; |
| 35 |
if (value == null) return browser as string; |
| 36 |
} |
| 37 |
} else { |
| 38 |
continue; |
| 39 |
} |
| 40 |
|
| 41 |
return typeof value == 'string' |
| 42 |
? ('./' + value.replace(/^\.?\//, '')) as t.Path |
| 43 |
: value; |
| 44 |
} |
| 45 |
} |
| 46 |
} |