index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. if (exports) {
  10. let idx, tmp;
  11. if (Array.isArray(exports)) {
  12. for (idx=0; idx < exports.length; idx++) {
  13. if (tmp = loop(exports[idx], keys)) return tmp;
  14. }
  15. } else {
  16. for (idx in exports) {
  17. if (keys.has(idx)) {
  18. return loop(exports[idx], keys);
  19. }
  20. }
  21. }
  22. }
  23. }
  24. /**
  25. * @param {string} name The package name
  26. * @param {string} entry The target entry, eg "."
  27. * @param {number} [condition] Unmatched condition?
  28. */
  29. function bail(name, entry, condition) {
  30. throw new Error(
  31. condition
  32. ? `No known conditions for "${entry}" entry in "${name}" package`
  33. : `Missing "${entry}" export in "${name}" package`
  34. );
  35. }
  36. /**
  37. * @param {string} name the package name
  38. * @param {string} entry the target path/import
  39. */
  40. function toName(name, entry) {
  41. return entry === name ? '.'
  42. : entry[0] === '.' ? entry
  43. : entry.replace(new RegExp('^' + name + '\/'), './');
  44. }
  45. /**
  46. * @param {object} pkg package.json contents
  47. * @param {string} [entry] entry name or import path
  48. * @param {object} [options]
  49. * @param {boolean} [options.browser]
  50. * @param {boolean} [options.require]
  51. * @param {string[]} [options.conditions]
  52. * @param {boolean} [options.unsafe]
  53. */
  54. function resolve(pkg, entry='.', options={}) {
  55. let { name, exports } = pkg;
  56. if (exports) {
  57. let { browser, require, unsafe, conditions=[] } = options;
  58. let target = toName(name, entry);
  59. if (target[0] !== '.') target = './' + target;
  60. if (typeof exports === 'string') {
  61. return target === '.' ? exports : bail(name, target);
  62. }
  63. let allows = new Set(['default', ...conditions]);
  64. unsafe || allows.add(require ? 'require' : 'import');
  65. unsafe || allows.add(browser ? 'browser' : 'node');
  66. let key, tmp, isSingle=false;
  67. for (key in exports) {
  68. isSingle = key[0] !== '.';
  69. break;
  70. }
  71. if (isSingle) {
  72. return target === '.'
  73. ? loop(exports, allows) || bail(name, target, 1)
  74. : bail(name, target);
  75. }
  76. if (tmp = exports[target]) {
  77. return loop(tmp, allows) || bail(name, target, 1);
  78. }
  79. for (key in exports) {
  80. tmp = key[key.length - 1];
  81. if (tmp === '/' && target.startsWith(key)) {
  82. return (tmp = loop(exports[key], allows))
  83. ? (tmp + target.substring(key.length))
  84. : bail(name, target, 1);
  85. }
  86. if (tmp === '*' && target.startsWith(key.slice(0, -1))) {
  87. // do not trigger if no *content* to inject
  88. if (target.substring(key.length - 1).length > 0) {
  89. return (tmp = loop(exports[key], allows))
  90. ? tmp.replace('*', target.substring(key.length - 1))
  91. : bail(name, target, 1);
  92. }
  93. }
  94. }
  95. return bail(name, target);
  96. }
  97. }
  98. /**
  99. * @param {object} pkg
  100. * @param {object} [options]
  101. * @param {string|boolean} [options.browser]
  102. * @param {string[]} [options.fields]
  103. */
  104. function legacy(pkg, options={}) {
  105. let i=0, value,
  106. browser = options.browser,
  107. fields = options.fields || ['module', 'main'];
  108. if (browser && !fields.includes('browser')) {
  109. fields.unshift('browser');
  110. }
  111. for (; i < fields.length; i++) {
  112. if (value = pkg[fields[i]]) {
  113. if (typeof value == 'string') {
  114. //
  115. } else if (typeof value == 'object' && fields[i] == 'browser') {
  116. if (typeof browser == 'string') {
  117. value = value[browser=toName(pkg.name, browser)];
  118. if (value == null) return browser;
  119. }
  120. } else {
  121. continue;
  122. }
  123. return typeof value == 'string'
  124. ? ('./' + value.replace(/^\.?\//, ''))
  125. : value;
  126. }
  127. }
  128. }
  129. exports.legacy = legacy;
  130. exports.resolve = resolve;