module_map.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. var _constants;
  6. function _load_constants() {
  7. return (_constants = _interopRequireDefault(require('./constants')));
  8. }
  9. function _interopRequireDefault(obj) {
  10. return obj && obj.__esModule ? obj : {default: obj};
  11. }
  12. const EMPTY_MAP = {};
  13. /**
  14. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  15. *
  16. * This source code is licensed under the MIT license found in the
  17. * LICENSE file in the root directory of this source tree.
  18. *
  19. *
  20. */
  21. class ModuleMap {
  22. constructor(raw) {
  23. this._raw = raw;
  24. }
  25. getModule(name, platform, supportsNativePlatform, type) {
  26. if (!type) {
  27. type = (_constants || _load_constants()).default.MODULE;
  28. }
  29. const module = this._getModuleMetadata(
  30. name,
  31. platform,
  32. !!supportsNativePlatform
  33. );
  34. if (
  35. module &&
  36. module[(_constants || _load_constants()).default.TYPE] === type
  37. ) {
  38. return module[(_constants || _load_constants()).default.PATH];
  39. }
  40. return null;
  41. }
  42. getPackage(name, platform, supportsNativePlatform) {
  43. return this.getModule(
  44. name,
  45. platform,
  46. null,
  47. (_constants || _load_constants()).default.PACKAGE
  48. );
  49. }
  50. getMockModule(name) {
  51. return this._raw.mocks[name] || this._raw.mocks[name + '/index'];
  52. }
  53. getRawModuleMap() {
  54. return {
  55. duplicates: this._raw.duplicates,
  56. map: this._raw.map,
  57. mocks: this._raw.mocks
  58. };
  59. }
  60. /**
  61. * When looking up a module's data, we walk through each eligible platform for
  62. * the query. For each platform, we want to check if there are known
  63. * duplicates for that name+platform pair. The duplication logic normally
  64. * removes elements from the `map` object, but we want to check upfront to be
  65. * extra sure. If metadata exists both in the `duplicates` object and the
  66. * `map`, this would be a bug.
  67. */
  68. _getModuleMetadata(name, platform, supportsNativePlatform) {
  69. const map = this._raw.map[name] || EMPTY_MAP;
  70. const dupMap = this._raw.duplicates[name] || EMPTY_MAP;
  71. if (platform != null) {
  72. this._assertNoDuplicates(
  73. name,
  74. platform,
  75. supportsNativePlatform,
  76. dupMap[platform]
  77. );
  78. if (map[platform] != null) {
  79. return map[platform];
  80. }
  81. }
  82. if (supportsNativePlatform) {
  83. this._assertNoDuplicates(
  84. name,
  85. (_constants || _load_constants()).default.NATIVE_PLATFORM,
  86. supportsNativePlatform,
  87. dupMap[(_constants || _load_constants()).default.NATIVE_PLATFORM]
  88. );
  89. if (map[(_constants || _load_constants()).default.NATIVE_PLATFORM]) {
  90. return map[(_constants || _load_constants()).default.NATIVE_PLATFORM];
  91. }
  92. }
  93. this._assertNoDuplicates(
  94. name,
  95. (_constants || _load_constants()).default.GENERIC_PLATFORM,
  96. supportsNativePlatform,
  97. dupMap[(_constants || _load_constants()).default.GENERIC_PLATFORM]
  98. );
  99. if (map[(_constants || _load_constants()).default.GENERIC_PLATFORM]) {
  100. return map[(_constants || _load_constants()).default.GENERIC_PLATFORM];
  101. }
  102. return null;
  103. }
  104. _assertNoDuplicates(name, platform, supportsNativePlatform, set) {
  105. if (set == null) {
  106. return;
  107. }
  108. throw new DuplicateHasteCandidatesError(
  109. name,
  110. platform,
  111. supportsNativePlatform,
  112. set
  113. );
  114. }
  115. }
  116. exports.default = ModuleMap;
  117. class DuplicateHasteCandidatesError extends Error {
  118. constructor(name, platform, supportsNativePlatform, duplicatesSet) {
  119. const platformMessage = getPlatformMessage(platform);
  120. super(
  121. `The name \`${name}\` was looked up in the Haste module map. It ` +
  122. `cannot be resolved, because there exists several different ` +
  123. `files, or packages, that provide a module for ` +
  124. `that particular name and platform. ${platformMessage} You must ` +
  125. `delete or blacklist files until there remains only one of these:\n\n` +
  126. Object.keys(duplicatesSet)
  127. .sort()
  128. .map(dupFilePath => {
  129. const typeMessage = getTypeMessage(duplicatesSet[dupFilePath]);
  130. return ` * \`${dupFilePath}\` (${typeMessage})\n`;
  131. })
  132. .join('')
  133. );
  134. this.hasteName = name;
  135. this.platform = platform;
  136. this.supportsNativePlatform = supportsNativePlatform;
  137. this.duplicatesSet = duplicatesSet;
  138. }
  139. }
  140. function getPlatformMessage(platform) {
  141. if (platform === (_constants || _load_constants()).default.GENERIC_PLATFORM) {
  142. return 'The platform is generic (no extension).';
  143. }
  144. return `The platform extension is \`${platform}\`.`;
  145. }
  146. function getTypeMessage(type) {
  147. switch (type) {
  148. case (_constants || _load_constants()).default.MODULE:
  149. return 'module';
  150. case (_constants || _load_constants()).default.PACKAGE:
  151. return 'package';
  152. }
  153. return 'unknown';
  154. }
  155. ModuleMap.DuplicateHasteCandidatesError = DuplicateHasteCandidatesError;