index.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. 'use strict';
  2. function _child_process() {
  3. const data = require('child_process');
  4. _child_process = function () {
  5. return data;
  6. };
  7. return data;
  8. }
  9. function _crypto() {
  10. const data = require('crypto');
  11. _crypto = function () {
  12. return data;
  13. };
  14. return data;
  15. }
  16. function _events() {
  17. const data = require('events');
  18. _events = function () {
  19. return data;
  20. };
  21. return data;
  22. }
  23. function _os() {
  24. const data = require('os');
  25. _os = function () {
  26. return data;
  27. };
  28. return data;
  29. }
  30. function path() {
  31. const data = _interopRequireWildcard(require('path'));
  32. path = function () {
  33. return data;
  34. };
  35. return data;
  36. }
  37. function _sane() {
  38. const data = require('sane');
  39. _sane = function () {
  40. return data;
  41. };
  42. return data;
  43. }
  44. function _jestRegexUtil() {
  45. const data = require('jest-regex-util');
  46. _jestRegexUtil = function () {
  47. return data;
  48. };
  49. return data;
  50. }
  51. function _jestSerializer() {
  52. const data = _interopRequireDefault(require('jest-serializer'));
  53. _jestSerializer = function () {
  54. return data;
  55. };
  56. return data;
  57. }
  58. function _jestWorker() {
  59. const data = _interopRequireDefault(require('jest-worker'));
  60. _jestWorker = function () {
  61. return data;
  62. };
  63. return data;
  64. }
  65. var _HasteFS = _interopRequireDefault(require('./HasteFS'));
  66. var _ModuleMap = _interopRequireDefault(require('./ModuleMap'));
  67. var _constants = _interopRequireDefault(require('./constants'));
  68. var _node = _interopRequireDefault(require('./crawlers/node'));
  69. var _watchman = _interopRequireDefault(require('./crawlers/watchman'));
  70. var _getMockName = _interopRequireDefault(require('./getMockName'));
  71. var _FSEventsWatcher = _interopRequireDefault(require('./lib/FSEventsWatcher'));
  72. var _WatchmanWatcher = _interopRequireDefault(require('./lib/WatchmanWatcher'));
  73. var fastPath = _interopRequireWildcard(require('./lib/fast_path'));
  74. var _getPlatformExtension = _interopRequireDefault(
  75. require('./lib/getPlatformExtension')
  76. );
  77. var _normalizePathSep = _interopRequireDefault(
  78. require('./lib/normalizePathSep')
  79. );
  80. var _worker = require('./worker');
  81. function _interopRequireDefault(obj) {
  82. return obj && obj.__esModule ? obj : {default: obj};
  83. }
  84. function _getRequireWildcardCache() {
  85. if (typeof WeakMap !== 'function') return null;
  86. var cache = new WeakMap();
  87. _getRequireWildcardCache = function () {
  88. return cache;
  89. };
  90. return cache;
  91. }
  92. function _interopRequireWildcard(obj) {
  93. if (obj && obj.__esModule) {
  94. return obj;
  95. }
  96. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  97. return {default: obj};
  98. }
  99. var cache = _getRequireWildcardCache();
  100. if (cache && cache.has(obj)) {
  101. return cache.get(obj);
  102. }
  103. var newObj = {};
  104. var hasPropertyDescriptor =
  105. Object.defineProperty && Object.getOwnPropertyDescriptor;
  106. for (var key in obj) {
  107. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  108. var desc = hasPropertyDescriptor
  109. ? Object.getOwnPropertyDescriptor(obj, key)
  110. : null;
  111. if (desc && (desc.get || desc.set)) {
  112. Object.defineProperty(newObj, key, desc);
  113. } else {
  114. newObj[key] = obj[key];
  115. }
  116. }
  117. }
  118. newObj.default = obj;
  119. if (cache) {
  120. cache.set(obj, newObj);
  121. }
  122. return newObj;
  123. }
  124. function _defineProperty(obj, key, value) {
  125. if (key in obj) {
  126. Object.defineProperty(obj, key, {
  127. value: value,
  128. enumerable: true,
  129. configurable: true,
  130. writable: true
  131. });
  132. } else {
  133. obj[key] = value;
  134. }
  135. return obj;
  136. }
  137. // TypeScript doesn't like us importing from outside `rootDir`, but it doesn't
  138. // understand `require`.
  139. const {version: VERSION} = require('../package.json');
  140. const CHANGE_INTERVAL = 30;
  141. const MAX_WAIT_TIME = 240000;
  142. const NODE_MODULES = path().sep + 'node_modules' + path().sep;
  143. const PACKAGE_JSON = path().sep + 'package.json';
  144. const VCS_DIRECTORIES = ['.git', '.hg']
  145. .map(vcs =>
  146. (0, _jestRegexUtil().escapePathForRegex)(path().sep + vcs + path().sep)
  147. )
  148. .join('|');
  149. const canUseWatchman = (() => {
  150. try {
  151. (0, _child_process().execSync)('watchman --version', {
  152. stdio: ['ignore']
  153. });
  154. return true;
  155. } catch {}
  156. return false;
  157. })();
  158. function invariant(condition, message) {
  159. if (!condition) {
  160. throw new Error(message);
  161. }
  162. }
  163. /**
  164. * HasteMap is a JavaScript implementation of Facebook's haste module system.
  165. *
  166. * This implementation is inspired by https://github.com/facebook/node-haste
  167. * and was built with for high-performance in large code repositories with
  168. * hundreds of thousands of files. This implementation is scalable and provides
  169. * predictable performance.
  170. *
  171. * Because the haste map creation and synchronization is critical to startup
  172. * performance and most tasks are blocked by I/O this class makes heavy use of
  173. * synchronous operations. It uses worker processes for parallelizing file
  174. * access and metadata extraction.
  175. *
  176. * The data structures created by `jest-haste-map` can be used directly from the
  177. * cache without further processing. The metadata objects in the `files` and
  178. * `map` objects contain cross-references: a metadata object from one can look
  179. * up the corresponding metadata object in the other map. Note that in most
  180. * projects, the number of files will be greater than the number of haste
  181. * modules one module can refer to many files based on platform extensions.
  182. *
  183. * type HasteMap = {
  184. * clocks: WatchmanClocks,
  185. * files: {[filepath: string]: FileMetaData},
  186. * map: {[id: string]: ModuleMapItem},
  187. * mocks: {[id: string]: string},
  188. * }
  189. *
  190. * // Watchman clocks are used for query synchronization and file system deltas.
  191. * type WatchmanClocks = {[filepath: string]: string};
  192. *
  193. * type FileMetaData = {
  194. * id: ?string, // used to look up module metadata objects in `map`.
  195. * mtime: number, // check for outdated files.
  196. * size: number, // size of the file in bytes.
  197. * visited: boolean, // whether the file has been parsed or not.
  198. * dependencies: Array<string>, // all relative dependencies of this file.
  199. * sha1: ?string, // SHA-1 of the file, if requested via options.
  200. * };
  201. *
  202. * // Modules can be targeted to a specific platform based on the file name.
  203. * // Example: platform.ios.js and Platform.android.js will both map to the same
  204. * // `Platform` module. The platform should be specified during resolution.
  205. * type ModuleMapItem = {[platform: string]: ModuleMetaData};
  206. *
  207. * //
  208. * type ModuleMetaData = {
  209. * path: string, // the path to look up the file object in `files`.
  210. * type: string, // the module type (either `package` or `module`).
  211. * };
  212. *
  213. * Note that the data structures described above are conceptual only. The actual
  214. * implementation uses arrays and constant keys for metadata storage. Instead of
  215. * `{id: 'flatMap', mtime: 3421, size: 42, visited: true, dependencies: []}` the real
  216. * representation is similar to `['flatMap', 3421, 42, 1, []]` to save storage space
  217. * and reduce parse and write time of a big JSON blob.
  218. *
  219. * The HasteMap is created as follows:
  220. * 1. read data from the cache or create an empty structure.
  221. *
  222. * 2. crawl the file system.
  223. * * empty cache: crawl the entire file system.
  224. * * cache available:
  225. * * if watchman is available: get file system delta changes.
  226. * * if watchman is unavailable: crawl the entire file system.
  227. * * build metadata objects for every file. This builds the `files` part of
  228. * the `HasteMap`.
  229. *
  230. * 3. parse and extract metadata from changed files.
  231. * * this is done in parallel over worker processes to improve performance.
  232. * * the worst case is to parse all files.
  233. * * the best case is no file system access and retrieving all data from
  234. * the cache.
  235. * * the average case is a small number of changed files.
  236. *
  237. * 4. serialize the new `HasteMap` in a cache file.
  238. * Worker processes can directly access the cache through `HasteMap.read()`.
  239. *
  240. */
  241. class HasteMap extends _events().EventEmitter {
  242. constructor(options) {
  243. super();
  244. _defineProperty(this, '_buildPromise', void 0);
  245. _defineProperty(this, '_cachePath', void 0);
  246. _defineProperty(this, '_changeInterval', void 0);
  247. _defineProperty(this, '_console', void 0);
  248. _defineProperty(this, '_options', void 0);
  249. _defineProperty(this, '_watchers', void 0);
  250. _defineProperty(this, '_worker', void 0);
  251. this._options = {
  252. cacheDirectory: options.cacheDirectory || (0, _os().tmpdir)(),
  253. computeDependencies:
  254. options.computeDependencies === undefined
  255. ? true
  256. : options.computeDependencies,
  257. computeSha1: options.computeSha1 || false,
  258. dependencyExtractor: options.dependencyExtractor || null,
  259. extensions: options.extensions,
  260. forceNodeFilesystemAPI: !!options.forceNodeFilesystemAPI,
  261. hasteImplModulePath: options.hasteImplModulePath,
  262. maxWorkers: options.maxWorkers,
  263. mocksPattern: options.mocksPattern
  264. ? new RegExp(options.mocksPattern)
  265. : null,
  266. name: options.name,
  267. platforms: options.platforms,
  268. resetCache: options.resetCache,
  269. retainAllFiles: options.retainAllFiles,
  270. rootDir: options.rootDir,
  271. roots: Array.from(new Set(options.roots)),
  272. skipPackageJson: !!options.skipPackageJson,
  273. throwOnModuleCollision: !!options.throwOnModuleCollision,
  274. useWatchman: options.useWatchman == null ? true : options.useWatchman,
  275. watch: !!options.watch
  276. };
  277. this._console = options.console || global.console;
  278. if (options.ignorePattern) {
  279. if (options.ignorePattern instanceof RegExp) {
  280. this._options.ignorePattern = new RegExp(
  281. options.ignorePattern.source.concat('|' + VCS_DIRECTORIES),
  282. options.ignorePattern.flags
  283. );
  284. } else {
  285. const ignorePattern = options.ignorePattern;
  286. const vcsIgnoreRegExp = new RegExp(VCS_DIRECTORIES);
  287. this._options.ignorePattern = filePath =>
  288. vcsIgnoreRegExp.test(filePath) || ignorePattern(filePath);
  289. this._console.warn(
  290. 'jest-haste-map: the `ignorePattern` options as a function is being ' +
  291. 'deprecated. Provide a RegExp instead. See https://github.com/facebook/jest/pull/4063.'
  292. );
  293. }
  294. } else {
  295. this._options.ignorePattern = new RegExp(VCS_DIRECTORIES);
  296. }
  297. const rootDirHash = (0, _crypto().createHash)('md5')
  298. .update(options.rootDir)
  299. .digest('hex');
  300. let hasteImplHash = '';
  301. let dependencyExtractorHash = '';
  302. if (options.hasteImplModulePath) {
  303. const hasteImpl = require(options.hasteImplModulePath);
  304. if (hasteImpl.getCacheKey) {
  305. hasteImplHash = String(hasteImpl.getCacheKey());
  306. }
  307. }
  308. if (options.dependencyExtractor) {
  309. const dependencyExtractor = require(options.dependencyExtractor);
  310. if (dependencyExtractor.getCacheKey) {
  311. dependencyExtractorHash = String(dependencyExtractor.getCacheKey());
  312. }
  313. }
  314. this._cachePath = HasteMap.getCacheFilePath(
  315. this._options.cacheDirectory,
  316. `haste-map-${this._options.name}-${rootDirHash}`,
  317. VERSION,
  318. this._options.name,
  319. this._options.roots
  320. .map(root => fastPath.relative(options.rootDir, root))
  321. .join(':'),
  322. this._options.extensions.join(':'),
  323. this._options.platforms.join(':'),
  324. this._options.computeSha1.toString(),
  325. options.mocksPattern || '',
  326. (options.ignorePattern || '').toString(),
  327. hasteImplHash,
  328. dependencyExtractorHash
  329. );
  330. this._buildPromise = null;
  331. this._watchers = [];
  332. this._worker = null;
  333. }
  334. static getCacheFilePath(tmpdir, name, ...extra) {
  335. const hash = (0, _crypto().createHash)('md5').update(extra.join(''));
  336. return path().join(
  337. tmpdir,
  338. name.replace(/\W/g, '-') + '-' + hash.digest('hex')
  339. );
  340. }
  341. getCacheFilePath() {
  342. return this._cachePath;
  343. }
  344. build() {
  345. if (!this._buildPromise) {
  346. this._buildPromise = (async () => {
  347. const data = await this._buildFileMap(); // Persist when we don't know if files changed (changedFiles undefined)
  348. // or when we know a file was changed or deleted.
  349. let hasteMap;
  350. if (
  351. data.changedFiles === undefined ||
  352. data.changedFiles.size > 0 ||
  353. data.removedFiles.size > 0
  354. ) {
  355. hasteMap = await this._buildHasteMap(data);
  356. this._persist(hasteMap);
  357. } else {
  358. hasteMap = data.hasteMap;
  359. }
  360. const rootDir = this._options.rootDir;
  361. const hasteFS = new _HasteFS.default({
  362. files: hasteMap.files,
  363. rootDir
  364. });
  365. const moduleMap = new _ModuleMap.default({
  366. duplicates: hasteMap.duplicates,
  367. map: hasteMap.map,
  368. mocks: hasteMap.mocks,
  369. rootDir
  370. });
  371. const __hasteMapForTest =
  372. (process.env.NODE_ENV === 'test' && hasteMap) || null;
  373. await this._watch(hasteMap);
  374. return {
  375. __hasteMapForTest,
  376. hasteFS,
  377. moduleMap
  378. };
  379. })();
  380. }
  381. return this._buildPromise;
  382. }
  383. /**
  384. * 1. read data from the cache or create an empty structure.
  385. */
  386. read() {
  387. let hasteMap;
  388. try {
  389. hasteMap = _jestSerializer().default.readFileSync(this._cachePath);
  390. } catch {
  391. hasteMap = this._createEmptyMap();
  392. }
  393. return hasteMap;
  394. }
  395. readModuleMap() {
  396. const data = this.read();
  397. return new _ModuleMap.default({
  398. duplicates: data.duplicates,
  399. map: data.map,
  400. mocks: data.mocks,
  401. rootDir: this._options.rootDir
  402. });
  403. }
  404. /**
  405. * 2. crawl the file system.
  406. */
  407. async _buildFileMap() {
  408. let hasteMap;
  409. try {
  410. const read = this._options.resetCache ? this._createEmptyMap : this.read;
  411. hasteMap = await read.call(this);
  412. } catch {
  413. hasteMap = this._createEmptyMap();
  414. }
  415. return this._crawl(hasteMap);
  416. }
  417. /**
  418. * 3. parse and extract metadata from changed files.
  419. */
  420. _processFile(hasteMap, map, mocks, filePath, workerOptions) {
  421. const rootDir = this._options.rootDir;
  422. const setModule = (id, module) => {
  423. let moduleMap = map.get(id);
  424. if (!moduleMap) {
  425. moduleMap = Object.create(null);
  426. map.set(id, moduleMap);
  427. }
  428. const platform =
  429. (0, _getPlatformExtension.default)(
  430. module[_constants.default.PATH],
  431. this._options.platforms
  432. ) || _constants.default.GENERIC_PLATFORM;
  433. const existingModule = moduleMap[platform];
  434. if (
  435. existingModule &&
  436. existingModule[_constants.default.PATH] !==
  437. module[_constants.default.PATH]
  438. ) {
  439. const method = this._options.throwOnModuleCollision ? 'error' : 'warn';
  440. this._console[method](
  441. [
  442. 'jest-haste-map: Haste module naming collision: ' + id,
  443. ' The following files share their name; please adjust your hasteImpl:',
  444. ' * <rootDir>' +
  445. path().sep +
  446. existingModule[_constants.default.PATH],
  447. ' * <rootDir>' + path().sep + module[_constants.default.PATH],
  448. ''
  449. ].join('\n')
  450. );
  451. if (this._options.throwOnModuleCollision) {
  452. throw new DuplicateError(
  453. existingModule[_constants.default.PATH],
  454. module[_constants.default.PATH]
  455. );
  456. } // We do NOT want consumers to use a module that is ambiguous.
  457. delete moduleMap[platform];
  458. if (Object.keys(moduleMap).length === 1) {
  459. map.delete(id);
  460. }
  461. let dupsByPlatform = hasteMap.duplicates.get(id);
  462. if (dupsByPlatform == null) {
  463. dupsByPlatform = new Map();
  464. hasteMap.duplicates.set(id, dupsByPlatform);
  465. }
  466. const dups = new Map([
  467. [module[_constants.default.PATH], module[_constants.default.TYPE]],
  468. [
  469. existingModule[_constants.default.PATH],
  470. existingModule[_constants.default.TYPE]
  471. ]
  472. ]);
  473. dupsByPlatform.set(platform, dups);
  474. return;
  475. }
  476. const dupsByPlatform = hasteMap.duplicates.get(id);
  477. if (dupsByPlatform != null) {
  478. const dups = dupsByPlatform.get(platform);
  479. if (dups != null) {
  480. dups.set(
  481. module[_constants.default.PATH],
  482. module[_constants.default.TYPE]
  483. );
  484. }
  485. return;
  486. }
  487. moduleMap[platform] = module;
  488. };
  489. const relativeFilePath = fastPath.relative(rootDir, filePath);
  490. const fileMetadata = hasteMap.files.get(relativeFilePath);
  491. if (!fileMetadata) {
  492. throw new Error(
  493. 'jest-haste-map: File to process was not found in the haste map.'
  494. );
  495. }
  496. const moduleMetadata = hasteMap.map.get(
  497. fileMetadata[_constants.default.ID]
  498. );
  499. const computeSha1 =
  500. this._options.computeSha1 && !fileMetadata[_constants.default.SHA1]; // Callback called when the response from the worker is successful.
  501. const workerReply = metadata => {
  502. // `1` for truthy values instead of `true` to save cache space.
  503. fileMetadata[_constants.default.VISITED] = 1;
  504. const metadataId = metadata.id;
  505. const metadataModule = metadata.module;
  506. if (metadataId && metadataModule) {
  507. fileMetadata[_constants.default.ID] = metadataId;
  508. setModule(metadataId, metadataModule);
  509. }
  510. fileMetadata[_constants.default.DEPENDENCIES] = metadata.dependencies
  511. ? metadata.dependencies.join(_constants.default.DEPENDENCY_DELIM)
  512. : '';
  513. if (computeSha1) {
  514. fileMetadata[_constants.default.SHA1] = metadata.sha1;
  515. }
  516. }; // Callback called when the response from the worker is an error.
  517. const workerError = error => {
  518. if (typeof error !== 'object' || !error.message || !error.stack) {
  519. error = new Error(error);
  520. error.stack = ''; // Remove stack for stack-less errors.
  521. }
  522. if (!['ENOENT', 'EACCES'].includes(error.code)) {
  523. throw error;
  524. } // If a file cannot be read we remove it from the file list and
  525. // ignore the failure silently.
  526. hasteMap.files.delete(relativeFilePath);
  527. }; // If we retain all files in the virtual HasteFS representation, we avoid
  528. // reading them if they aren't important (node_modules).
  529. if (this._options.retainAllFiles && filePath.includes(NODE_MODULES)) {
  530. if (computeSha1) {
  531. return this._getWorker(workerOptions)
  532. .getSha1({
  533. computeDependencies: this._options.computeDependencies,
  534. computeSha1,
  535. dependencyExtractor: this._options.dependencyExtractor,
  536. filePath,
  537. hasteImplModulePath: this._options.hasteImplModulePath,
  538. rootDir
  539. })
  540. .then(workerReply, workerError);
  541. }
  542. return null;
  543. }
  544. if (
  545. this._options.mocksPattern &&
  546. this._options.mocksPattern.test(filePath)
  547. ) {
  548. const mockPath = (0, _getMockName.default)(filePath);
  549. const existingMockPath = mocks.get(mockPath);
  550. if (existingMockPath) {
  551. const secondMockPath = fastPath.relative(rootDir, filePath);
  552. if (existingMockPath !== secondMockPath) {
  553. const method = this._options.throwOnModuleCollision
  554. ? 'error'
  555. : 'warn';
  556. this._console[method](
  557. [
  558. 'jest-haste-map: duplicate manual mock found: ' + mockPath,
  559. ' The following files share their name; please delete one of them:',
  560. ' * <rootDir>' + path().sep + existingMockPath,
  561. ' * <rootDir>' + path().sep + secondMockPath,
  562. ''
  563. ].join('\n')
  564. );
  565. if (this._options.throwOnModuleCollision) {
  566. throw new DuplicateError(existingMockPath, secondMockPath);
  567. }
  568. }
  569. }
  570. mocks.set(mockPath, relativeFilePath);
  571. }
  572. if (fileMetadata[_constants.default.VISITED]) {
  573. if (!fileMetadata[_constants.default.ID]) {
  574. return null;
  575. }
  576. if (moduleMetadata != null) {
  577. const platform =
  578. (0, _getPlatformExtension.default)(
  579. filePath,
  580. this._options.platforms
  581. ) || _constants.default.GENERIC_PLATFORM;
  582. const module = moduleMetadata[platform];
  583. if (module == null) {
  584. return null;
  585. }
  586. const moduleId = fileMetadata[_constants.default.ID];
  587. let modulesByPlatform = map.get(moduleId);
  588. if (!modulesByPlatform) {
  589. modulesByPlatform = Object.create(null);
  590. map.set(moduleId, modulesByPlatform);
  591. }
  592. modulesByPlatform[platform] = module;
  593. return null;
  594. }
  595. }
  596. return this._getWorker(workerOptions)
  597. .worker({
  598. computeDependencies: this._options.computeDependencies,
  599. computeSha1,
  600. dependencyExtractor: this._options.dependencyExtractor,
  601. filePath,
  602. hasteImplModulePath: this._options.hasteImplModulePath,
  603. rootDir
  604. })
  605. .then(workerReply, workerError);
  606. }
  607. _buildHasteMap(data) {
  608. const {removedFiles, changedFiles, hasteMap} = data; // If any files were removed or we did not track what files changed, process
  609. // every file looking for changes. Otherwise, process only changed files.
  610. let map;
  611. let mocks;
  612. let filesToProcess;
  613. if (changedFiles === undefined || removedFiles.size) {
  614. map = new Map();
  615. mocks = new Map();
  616. filesToProcess = hasteMap.files;
  617. } else {
  618. map = hasteMap.map;
  619. mocks = hasteMap.mocks;
  620. filesToProcess = changedFiles;
  621. }
  622. for (const [relativeFilePath, fileMetadata] of removedFiles) {
  623. this._recoverDuplicates(
  624. hasteMap,
  625. relativeFilePath,
  626. fileMetadata[_constants.default.ID]
  627. );
  628. }
  629. const promises = [];
  630. for (const relativeFilePath of filesToProcess.keys()) {
  631. if (
  632. this._options.skipPackageJson &&
  633. relativeFilePath.endsWith(PACKAGE_JSON)
  634. ) {
  635. continue;
  636. } // SHA-1, if requested, should already be present thanks to the crawler.
  637. const filePath = fastPath.resolve(
  638. this._options.rootDir,
  639. relativeFilePath
  640. );
  641. const promise = this._processFile(hasteMap, map, mocks, filePath);
  642. if (promise) {
  643. promises.push(promise);
  644. }
  645. }
  646. return Promise.all(promises).then(
  647. () => {
  648. this._cleanup();
  649. hasteMap.map = map;
  650. hasteMap.mocks = mocks;
  651. return hasteMap;
  652. },
  653. error => {
  654. this._cleanup();
  655. throw error;
  656. }
  657. );
  658. }
  659. _cleanup() {
  660. const worker = this._worker; // @ts-expect-error
  661. if (worker && typeof worker.end === 'function') {
  662. // @ts-expect-error
  663. worker.end();
  664. }
  665. this._worker = null;
  666. }
  667. /**
  668. * 4. serialize the new `HasteMap` in a cache file.
  669. */
  670. _persist(hasteMap) {
  671. _jestSerializer().default.writeFileSync(this._cachePath, hasteMap);
  672. }
  673. /**
  674. * Creates workers or parses files and extracts metadata in-process.
  675. */
  676. _getWorker(options) {
  677. if (!this._worker) {
  678. if ((options && options.forceInBand) || this._options.maxWorkers <= 1) {
  679. this._worker = {
  680. getSha1: _worker.getSha1,
  681. worker: _worker.worker
  682. };
  683. } else {
  684. // @ts-expect-error: assignment of a worker with custom properties.
  685. this._worker = new (_jestWorker().default)(
  686. require.resolve('./worker'),
  687. {
  688. exposedMethods: ['getSha1', 'worker'],
  689. maxRetries: 3,
  690. numWorkers: this._options.maxWorkers
  691. }
  692. );
  693. }
  694. }
  695. return this._worker;
  696. }
  697. _crawl(hasteMap) {
  698. const options = this._options;
  699. const ignore = this._ignore.bind(this);
  700. const crawl =
  701. canUseWatchman && this._options.useWatchman
  702. ? _watchman.default
  703. : _node.default;
  704. const crawlerOptions = {
  705. computeSha1: options.computeSha1,
  706. data: hasteMap,
  707. extensions: options.extensions,
  708. forceNodeFilesystemAPI: options.forceNodeFilesystemAPI,
  709. ignore,
  710. rootDir: options.rootDir,
  711. roots: options.roots
  712. };
  713. const retry = error => {
  714. if (crawl === _watchman.default) {
  715. this._console.warn(
  716. `jest-haste-map: Watchman crawl failed. Retrying once with node ` +
  717. `crawler.\n` +
  718. ` Usually this happens when watchman isn't running. Create an ` +
  719. `empty \`.watchmanconfig\` file in your project's root folder or ` +
  720. `initialize a git or hg repository in your project.\n` +
  721. ` ` +
  722. error
  723. );
  724. return (0, _node.default)(crawlerOptions).catch(e => {
  725. throw new Error(
  726. `Crawler retry failed:\n` +
  727. ` Original error: ${error.message}\n` +
  728. ` Retry error: ${e.message}\n`
  729. );
  730. });
  731. }
  732. throw error;
  733. };
  734. try {
  735. return crawl(crawlerOptions).catch(retry);
  736. } catch (error) {
  737. return retry(error);
  738. }
  739. }
  740. /**
  741. * Watch mode
  742. */
  743. _watch(hasteMap) {
  744. if (!this._options.watch) {
  745. return Promise.resolve();
  746. } // In watch mode, we'll only warn about module collisions and we'll retain
  747. // all files, even changes to node_modules.
  748. this._options.throwOnModuleCollision = false;
  749. this._options.retainAllFiles = true; // WatchmanWatcher > FSEventsWatcher > sane.NodeWatcher
  750. const Watcher =
  751. canUseWatchman && this._options.useWatchman
  752. ? _WatchmanWatcher.default
  753. : _FSEventsWatcher.default.isSupported()
  754. ? _FSEventsWatcher.default
  755. : _sane().NodeWatcher;
  756. const extensions = this._options.extensions;
  757. const ignorePattern = this._options.ignorePattern;
  758. const rootDir = this._options.rootDir;
  759. let changeQueue = Promise.resolve();
  760. let eventsQueue = []; // We only need to copy the entire haste map once on every "frame".
  761. let mustCopy = true;
  762. const createWatcher = root => {
  763. // @ts-expect-error: TODO how? "Cannot use 'new' with an expression whose type lacks a call or construct signature."
  764. const watcher = new Watcher(root, {
  765. dot: true,
  766. glob: extensions.map(extension => '**/*.' + extension),
  767. ignored: ignorePattern
  768. });
  769. return new Promise((resolve, reject) => {
  770. const rejectTimeout = setTimeout(
  771. () => reject(new Error('Failed to start watch mode.')),
  772. MAX_WAIT_TIME
  773. );
  774. watcher.once('ready', () => {
  775. clearTimeout(rejectTimeout);
  776. watcher.on('all', onChange);
  777. resolve(watcher);
  778. });
  779. });
  780. };
  781. const emitChange = () => {
  782. if (eventsQueue.length) {
  783. mustCopy = true;
  784. const changeEvent = {
  785. eventsQueue,
  786. hasteFS: new _HasteFS.default({
  787. files: hasteMap.files,
  788. rootDir
  789. }),
  790. moduleMap: new _ModuleMap.default({
  791. duplicates: hasteMap.duplicates,
  792. map: hasteMap.map,
  793. mocks: hasteMap.mocks,
  794. rootDir
  795. })
  796. };
  797. this.emit('change', changeEvent);
  798. eventsQueue = [];
  799. }
  800. };
  801. const onChange = (type, filePath, root, stat) => {
  802. filePath = path().join(root, (0, _normalizePathSep.default)(filePath));
  803. if (
  804. (stat && stat.isDirectory()) ||
  805. this._ignore(filePath) ||
  806. !extensions.some(extension => filePath.endsWith(extension))
  807. ) {
  808. return;
  809. }
  810. const relativeFilePath = fastPath.relative(rootDir, filePath);
  811. const fileMetadata = hasteMap.files.get(relativeFilePath); // The file has been accessed, not modified
  812. if (
  813. type === 'change' &&
  814. fileMetadata &&
  815. stat &&
  816. fileMetadata[_constants.default.MTIME] === stat.mtime.getTime()
  817. ) {
  818. return;
  819. }
  820. changeQueue = changeQueue
  821. .then(() => {
  822. // If we get duplicate events for the same file, ignore them.
  823. if (
  824. eventsQueue.find(
  825. event =>
  826. event.type === type &&
  827. event.filePath === filePath &&
  828. ((!event.stat && !stat) ||
  829. (!!event.stat &&
  830. !!stat &&
  831. event.stat.mtime.getTime() === stat.mtime.getTime()))
  832. )
  833. ) {
  834. return null;
  835. }
  836. if (mustCopy) {
  837. mustCopy = false;
  838. hasteMap = {
  839. clocks: new Map(hasteMap.clocks),
  840. duplicates: new Map(hasteMap.duplicates),
  841. files: new Map(hasteMap.files),
  842. map: new Map(hasteMap.map),
  843. mocks: new Map(hasteMap.mocks)
  844. };
  845. }
  846. const add = () => {
  847. eventsQueue.push({
  848. filePath,
  849. stat,
  850. type
  851. });
  852. return null;
  853. };
  854. const fileMetadata = hasteMap.files.get(relativeFilePath); // If it's not an addition, delete the file and all its metadata
  855. if (fileMetadata != null) {
  856. const moduleName = fileMetadata[_constants.default.ID];
  857. const platform =
  858. (0, _getPlatformExtension.default)(
  859. filePath,
  860. this._options.platforms
  861. ) || _constants.default.GENERIC_PLATFORM;
  862. hasteMap.files.delete(relativeFilePath);
  863. let moduleMap = hasteMap.map.get(moduleName);
  864. if (moduleMap != null) {
  865. // We are forced to copy the object because jest-haste-map exposes
  866. // the map as an immutable entity.
  867. moduleMap = copy(moduleMap);
  868. delete moduleMap[platform];
  869. if (Object.keys(moduleMap).length === 0) {
  870. hasteMap.map.delete(moduleName);
  871. } else {
  872. hasteMap.map.set(moduleName, moduleMap);
  873. }
  874. }
  875. if (
  876. this._options.mocksPattern &&
  877. this._options.mocksPattern.test(filePath)
  878. ) {
  879. const mockName = (0, _getMockName.default)(filePath);
  880. hasteMap.mocks.delete(mockName);
  881. }
  882. this._recoverDuplicates(hasteMap, relativeFilePath, moduleName);
  883. } // If the file was added or changed,
  884. // parse it and update the haste map.
  885. if (type === 'add' || type === 'change') {
  886. invariant(
  887. stat,
  888. 'since the file exists or changed, it should have stats'
  889. );
  890. const fileMetadata = [
  891. '',
  892. stat.mtime.getTime(),
  893. stat.size,
  894. 0,
  895. '',
  896. null
  897. ];
  898. hasteMap.files.set(relativeFilePath, fileMetadata);
  899. const promise = this._processFile(
  900. hasteMap,
  901. hasteMap.map,
  902. hasteMap.mocks,
  903. filePath,
  904. {
  905. forceInBand: true
  906. }
  907. ); // Cleanup
  908. this._cleanup();
  909. if (promise) {
  910. return promise.then(add);
  911. } else {
  912. // If a file in node_modules has changed,
  913. // emit an event regardless.
  914. add();
  915. }
  916. } else {
  917. add();
  918. }
  919. return null;
  920. })
  921. .catch(error => {
  922. this._console.error(
  923. `jest-haste-map: watch error:\n ${error.stack}\n`
  924. );
  925. });
  926. };
  927. this._changeInterval = setInterval(emitChange, CHANGE_INTERVAL);
  928. return Promise.all(this._options.roots.map(createWatcher)).then(
  929. watchers => {
  930. this._watchers = watchers;
  931. }
  932. );
  933. }
  934. /**
  935. * This function should be called when the file under `filePath` is removed
  936. * or changed. When that happens, we want to figure out if that file was
  937. * part of a group of files that had the same ID. If it was, we want to
  938. * remove it from the group. Furthermore, if there is only one file
  939. * remaining in the group, then we want to restore that single file as the
  940. * correct resolution for its ID, and cleanup the duplicates index.
  941. */
  942. _recoverDuplicates(hasteMap, relativeFilePath, moduleName) {
  943. let dupsByPlatform = hasteMap.duplicates.get(moduleName);
  944. if (dupsByPlatform == null) {
  945. return;
  946. }
  947. const platform =
  948. (0, _getPlatformExtension.default)(
  949. relativeFilePath,
  950. this._options.platforms
  951. ) || _constants.default.GENERIC_PLATFORM;
  952. let dups = dupsByPlatform.get(platform);
  953. if (dups == null) {
  954. return;
  955. }
  956. dupsByPlatform = copyMap(dupsByPlatform);
  957. hasteMap.duplicates.set(moduleName, dupsByPlatform);
  958. dups = copyMap(dups);
  959. dupsByPlatform.set(platform, dups);
  960. dups.delete(relativeFilePath);
  961. if (dups.size !== 1) {
  962. return;
  963. }
  964. const uniqueModule = dups.entries().next().value;
  965. if (!uniqueModule) {
  966. return;
  967. }
  968. let dedupMap = hasteMap.map.get(moduleName);
  969. if (dedupMap == null) {
  970. dedupMap = Object.create(null);
  971. hasteMap.map.set(moduleName, dedupMap);
  972. }
  973. dedupMap[platform] = uniqueModule;
  974. dupsByPlatform.delete(platform);
  975. if (dupsByPlatform.size === 0) {
  976. hasteMap.duplicates.delete(moduleName);
  977. }
  978. }
  979. end() {
  980. // @ts-expect-error: TODO TS cannot decide if `setInterval` and `clearInterval` comes from NodeJS or the DOM
  981. clearInterval(this._changeInterval);
  982. if (!this._watchers.length) {
  983. return Promise.resolve();
  984. }
  985. return Promise.all(
  986. this._watchers.map(
  987. watcher => new Promise(resolve => watcher.close(resolve))
  988. )
  989. ).then(() => {
  990. this._watchers = [];
  991. });
  992. }
  993. /**
  994. * Helpers
  995. */
  996. _ignore(filePath) {
  997. const ignorePattern = this._options.ignorePattern;
  998. const ignoreMatched =
  999. ignorePattern instanceof RegExp
  1000. ? ignorePattern.test(filePath)
  1001. : ignorePattern && ignorePattern(filePath);
  1002. return (
  1003. ignoreMatched ||
  1004. (!this._options.retainAllFiles && filePath.includes(NODE_MODULES))
  1005. );
  1006. }
  1007. _createEmptyMap() {
  1008. return {
  1009. clocks: new Map(),
  1010. duplicates: new Map(),
  1011. files: new Map(),
  1012. map: new Map(),
  1013. mocks: new Map()
  1014. };
  1015. }
  1016. }
  1017. _defineProperty(HasteMap, 'H', void 0);
  1018. _defineProperty(HasteMap, 'DuplicateError', void 0);
  1019. _defineProperty(HasteMap, 'ModuleMap', void 0);
  1020. class DuplicateError extends Error {
  1021. constructor(mockPath1, mockPath2) {
  1022. super('Duplicated files or mocks. Please check the console for more info');
  1023. _defineProperty(this, 'mockPath1', void 0);
  1024. _defineProperty(this, 'mockPath2', void 0);
  1025. this.mockPath1 = mockPath1;
  1026. this.mockPath2 = mockPath2;
  1027. }
  1028. }
  1029. function copy(object) {
  1030. return Object.assign(Object.create(null), object);
  1031. }
  1032. function copyMap(input) {
  1033. return new Map(input);
  1034. }
  1035. HasteMap.H = _constants.default;
  1036. HasteMap.DuplicateError = DuplicateError;
  1037. HasteMap.ModuleMap = _ModuleMap.default;
  1038. module.exports = HasteMap;