index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. 'use strict';var _crypto;
  2. function _load_crypto() {return _crypto = _interopRequireDefault(require('crypto'));}var _fs;
  3. function _load_fs() {return _fs = _interopRequireDefault(require('fs'));}var _path;
  4. function _load_path() {return _path = _interopRequireDefault(require('path'));}var _babelPresetJest;
  5. function _load_babelPresetJest() {return _babelPresetJest = _interopRequireDefault(require('babel-preset-jest'));}var _babelCore;
  6. function _load_babelCore() {return _babelCore = require('babel-core');}var _babelPluginIstanbul;
  7. function _load_babelPluginIstanbul() {return _babelPluginIstanbul = _interopRequireDefault(require('babel-plugin-istanbul'));}function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };} /**
  8. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  9. *
  10. * This source code is licensed under the MIT license found in the
  11. * LICENSE file in the root directory of this source tree.
  12. *
  13. *
  14. */const BABELRC_FILENAME = '.babelrc';const BABELRC_JS_FILENAME = '.babelrc.js';const BABEL_CONFIG_KEY = 'babel';const PACKAGE_JSON = 'package.json';const THIS_FILE = (_fs || _load_fs()).default.readFileSync(__filename);
  15. const createTransformer = options => {
  16. const cache = Object.create(null);
  17. const getBabelRC = filename => {
  18. const paths = [];
  19. let directory = filename;
  20. while (directory !== (directory = (_path || _load_path()).default.dirname(directory))) {
  21. if (cache[directory]) {
  22. break;
  23. }
  24. paths.push(directory);
  25. const configFilePath = (_path || _load_path()).default.join(directory, BABELRC_FILENAME);
  26. if ((_fs || _load_fs()).default.existsSync(configFilePath)) {
  27. cache[directory] = (_fs || _load_fs()).default.readFileSync(configFilePath, 'utf8');
  28. break;
  29. }
  30. const configJsFilePath = (_path || _load_path()).default.join(directory, BABELRC_JS_FILENAME);
  31. if ((_fs || _load_fs()).default.existsSync(configJsFilePath)) {
  32. // $FlowFixMe
  33. cache[directory] = JSON.stringify(require(configJsFilePath));
  34. break;
  35. }
  36. const resolvedJsonFilePath = (_path || _load_path()).default.join(directory, PACKAGE_JSON);
  37. const packageJsonFilePath =
  38. resolvedJsonFilePath === PACKAGE_JSON ?
  39. (_path || _load_path()).default.resolve(directory, PACKAGE_JSON) :
  40. resolvedJsonFilePath;
  41. if ((_fs || _load_fs()).default.existsSync(packageJsonFilePath)) {
  42. // $FlowFixMe
  43. const packageJsonFileContents = require(packageJsonFilePath);
  44. if (packageJsonFileContents[BABEL_CONFIG_KEY]) {
  45. cache[directory] = JSON.stringify(
  46. packageJsonFileContents[BABEL_CONFIG_KEY]);
  47. break;
  48. }
  49. }
  50. }
  51. paths.forEach(directoryPath => cache[directoryPath] = cache[directory]);
  52. return cache[directory] || '';
  53. };
  54. options = Object.assign({}, options, {
  55. plugins: options && options.plugins || [],
  56. presets: (options && options.presets || []).concat([(_babelPresetJest || _load_babelPresetJest()).default]),
  57. retainLines: true,
  58. sourceMaps: 'inline' });
  59. delete options.cacheDirectory;
  60. delete options.filename;
  61. return {
  62. canInstrument: true,
  63. getCacheKey(
  64. fileData,
  65. filename,
  66. configString, _ref)
  67. {let instrument = _ref.instrument,rootDir = _ref.rootDir;
  68. return (_crypto || _load_crypto()).default.
  69. createHash('md5').
  70. update(THIS_FILE).
  71. update('\0', 'utf8').
  72. update(fileData).
  73. update('\0', 'utf8').
  74. update((_path || _load_path()).default.relative(rootDir, filename)).
  75. update('\0', 'utf8').
  76. update(configString).
  77. update('\0', 'utf8').
  78. update(getBabelRC(filename)).
  79. update('\0', 'utf8').
  80. update(instrument ? 'instrument' : '').
  81. digest('hex');
  82. },
  83. process(
  84. src,
  85. filename,
  86. config,
  87. transformOptions)
  88. {
  89. if ((_babelCore || _load_babelCore()).util && !(_babelCore || _load_babelCore()).util.canCompile(filename)) {
  90. return src;
  91. }
  92. const theseOptions = Object.assign({ filename }, options);
  93. if (transformOptions && transformOptions.instrument) {
  94. theseOptions.auxiliaryCommentBefore = ' istanbul ignore next ';
  95. // Copied from jest-runtime transform.js
  96. theseOptions.plugins = theseOptions.plugins.concat([
  97. [(_babelPluginIstanbul || _load_babelPluginIstanbul()).default,
  98. {
  99. // files outside `cwd` will not be instrumented
  100. cwd: config.rootDir,
  101. exclude: [] }]]);
  102. }
  103. // babel v7 might return null in the case when the file has been ignored.
  104. const transformResult = (0, (_babelCore || _load_babelCore()).transform)(src, theseOptions);
  105. return transformResult ? transformResult.code : src;
  106. } };
  107. };
  108. module.exports = createTransformer();
  109. module.exports.createTransformer = createTransformer;