UNPKG

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