module_map.js 4.6 KB

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