ModuleMap.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var fastPath = _interopRequireWildcard(require('./lib/fast_path'));
  7. var _constants = _interopRequireDefault(require('./constants'));
  8. function _interopRequireDefault(obj) {
  9. return obj && obj.__esModule ? obj : {default: obj};
  10. }
  11. function _interopRequireWildcard(obj) {
  12. if (obj && obj.__esModule) {
  13. return obj;
  14. } else {
  15. var newObj = {};
  16. if (obj != null) {
  17. for (var key in obj) {
  18. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  19. var desc =
  20. Object.defineProperty && Object.getOwnPropertyDescriptor
  21. ? Object.getOwnPropertyDescriptor(obj, key)
  22. : {};
  23. if (desc.get || desc.set) {
  24. Object.defineProperty(newObj, key, desc);
  25. } else {
  26. newObj[key] = obj[key];
  27. }
  28. }
  29. }
  30. }
  31. newObj.default = obj;
  32. return newObj;
  33. }
  34. }
  35. function _slicedToArray(arr, i) {
  36. return (
  37. _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest()
  38. );
  39. }
  40. function _nonIterableRest() {
  41. throw new TypeError('Invalid attempt to destructure non-iterable instance');
  42. }
  43. function _iterableToArrayLimit(arr, i) {
  44. var _arr = [];
  45. var _n = true;
  46. var _d = false;
  47. var _e = undefined;
  48. try {
  49. for (
  50. var _i = arr[Symbol.iterator](), _s;
  51. !(_n = (_s = _i.next()).done);
  52. _n = true
  53. ) {
  54. _arr.push(_s.value);
  55. if (i && _arr.length === i) break;
  56. }
  57. } catch (err) {
  58. _d = true;
  59. _e = err;
  60. } finally {
  61. try {
  62. if (!_n && _i['return'] != null) _i['return']();
  63. } finally {
  64. if (_d) throw _e;
  65. }
  66. }
  67. return _arr;
  68. }
  69. function _arrayWithHoles(arr) {
  70. if (Array.isArray(arr)) return arr;
  71. }
  72. function _defineProperty(obj, key, value) {
  73. if (key in obj) {
  74. Object.defineProperty(obj, key, {
  75. value: value,
  76. enumerable: true,
  77. configurable: true,
  78. writable: true
  79. });
  80. } else {
  81. obj[key] = value;
  82. }
  83. return obj;
  84. }
  85. const EMPTY_OBJ = {};
  86. const EMPTY_MAP = new Map();
  87. class ModuleMap {
  88. static mapToArrayRecursive(map) {
  89. let arr = Array.from(map);
  90. if (arr[0] && arr[0][1] instanceof Map) {
  91. arr = arr.map(el => [el[0], this.mapToArrayRecursive(el[1])]);
  92. }
  93. return arr;
  94. }
  95. static mapFromArrayRecursive(arr) {
  96. if (arr[0] && Array.isArray(arr[1])) {
  97. arr = arr.map(el => [el[0], this.mapFromArrayRecursive(el[1])]);
  98. }
  99. return new Map(arr);
  100. }
  101. constructor(raw) {
  102. _defineProperty(this, '_raw', void 0);
  103. _defineProperty(this, 'json', void 0);
  104. this._raw = raw;
  105. }
  106. getModule(name, platform, supportsNativePlatform, type) {
  107. if (type == null) {
  108. type = _constants.default.MODULE;
  109. }
  110. const module = this._getModuleMetadata(
  111. name,
  112. platform,
  113. !!supportsNativePlatform
  114. );
  115. if (module && module[_constants.default.TYPE] === type) {
  116. const modulePath = module[_constants.default.PATH];
  117. return modulePath && fastPath.resolve(this._raw.rootDir, modulePath);
  118. }
  119. return null;
  120. }
  121. getPackage(name, platform, _supportsNativePlatform) {
  122. return this.getModule(name, platform, null, _constants.default.PACKAGE);
  123. }
  124. getMockModule(name) {
  125. const mockPath =
  126. this._raw.mocks.get(name) || this._raw.mocks.get(name + '/index');
  127. return mockPath && fastPath.resolve(this._raw.rootDir, mockPath);
  128. }
  129. getRawModuleMap() {
  130. return {
  131. duplicates: this._raw.duplicates,
  132. map: this._raw.map,
  133. mocks: this._raw.mocks,
  134. rootDir: this._raw.rootDir
  135. };
  136. }
  137. toJSON() {
  138. if (!this.json) {
  139. this.json = {
  140. duplicates: ModuleMap.mapToArrayRecursive(this._raw.duplicates),
  141. map: Array.from(this._raw.map),
  142. mocks: Array.from(this._raw.mocks),
  143. rootDir: this._raw.rootDir
  144. };
  145. }
  146. return this.json;
  147. }
  148. static fromJSON(serializableModuleMap) {
  149. return new ModuleMap({
  150. duplicates: ModuleMap.mapFromArrayRecursive(
  151. serializableModuleMap.duplicates
  152. ),
  153. map: new Map(serializableModuleMap.map),
  154. mocks: new Map(serializableModuleMap.mocks),
  155. rootDir: serializableModuleMap.rootDir
  156. });
  157. }
  158. /**
  159. * When looking up a module's data, we walk through each eligible platform for
  160. * the query. For each platform, we want to check if there are known
  161. * duplicates for that name+platform pair. The duplication logic normally
  162. * removes elements from the `map` object, but we want to check upfront to be
  163. * extra sure. If metadata exists both in the `duplicates` object and the
  164. * `map`, this would be a bug.
  165. */
  166. _getModuleMetadata(name, platform, supportsNativePlatform) {
  167. const map = this._raw.map.get(name) || EMPTY_OBJ;
  168. const dupMap = this._raw.duplicates.get(name) || EMPTY_MAP;
  169. if (platform != null) {
  170. this._assertNoDuplicates(
  171. name,
  172. platform,
  173. supportsNativePlatform,
  174. dupMap.get(platform)
  175. );
  176. if (map[platform] != null) {
  177. return map[platform];
  178. }
  179. }
  180. if (supportsNativePlatform) {
  181. this._assertNoDuplicates(
  182. name,
  183. _constants.default.NATIVE_PLATFORM,
  184. supportsNativePlatform,
  185. dupMap.get(_constants.default.NATIVE_PLATFORM)
  186. );
  187. if (map[_constants.default.NATIVE_PLATFORM]) {
  188. return map[_constants.default.NATIVE_PLATFORM];
  189. }
  190. }
  191. this._assertNoDuplicates(
  192. name,
  193. _constants.default.GENERIC_PLATFORM,
  194. supportsNativePlatform,
  195. dupMap.get(_constants.default.GENERIC_PLATFORM)
  196. );
  197. if (map[_constants.default.GENERIC_PLATFORM]) {
  198. return map[_constants.default.GENERIC_PLATFORM];
  199. }
  200. return null;
  201. }
  202. _assertNoDuplicates(name, platform, supportsNativePlatform, relativePathSet) {
  203. if (relativePathSet == null) {
  204. return;
  205. } // Force flow refinement
  206. const previousSet = relativePathSet;
  207. const duplicates = new Map();
  208. var _iteratorNormalCompletion = true;
  209. var _didIteratorError = false;
  210. var _iteratorError = undefined;
  211. try {
  212. for (
  213. var _iterator = previousSet[Symbol.iterator](), _step;
  214. !(_iteratorNormalCompletion = (_step = _iterator.next()).done);
  215. _iteratorNormalCompletion = true
  216. ) {
  217. const _step$value = _slicedToArray(_step.value, 2),
  218. relativePath = _step$value[0],
  219. type = _step$value[1];
  220. const duplicatePath = fastPath.resolve(this._raw.rootDir, relativePath);
  221. duplicates.set(duplicatePath, type);
  222. }
  223. } catch (err) {
  224. _didIteratorError = true;
  225. _iteratorError = err;
  226. } finally {
  227. try {
  228. if (!_iteratorNormalCompletion && _iterator.return != null) {
  229. _iterator.return();
  230. }
  231. } finally {
  232. if (_didIteratorError) {
  233. throw _iteratorError;
  234. }
  235. }
  236. }
  237. throw new DuplicateHasteCandidatesError(
  238. name,
  239. platform,
  240. supportsNativePlatform,
  241. duplicates
  242. );
  243. }
  244. static create(rootDir) {
  245. return new ModuleMap({
  246. duplicates: new Map(),
  247. map: new Map(),
  248. mocks: new Map(),
  249. rootDir
  250. });
  251. }
  252. }
  253. exports.default = ModuleMap;
  254. _defineProperty(ModuleMap, 'DuplicateHasteCandidatesError', void 0);
  255. class DuplicateHasteCandidatesError extends Error {
  256. constructor(name, platform, supportsNativePlatform, duplicatesSet) {
  257. const platformMessage = getPlatformMessage(platform);
  258. super(
  259. `The name \`${name}\` was looked up in the Haste module map. It ` +
  260. `cannot be resolved, because there exists several different ` +
  261. `files, or packages, that provide a module for ` +
  262. `that particular name and platform. ${platformMessage} You must ` +
  263. `delete or blacklist files until there remains only one of these:\n\n` +
  264. Array.from(duplicatesSet)
  265. .map(
  266. ([dupFilePath, dupFileType]) =>
  267. ` * \`${dupFilePath}\` (${getTypeMessage(dupFileType)})\n`
  268. )
  269. .sort()
  270. .join('')
  271. );
  272. _defineProperty(this, 'hasteName', void 0);
  273. _defineProperty(this, 'platform', void 0);
  274. _defineProperty(this, 'supportsNativePlatform', void 0);
  275. _defineProperty(this, 'duplicatesSet', void 0);
  276. this.hasteName = name;
  277. this.platform = platform;
  278. this.supportsNativePlatform = supportsNativePlatform;
  279. this.duplicatesSet = duplicatesSet;
  280. }
  281. }
  282. function getPlatformMessage(platform) {
  283. if (platform === _constants.default.GENERIC_PLATFORM) {
  284. return 'The platform is generic (no extension).';
  285. }
  286. return `The platform extension is \`${platform}\`.`;
  287. }
  288. function getTypeMessage(type) {
  289. switch (type) {
  290. case _constants.default.MODULE:
  291. return 'module';
  292. case _constants.default.PACKAGE:
  293. return 'package';
  294. }
  295. return 'unknown';
  296. }
  297. ModuleMap.DuplicateHasteCandidatesError = DuplicateHasteCandidatesError;