index.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. 'use strict';
  2. var _crypto;
  3. function _load_crypto() {
  4. return (_crypto = _interopRequireDefault(require('crypto')));
  5. }
  6. var _fs;
  7. function _load_fs() {
  8. return (_fs = _interopRequireDefault(require('fs')));
  9. }
  10. var _path;
  11. function _load_path() {
  12. return (_path = _interopRequireDefault(require('path')));
  13. }
  14. var _babelPresetJest;
  15. function _load_babelPresetJest() {
  16. return (_babelPresetJest = _interopRequireDefault(
  17. require('babel-preset-jest')
  18. ));
  19. }
  20. var _babelCore;
  21. function _load_babelCore() {
  22. return (_babelCore = require('babel-core'));
  23. }
  24. var _babelPluginIstanbul;
  25. function _load_babelPluginIstanbul() {
  26. return (_babelPluginIstanbul = _interopRequireDefault(
  27. require('babel-plugin-istanbul')
  28. ));
  29. }
  30. function _interopRequireDefault(obj) {
  31. return obj && obj.__esModule ? obj : {default: obj};
  32. }
  33. /**
  34. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  35. *
  36. * This source code is licensed under the MIT license found in the
  37. * LICENSE file in the root directory of this source tree.
  38. *
  39. *
  40. */
  41. const BABELRC_FILENAME = '.babelrc';
  42. const BABELRC_JS_FILENAME = '.babelrc.js';
  43. const BABEL_CONFIG_JS_FILENAME = 'babel.config.js';
  44. const BABEL_CONFIG_KEY = 'babel';
  45. const PACKAGE_JSON = 'package.json';
  46. const THIS_FILE = (_fs || _load_fs()).default.readFileSync(__filename);
  47. const createTransformer = options => {
  48. const cache = Object.create(null);
  49. const getBabelRC = filename => {
  50. const paths = [];
  51. let directory = filename;
  52. while (
  53. directory !==
  54. (directory = (_path || _load_path()).default.dirname(directory))
  55. ) {
  56. if (cache[directory]) {
  57. break;
  58. }
  59. paths.push(directory);
  60. const configFilePath = (_path || _load_path()).default.join(
  61. directory,
  62. BABELRC_FILENAME
  63. );
  64. if ((_fs || _load_fs()).default.existsSync(configFilePath)) {
  65. cache[directory] = (_fs || _load_fs()).default.readFileSync(
  66. configFilePath,
  67. 'utf8'
  68. );
  69. break;
  70. }
  71. let configJsFilePath = (_path || _load_path()).default.join(
  72. directory,
  73. BABELRC_JS_FILENAME
  74. );
  75. if ((_fs || _load_fs()).default.existsSync(configJsFilePath)) {
  76. // $FlowFixMe
  77. cache[directory] = JSON.stringify(require(configJsFilePath));
  78. break;
  79. }
  80. configJsFilePath = (_path || _load_path()).default.join(
  81. directory,
  82. BABEL_CONFIG_JS_FILENAME
  83. );
  84. if ((_fs || _load_fs()).default.existsSync(configJsFilePath)) {
  85. // $FlowFixMe
  86. cache[directory] = JSON.stringify(require(configJsFilePath));
  87. break;
  88. }
  89. const resolvedJsonFilePath = (_path || _load_path()).default.join(
  90. directory,
  91. PACKAGE_JSON
  92. );
  93. const packageJsonFilePath =
  94. resolvedJsonFilePath === PACKAGE_JSON
  95. ? (_path || _load_path()).default.resolve(directory, PACKAGE_JSON)
  96. : resolvedJsonFilePath;
  97. if ((_fs || _load_fs()).default.existsSync(packageJsonFilePath)) {
  98. // $FlowFixMe
  99. const packageJsonFileContents = require(packageJsonFilePath);
  100. if (packageJsonFileContents[BABEL_CONFIG_KEY]) {
  101. cache[directory] = JSON.stringify(
  102. packageJsonFileContents[BABEL_CONFIG_KEY]
  103. );
  104. break;
  105. }
  106. }
  107. }
  108. paths.forEach(directoryPath => (cache[directoryPath] = cache[directory]));
  109. return cache[directory] || '';
  110. };
  111. options = Object.assign({}, options, {
  112. compact: false,
  113. plugins: (options && options.plugins) || [],
  114. presets: ((options && options.presets) || []).concat([
  115. (_babelPresetJest || _load_babelPresetJest()).default
  116. ]),
  117. sourceMaps: 'both'
  118. });
  119. delete options.cacheDirectory;
  120. delete options.filename;
  121. return {
  122. canInstrument: true,
  123. getCacheKey: function(fileData, filename, configString, _ref) {
  124. let instrument = _ref.instrument,
  125. rootDir = _ref.rootDir;
  126. return (_crypto || _load_crypto()).default
  127. .createHash('md5')
  128. .update(THIS_FILE)
  129. .update('\0', 'utf8')
  130. .update(JSON.stringify(options))
  131. .update('\0', 'utf8')
  132. .update(fileData)
  133. .update('\0', 'utf8')
  134. .update((_path || _load_path()).default.relative(rootDir, filename))
  135. .update('\0', 'utf8')
  136. .update(configString)
  137. .update('\0', 'utf8')
  138. .update(getBabelRC(filename))
  139. .update('\0', 'utf8')
  140. .update(instrument ? 'instrument' : '')
  141. .digest('hex');
  142. },
  143. process: function(src, filename, config, transformOptions) {
  144. const altExts = config.moduleFileExtensions.map(
  145. extension => '.' + extension
  146. );
  147. if (
  148. (_babelCore || _load_babelCore()).util &&
  149. !(_babelCore || _load_babelCore()).util.canCompile(filename, altExts)
  150. ) {
  151. return src;
  152. }
  153. const theseOptions = Object.assign({filename: filename}, options);
  154. if (transformOptions && transformOptions.instrument) {
  155. theseOptions.auxiliaryCommentBefore = ' istanbul ignore next ';
  156. // Copied from jest-runtime transform.js
  157. theseOptions.plugins = theseOptions.plugins.concat([
  158. [
  159. (_babelPluginIstanbul || _load_babelPluginIstanbul()).default,
  160. {
  161. // files outside `cwd` will not be instrumented
  162. cwd: config.rootDir,
  163. exclude: []
  164. }
  165. ]
  166. ]);
  167. }
  168. // babel v7 might return null in the case when the file has been ignored.
  169. const transformResult = (0, (_babelCore || _load_babelCore()).transform)(
  170. src,
  171. theseOptions
  172. );
  173. return transformResult || src;
  174. }
  175. };
  176. };
  177. module.exports = createTransformer();
  178. module.exports.createTransformer = createTransformer;