index.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. 'use strict';
  2. var _child_process;
  3. function _load_child_process() {
  4. return _child_process = require('child_process');
  5. }
  6. var _package;
  7. function _load_package() {
  8. return _package = require('../package.json');
  9. }
  10. var _worker;
  11. function _load_worker() {
  12. return _worker = require('./worker');
  13. }
  14. var _crypto;
  15. function _load_crypto() {
  16. return _crypto = _interopRequireDefault(require('crypto'));
  17. }
  18. var _events;
  19. function _load_events() {
  20. return _events = _interopRequireDefault(require('events'));
  21. }
  22. var _get_mock_name;
  23. function _load_get_mock_name() {
  24. return _get_mock_name = _interopRequireDefault(require('./get_mock_name'));
  25. }
  26. var _get_platform_extension;
  27. function _load_get_platform_extension() {
  28. return _get_platform_extension = _interopRequireDefault(require('./lib/get_platform_extension'));
  29. }
  30. var _constants;
  31. function _load_constants() {
  32. return _constants = _interopRequireDefault(require('./constants'));
  33. }
  34. var _haste_fs;
  35. function _load_haste_fs() {
  36. return _haste_fs = _interopRequireDefault(require('./haste_fs'));
  37. }
  38. var _module_map;
  39. function _load_module_map() {
  40. return _module_map = _interopRequireDefault(require('./module_map'));
  41. }
  42. var _node;
  43. function _load_node() {
  44. return _node = _interopRequireDefault(require('./crawlers/node'));
  45. }
  46. var _normalize_path_sep;
  47. function _load_normalize_path_sep() {
  48. return _normalize_path_sep = _interopRequireDefault(require('./lib/normalize_path_sep'));
  49. }
  50. var _os;
  51. function _load_os() {
  52. return _os = _interopRequireDefault(require('os'));
  53. }
  54. var _path;
  55. function _load_path() {
  56. return _path = _interopRequireDefault(require('path'));
  57. }
  58. var _sane;
  59. function _load_sane() {
  60. return _sane = _interopRequireDefault(require('sane'));
  61. }
  62. var _jestSerializer;
  63. function _load_jestSerializer() {
  64. return _jestSerializer = _interopRequireDefault(require('jest-serializer'));
  65. }
  66. var _watchman;
  67. function _load_watchman() {
  68. return _watchman = _interopRequireDefault(require('./crawlers/watchman'));
  69. }
  70. var _watchman_watcher;
  71. function _load_watchman_watcher() {
  72. return _watchman_watcher = _interopRequireDefault(require('./lib/watchman_watcher'));
  73. }
  74. var _jestWorker;
  75. function _load_jestWorker() {
  76. return _jestWorker = _interopRequireDefault(require('jest-worker'));
  77. }
  78. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  79. // eslint-disable-next-line import/no-duplicates
  80. // eslint-disable-next-line import/no-duplicates
  81. const CHANGE_INTERVAL = 30;
  82. // eslint-disable-next-line import/default
  83. // eslint-disable-next-line import/default
  84. /**
  85. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  86. *
  87. * This source code is licensed under the MIT license found in the
  88. * LICENSE file in the root directory of this source tree.
  89. *
  90. *
  91. */
  92. const MAX_WAIT_TIME = 240000;
  93. const NODE_MODULES = (_path || _load_path()).default.sep + 'node_modules' + (_path || _load_path()).default.sep;
  94. const canUseWatchman = (() => {
  95. try {
  96. (0, (_child_process || _load_child_process()).execSync)('watchman --version', { stdio: ['ignore'] });
  97. return true;
  98. } catch (e) {}
  99. return false;
  100. })();
  101. const escapePathSeparator = string => (_path || _load_path()).default.sep === '\\' ? string.replace(/(\/|\\)/g, '\\\\') : string;
  102. const getWhiteList = list => {
  103. if (list && list.length) {
  104. return new RegExp('(' + escapePathSeparator(NODE_MODULES) + '(?:' + list.join('|') + ')(?=$|' + escapePathSeparator((_path || _load_path()).default.sep) + '))', 'g');
  105. }
  106. return null;
  107. };
  108. /**
  109. * HasteMap is a JavaScript implementation of Facebook's haste module system.
  110. *
  111. * This implementation is inspired by https://github.com/facebook/node-haste
  112. * and was built with for high-performance in large code repositories with
  113. * hundreds of thousands of files. This implementation is scalable and provides
  114. * predictable performance.
  115. *
  116. * Because the haste map creation and synchronization is critical to startup
  117. * performance and most tasks are blocked by I/O this class makes heavy use of
  118. * synchronous operations. It uses worker processes for parallelizing file
  119. * access and metadata extraction.
  120. *
  121. * The data structures created by `jest-haste-map` can be used directly from the
  122. * cache without further processing. The metadata objects in the `files` and
  123. * `map` objects contain cross-references: a metadata object from one can look
  124. * up the corresponding metadata object in the other map. Note that in most
  125. * projects, the number of files will be greater than the number of haste
  126. * modules one module can refer to many files based on platform extensions.
  127. *
  128. * type HasteMap = {
  129. * clocks: WatchmanClocks,
  130. * files: {[filepath: string]: FileMetaData},
  131. * map: {[id: string]: ModuleMapItem},
  132. * mocks: {[id: string]: string},
  133. * }
  134. *
  135. * // Watchman clocks are used for query synchronization and file system deltas.
  136. * type WatchmanClocks = {[filepath: string]: string};
  137. *
  138. * type FileMetaData = {
  139. * id: ?string, // used to look up module metadata objects in `map`.
  140. * mtime: number, // check for outdated files.
  141. * visited: boolean, // whether the file has been parsed or not.
  142. * dependencies: Array<string>, // all relative dependencies of this file.
  143. * };
  144. *
  145. * // Modules can be targeted to a specific platform based on the file name.
  146. * // Example: platform.ios.js and Platform.android.js will both map to the same
  147. * // `Platform` module. The platform should be specified during resolution.
  148. * type ModuleMapItem = {[platform: string]: ModuleMetaData};
  149. *
  150. * //
  151. * type ModuleMetaData = {
  152. * path: string, // the path to look up the file object in `files`.
  153. * type: string, // the module type (either `package` or `module`).
  154. * };
  155. *
  156. * Note that the data structures described above are conceptual only. The actual
  157. * implementation uses arrays and constant keys for metadata storage. Instead of
  158. * `{id: 'flatMap', mtime: 3421, visited: true, dependencies: []}` the real
  159. * representation is similar to `['flatMap', 3421, 1, []]` to save storage space
  160. * and reduce parse and write time of a big JSON blob.
  161. *
  162. * The HasteMap is created as follows:
  163. * 1. read data from the cache or create an empty structure.
  164. * 2. crawl the file system.
  165. * * empty cache: crawl the entire file system.
  166. * * cache available:
  167. * * if watchman is available: get file system delta changes.
  168. * * if watchman is unavailable: crawl the entire file system.
  169. * * build metadata objects for every file. This builds the `files` part of
  170. * the `HasteMap`.
  171. * 3. parse and extract metadata from changed files.
  172. * * this is done in parallel over worker processes to improve performance.
  173. * * the worst case is to parse all files.
  174. * * the best case is no file system access and retrieving all data from
  175. * the cache.
  176. * * the average case is a small number of changed files.
  177. * 4. serialize the new `HasteMap` in a cache file.
  178. * Worker processes can directly access the cache through `HasteMap.read()`.
  179. *
  180. */
  181. class HasteMap extends (_events || _load_events()).default {
  182. constructor(options) {
  183. super();
  184. this._options = {
  185. cacheDirectory: options.cacheDirectory || (_os || _load_os()).default.tmpdir(),
  186. extensions: options.extensions,
  187. forceNodeFilesystemAPI: !!options.forceNodeFilesystemAPI,
  188. hasteImplModulePath: options.hasteImplModulePath,
  189. ignorePattern: options.ignorePattern,
  190. maxWorkers: options.maxWorkers,
  191. mocksPattern: options.mocksPattern ? new RegExp(options.mocksPattern) : null,
  192. name: options.name,
  193. platforms: options.platforms,
  194. resetCache: options.resetCache,
  195. retainAllFiles: options.retainAllFiles,
  196. roots: Array.from(new Set(options.roots)),
  197. throwOnModuleCollision: !!options.throwOnModuleCollision,
  198. useWatchman: options.useWatchman == null ? true : options.useWatchman,
  199. watch: !!options.watch
  200. };
  201. this._console = options.console || global.console;
  202. if (!(options.ignorePattern instanceof RegExp)) {
  203. this._console.warn('jest-haste-map: the `ignorePattern` options as a function is being ' + 'deprecated. Provide a RegExp instead. See https://github.com/facebook/jest/pull/4063.');
  204. }
  205. this._cachePath = HasteMap.getCacheFilePath(this._options.cacheDirectory, `haste-map-${this._options.name}`, (_package || _load_package()).version, this._options.roots.join(':'), this._options.extensions.join(':'), this._options.platforms.join(':'), options.mocksPattern || '', options.ignorePattern.toString());
  206. this._whitelist = getWhiteList(options.providesModuleNodeModules);
  207. this._buildPromise = null;
  208. this._watchers = [];
  209. this._worker = null;
  210. }
  211. static getCacheFilePath(tmpdir, name) {
  212. for (var _len = arguments.length, extra = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
  213. extra[_key - 2] = arguments[_key];
  214. }
  215. const hash = (_crypto || _load_crypto()).default.createHash('md5').update(name + extra.join(''));
  216. return (_path || _load_path()).default.join(tmpdir, name.replace(/\W/g, '-') + '-' + hash.digest('hex'));
  217. }
  218. build() {
  219. if (!this._buildPromise) {
  220. this._buildPromise = this._buildFileMap().then(data => this._buildHasteMap(data)).then(hasteMap => {
  221. this._persist(hasteMap);
  222. const hasteFS = new (_haste_fs || _load_haste_fs()).default(hasteMap.files);
  223. const moduleMap = new (_module_map || _load_module_map()).default({
  224. duplicates: hasteMap.duplicates,
  225. map: hasteMap.map,
  226. mocks: hasteMap.mocks
  227. });
  228. const __hasteMapForTest = process.env.NODE_ENV === 'test' && hasteMap || null;
  229. return this._watch(hasteMap, hasteFS, moduleMap).then(() => ({
  230. __hasteMapForTest,
  231. hasteFS,
  232. moduleMap
  233. }));
  234. });
  235. }
  236. return this._buildPromise;
  237. }
  238. /**
  239. * 1. read data from the cache or create an empty structure.
  240. */
  241. read() {
  242. let hasteMap;
  243. try {
  244. hasteMap = (_jestSerializer || _load_jestSerializer()).default.readFileSync(this._cachePath);
  245. } catch (err) {
  246. hasteMap = this._createEmptyMap();
  247. }
  248. for (const key in hasteMap) {
  249. Object.setPrototypeOf(hasteMap[key], null);
  250. }
  251. return hasteMap;
  252. }
  253. readModuleMap() {
  254. const data = this.read();
  255. return new (_module_map || _load_module_map()).default({
  256. duplicates: data.duplicates,
  257. map: data.map,
  258. mocks: data.mocks
  259. });
  260. }
  261. /**
  262. * 2. crawl the file system.
  263. */
  264. _buildFileMap() {
  265. const read = this._options.resetCache ? this._createEmptyMap : this.read;
  266. return Promise.resolve().then(() => read.call(this)).catch(() => this._createEmptyMap()).then(cachedHasteMap => {
  267. const cachedFiles = Object.keys(cachedHasteMap.files).map(filePath => {
  268. const moduleName = cachedHasteMap.files[filePath][(_constants || _load_constants()).default.ID];
  269. return { moduleName, path: filePath };
  270. });
  271. return this._crawl(cachedHasteMap).then(hasteMap => {
  272. const deprecatedFiles = cachedFiles.filter(file => {
  273. const fileData = hasteMap.files[file.path];
  274. return fileData == null || file.moduleName !== fileData[(_constants || _load_constants()).default.ID];
  275. });
  276. return { deprecatedFiles, hasteMap };
  277. });
  278. });
  279. }
  280. /**
  281. * 3. parse and extract metadata from changed files.
  282. */
  283. _processFile(hasteMap, map, mocks, filePath, workerOptions) {
  284. const setModule = (id, module) => {
  285. if (!map[id]) {
  286. // $FlowFixMe
  287. map[id] = Object.create(null);
  288. }
  289. const moduleMap = map[id];
  290. const platform = (0, (_get_platform_extension || _load_get_platform_extension()).default)(module[(_constants || _load_constants()).default.PATH], this._options.platforms) || (_constants || _load_constants()).default.GENERIC_PLATFORM;
  291. const existingModule = moduleMap[platform];
  292. if (existingModule && existingModule[(_constants || _load_constants()).default.PATH] !== module[(_constants || _load_constants()).default.PATH]) {
  293. const message = `jest-haste-map: @providesModule naming collision:\n` + ` Duplicate module name: ${id}\n` + ` Paths: ${module[(_constants || _load_constants()).default.PATH]} collides with ` + `${existingModule[(_constants || _load_constants()).default.PATH]}\n\nThis ` + `${this._options.throwOnModuleCollision ? 'error' : 'warning'} ` + `is caused by a @providesModule declaration ` + `with the same name across two different files.`;
  294. if (this._options.throwOnModuleCollision) {
  295. throw new Error(message);
  296. }
  297. this._console.warn(message);
  298. // We do NOT want consumers to use a module that is ambiguous.
  299. delete moduleMap[platform];
  300. if (Object.keys(moduleMap).length === 1) {
  301. delete map[id];
  302. }
  303. let dupsByPlatform = hasteMap.duplicates[id];
  304. if (dupsByPlatform == null) {
  305. dupsByPlatform = hasteMap.duplicates[id] = Object.create(null);
  306. }
  307. const dups = dupsByPlatform[platform] = Object.create(null);
  308. dups[module[(_constants || _load_constants()).default.PATH]] = module[(_constants || _load_constants()).default.TYPE];
  309. dups[existingModule[(_constants || _load_constants()).default.PATH]] = existingModule[(_constants || _load_constants()).default.TYPE];
  310. return;
  311. }
  312. const dupsByPlatform = hasteMap.duplicates[id];
  313. if (dupsByPlatform != null) {
  314. const dups = dupsByPlatform[platform];
  315. if (dups != null) {
  316. dups[module[(_constants || _load_constants()).default.PATH]] = module[(_constants || _load_constants()).default.TYPE];
  317. }
  318. return;
  319. }
  320. moduleMap[platform] = module;
  321. };
  322. // If we retain all files in the virtual HasteFS representation, we avoid
  323. // reading them if they aren't important (node_modules).
  324. if (this._options.retainAllFiles && this._isNodeModulesDir(filePath)) {
  325. return null;
  326. }
  327. if (this._options.mocksPattern && this._options.mocksPattern.test(filePath)) {
  328. const mockPath = (0, (_get_mock_name || _load_get_mock_name()).default)(filePath);
  329. if (mocks[mockPath]) {
  330. this._console.warn(`jest-haste-map: duplicate manual mock found:\n` + ` Module name: ${mockPath}\n` + ` Duplicate Mock path: ${filePath}\nThis warning ` + `is caused by two manual mock files with the same file name.\n` + `Jest will use the mock file found in: \n` + `${filePath}\n` + ` Please delete one of the following two files: \n ` + `${mocks[mockPath]}\n${filePath}\n\n`);
  331. }
  332. mocks[mockPath] = filePath;
  333. }
  334. const fileMetadata = hasteMap.files[filePath];
  335. const moduleMetadata = hasteMap.map[fileMetadata[(_constants || _load_constants()).default.ID]];
  336. if (fileMetadata[(_constants || _load_constants()).default.VISITED]) {
  337. if (!fileMetadata[(_constants || _load_constants()).default.ID]) {
  338. return null;
  339. }
  340. if (moduleMetadata != null) {
  341. const platform = (0, (_get_platform_extension || _load_get_platform_extension()).default)(filePath, this._options.platforms) || (_constants || _load_constants()).default.GENERIC_PLATFORM;
  342. const module = moduleMetadata[platform];
  343. if (module == null) {
  344. return null;
  345. }
  346. const modulesByPlatform = map[fileMetadata[(_constants || _load_constants()).default.ID]] || (map[fileMetadata[(_constants || _load_constants()).default.ID]] = {});
  347. modulesByPlatform[platform] = module;
  348. return null;
  349. }
  350. }
  351. return this._getWorker(workerOptions).worker({
  352. filePath,
  353. hasteImplModulePath: this._options.hasteImplModulePath
  354. }).then(metadata => {
  355. // `1` for truthy values instead of `true` to save cache space.
  356. fileMetadata[(_constants || _load_constants()).default.VISITED] = 1;
  357. const metadataId = metadata.id;
  358. const metadataModule = metadata.module;
  359. if (metadataId && metadataModule) {
  360. fileMetadata[(_constants || _load_constants()).default.ID] = metadataId;
  361. setModule(metadataId, metadataModule);
  362. }
  363. fileMetadata[(_constants || _load_constants()).default.DEPENDENCIES] = metadata.dependencies || [];
  364. }, error => {
  365. if (typeof error !== 'object' || !error.message || !error.stack) {
  366. error = new Error(error);
  367. error.stack = ''; // Remove stack for stack-less errors.
  368. }
  369. // $FlowFixMe: checking error code is OK if error comes from "fs".
  370. if (['ENOENT', 'EACCES'].indexOf(error.code) < 0) {
  371. throw error;
  372. }
  373. // If a file cannot be read we remove it from the file list and
  374. // ignore the failure silently.
  375. delete hasteMap.files[filePath];
  376. });
  377. }
  378. _buildHasteMap(data) {
  379. const deprecatedFiles = data.deprecatedFiles,
  380. hasteMap = data.hasteMap;
  381. const map = Object.create(null);
  382. const mocks = Object.create(null);
  383. const promises = [];
  384. for (let i = 0; i < deprecatedFiles.length; ++i) {
  385. const file = deprecatedFiles[i];
  386. this._recoverDuplicates(hasteMap, file.path, file.moduleName);
  387. }
  388. for (const filePath in hasteMap.files) {
  389. const promise = this._processFile(hasteMap, map, mocks, filePath);
  390. if (promise) {
  391. promises.push(promise);
  392. }
  393. }
  394. return Promise.all(promises).then(() => {
  395. this._cleanup();
  396. hasteMap.map = map;
  397. hasteMap.mocks = mocks;
  398. return hasteMap;
  399. }).catch(error => {
  400. this._cleanup();
  401. return Promise.reject(error);
  402. });
  403. }
  404. _cleanup() {
  405. const worker = this._worker;
  406. if (worker && typeof worker.end === 'function') {
  407. worker.end();
  408. }
  409. this._worker = null;
  410. }
  411. /**
  412. * 4. serialize the new `HasteMap` in a cache file.
  413. */
  414. _persist(hasteMap) {
  415. (_jestSerializer || _load_jestSerializer()).default.writeFileSync(this._cachePath, hasteMap);
  416. }
  417. /**
  418. * Creates workers or parses files and extracts metadata in-process.
  419. */
  420. _getWorker(options) {
  421. if (!this._worker) {
  422. if (options && options.forceInBand || this._options.maxWorkers <= 1) {
  423. this._worker = { worker: (_worker || _load_worker()).worker };
  424. } else {
  425. // $FlowFixMe: assignment of a worker with custom properties.
  426. this._worker = new (_jestWorker || _load_jestWorker()).default(require.resolve('./worker'), {
  427. exposedMethods: ['worker'],
  428. maxRetries: 3,
  429. numWorkers: this._options.maxWorkers
  430. });
  431. }
  432. }
  433. return this._worker;
  434. }
  435. _crawl(hasteMap) {
  436. const options = this._options;
  437. const ignore = this._ignore.bind(this);
  438. const crawl = canUseWatchman && this._options.useWatchman ? (_watchman || _load_watchman()).default : (_node || _load_node()).default;
  439. const retry = error => {
  440. if (crawl === (_watchman || _load_watchman()).default) {
  441. this._console.warn(`jest-haste-map: Watchman crawl failed. Retrying once with node ` + `crawler.\n` + ` Usually this happens when watchman isn't running. Create an ` + `empty \`.watchmanconfig\` file in your project's root folder or ` + `initialize a git or hg repository in your project.\n` + ` ` + error);
  442. return (0, (_node || _load_node()).default)({
  443. data: hasteMap,
  444. extensions: options.extensions,
  445. forceNodeFilesystemAPI: options.forceNodeFilesystemAPI,
  446. ignore,
  447. roots: options.roots
  448. }).catch(e => {
  449. throw new Error(`Crawler retry failed:\n` + ` Original error: ${error.message}\n` + ` Retry error: ${e.message}\n`);
  450. });
  451. }
  452. throw error;
  453. };
  454. try {
  455. return crawl({
  456. data: hasteMap,
  457. extensions: options.extensions,
  458. forceNodeFilesystemAPI: options.forceNodeFilesystemAPI,
  459. ignore,
  460. roots: options.roots
  461. }).catch(retry);
  462. } catch (error) {
  463. return retry(error);
  464. }
  465. }
  466. /**
  467. * Watch mode
  468. */
  469. _watch(hasteMap, hasteFS, moduleMap) {
  470. if (!this._options.watch) {
  471. return Promise.resolve();
  472. }
  473. // In watch mode, we'll only warn about module collisions and we'll retain
  474. // all files, even changes to node_modules.
  475. this._options.throwOnModuleCollision = false;
  476. this._options.retainAllFiles = true;
  477. const Watcher = canUseWatchman && this._options.useWatchman ? (_watchman_watcher || _load_watchman_watcher()).default : (_os || _load_os()).default.platform() === 'darwin' ? (_sane || _load_sane()).default.FSEventsWatcher : (_sane || _load_sane()).default.NodeWatcher;
  478. const extensions = this._options.extensions;
  479. const ignorePattern = this._options.ignorePattern;
  480. let changeQueue = Promise.resolve();
  481. let eventsQueue = [];
  482. // We only need to copy the entire haste map once on every "frame".
  483. let mustCopy = true;
  484. const createWatcher = root => {
  485. const watcher = new Watcher(root, {
  486. dot: false,
  487. glob: extensions.map(extension => '**/*.' + extension),
  488. ignored: ignorePattern
  489. });
  490. return new Promise((resolve, reject) => {
  491. const rejectTimeout = setTimeout(() => reject(new Error('Failed to start watch mode.')), MAX_WAIT_TIME);
  492. watcher.once('ready', () => {
  493. clearTimeout(rejectTimeout);
  494. watcher.on('all', onChange);
  495. resolve(watcher);
  496. });
  497. });
  498. };
  499. const emitChange = () => {
  500. if (eventsQueue.length) {
  501. mustCopy = true;
  502. this.emit('change', {
  503. eventsQueue,
  504. hasteFS: new (_haste_fs || _load_haste_fs()).default(hasteMap.files),
  505. moduleMap: new (_module_map || _load_module_map()).default({
  506. duplicates: hasteMap.duplicates,
  507. map: hasteMap.map,
  508. mocks: hasteMap.mocks
  509. })
  510. });
  511. eventsQueue = [];
  512. }
  513. };
  514. const onChange = (type, filePath, root, stat) => {
  515. filePath = (_path || _load_path()).default.join(root, (0, (_normalize_path_sep || _load_normalize_path_sep()).default)(filePath));
  516. if (this._ignore(filePath) || !extensions.some(extension => filePath.endsWith(extension))) {
  517. return;
  518. }
  519. changeQueue = changeQueue.then(() => {
  520. // If we get duplicate events for the same file, ignore them.
  521. if (eventsQueue.find(event => event.type === type && event.filePath === filePath && (!event.stat && !stat || event.stat && stat && event.stat.mtime.getTime() === stat.mtime.getTime()))) {
  522. return null;
  523. }
  524. if (mustCopy) {
  525. mustCopy = false;
  526. // $FlowFixMe
  527. hasteMap = {
  528. clocks: copy(hasteMap.clocks),
  529. duplicates: copy(hasteMap.duplicates),
  530. files: copy(hasteMap.files),
  531. map: copy(hasteMap.map),
  532. mocks: copy(hasteMap.mocks)
  533. };
  534. }
  535. const add = () => eventsQueue.push({ filePath, stat, type });
  536. // Delete the file and all of its metadata.
  537. const moduleName = hasteMap.files[filePath] && hasteMap.files[filePath][(_constants || _load_constants()).default.ID];
  538. const platform = (0, (_get_platform_extension || _load_get_platform_extension()).default)(filePath, this._options.platforms) || (_constants || _load_constants()).default.GENERIC_PLATFORM;
  539. delete hasteMap.files[filePath];
  540. let moduleMap = hasteMap.map[moduleName];
  541. if (moduleMap != null) {
  542. // We are forced to copy the object because jest-haste-map exposes
  543. // the map as an immutable entity.
  544. moduleMap = copy(moduleMap);
  545. delete moduleMap[platform];
  546. if (Object.keys(moduleMap).length === 0) {
  547. delete hasteMap.map[moduleName];
  548. } else {
  549. // $FlowFixMe
  550. hasteMap.map[moduleName] = moduleMap;
  551. }
  552. }
  553. if (this._options.mocksPattern && this._options.mocksPattern.test(filePath)) {
  554. const mockName = (0, (_get_mock_name || _load_get_mock_name()).default)(filePath);
  555. delete hasteMap.mocks[mockName];
  556. }
  557. this._recoverDuplicates(hasteMap, filePath, moduleName);
  558. // If the file was added or changed,
  559. // parse it and update the haste map.
  560. if (type === 'add' || type === 'change') {
  561. const fileMetadata = ['', stat.mtime.getTime(), 0, []];
  562. hasteMap.files[filePath] = fileMetadata;
  563. const promise = this._processFile(hasteMap, hasteMap.map, hasteMap.mocks, filePath, {
  564. forceInBand: true
  565. });
  566. // Cleanup
  567. this._cleanup();
  568. if (promise) {
  569. return promise.then(add);
  570. } else {
  571. // If a file in node_modules has changed,
  572. // emit an event regardless.
  573. add();
  574. }
  575. } else {
  576. add();
  577. }
  578. return null;
  579. }).catch(error => {
  580. this._console.error(`jest-haste-map: watch error:\n ${error.stack}\n`);
  581. });
  582. };
  583. this._changeInterval = setInterval(emitChange, CHANGE_INTERVAL);
  584. return Promise.all(this._options.roots.map(createWatcher)).then(watchers => {
  585. this._watchers = watchers;
  586. });
  587. }
  588. /**
  589. * This function should be called when the file under `filePath` is removed
  590. * or changed. When that happens, we want to figure out if that file was
  591. * part of a group of files that had the same ID. If it was, we want to
  592. * remove it from the group. Furthermore, if there is only one file
  593. * remaining in the group, then we want to restore that single file as the
  594. * correct resolution for its ID, and cleanup the duplicates index.
  595. */
  596. _recoverDuplicates(hasteMap, filePath, moduleName) {
  597. let dupsByPlatform = hasteMap.duplicates[moduleName];
  598. if (dupsByPlatform == null) {
  599. return;
  600. }
  601. const platform = (0, (_get_platform_extension || _load_get_platform_extension()).default)(filePath, this._options.platforms) || (_constants || _load_constants()).default.GENERIC_PLATFORM;
  602. let dups = dupsByPlatform[platform];
  603. if (dups == null) {
  604. return;
  605. }
  606. dupsByPlatform = hasteMap.duplicates[moduleName] = copy(dupsByPlatform);
  607. dups = dupsByPlatform[platform] = copy(dups);
  608. const dedupType = dups[filePath];
  609. delete dups[filePath];
  610. const filePaths = Object.keys(dups);
  611. if (filePaths.length > 1) {
  612. return;
  613. }
  614. let dedupMap = hasteMap.map[moduleName];
  615. if (dedupMap == null) {
  616. dedupMap = hasteMap.map[moduleName] = Object.create(null);
  617. }
  618. dedupMap[platform] = [filePaths[0], dedupType];
  619. delete dupsByPlatform[platform];
  620. if (Object.keys(dupsByPlatform).length === 0) {
  621. delete hasteMap.duplicates[moduleName];
  622. }
  623. }
  624. end() {
  625. clearInterval(this._changeInterval);
  626. if (!this._watchers.length) {
  627. return Promise.resolve();
  628. }
  629. return Promise.all(this._watchers.map(watcher => new Promise(resolve => watcher.close(resolve)))).then(() => {
  630. this._watchers = [];
  631. });
  632. }
  633. /**
  634. * Helpers
  635. */
  636. _ignore(filePath) {
  637. const ignorePattern = this._options.ignorePattern;
  638. const ignoreMatched = ignorePattern instanceof RegExp ? ignorePattern.test(filePath) : ignorePattern(filePath);
  639. return ignoreMatched || !this._options.retainAllFiles && this._isNodeModulesDir(filePath);
  640. }
  641. _isNodeModulesDir(filePath) {
  642. if (!filePath.includes(NODE_MODULES)) {
  643. return false;
  644. }
  645. if (this._whitelist) {
  646. const whitelist = this._whitelist;
  647. const match = whitelist.exec(filePath);
  648. const matchEndIndex = whitelist.lastIndex;
  649. whitelist.lastIndex = 0;
  650. if (!match) {
  651. return true;
  652. }
  653. const filePathInPackage = filePath.substr(matchEndIndex);
  654. return filePathInPackage.startsWith(NODE_MODULES);
  655. }
  656. return true;
  657. }
  658. _createEmptyMap() {
  659. // $FlowFixMe
  660. return {
  661. clocks: Object.create(null),
  662. duplicates: Object.create(null),
  663. files: Object.create(null),
  664. map: Object.create(null),
  665. mocks: Object.create(null)
  666. };
  667. }
  668. }
  669. const copy = object => Object.assign(Object.create(null), object);
  670. HasteMap.H = (_constants || _load_constants()).default;
  671. HasteMap.ModuleMap = (_module_map || _load_module_map()).default;
  672. module.exports = HasteMap;