index.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = exports.createTransformer = void 0;
  6. function _crypto() {
  7. const data = require('crypto');
  8. _crypto = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function path() {
  14. const data = _interopRequireWildcard(require('path'));
  15. path = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _core() {
  21. const data = require('@babel/core');
  22. _core = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _chalk() {
  28. const data = _interopRequireDefault(require('chalk'));
  29. _chalk = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function fs() {
  35. const data = _interopRequireWildcard(require('graceful-fs'));
  36. fs = function () {
  37. return data;
  38. };
  39. return data;
  40. }
  41. function _slash() {
  42. const data = _interopRequireDefault(require('slash'));
  43. _slash = function () {
  44. return data;
  45. };
  46. return data;
  47. }
  48. var _loadBabelConfig = require('./loadBabelConfig');
  49. function _interopRequireDefault(obj) {
  50. return obj && obj.__esModule ? obj : {default: obj};
  51. }
  52. function _getRequireWildcardCache(nodeInterop) {
  53. if (typeof WeakMap !== 'function') return null;
  54. var cacheBabelInterop = new WeakMap();
  55. var cacheNodeInterop = new WeakMap();
  56. return (_getRequireWildcardCache = function (nodeInterop) {
  57. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  58. })(nodeInterop);
  59. }
  60. function _interopRequireWildcard(obj, nodeInterop) {
  61. if (!nodeInterop && obj && obj.__esModule) {
  62. return obj;
  63. }
  64. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  65. return {default: obj};
  66. }
  67. var cache = _getRequireWildcardCache(nodeInterop);
  68. if (cache && cache.has(obj)) {
  69. return cache.get(obj);
  70. }
  71. var newObj = {};
  72. var hasPropertyDescriptor =
  73. Object.defineProperty && Object.getOwnPropertyDescriptor;
  74. for (var key in obj) {
  75. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  76. var desc = hasPropertyDescriptor
  77. ? Object.getOwnPropertyDescriptor(obj, key)
  78. : null;
  79. if (desc && (desc.get || desc.set)) {
  80. Object.defineProperty(newObj, key, desc);
  81. } else {
  82. newObj[key] = obj[key];
  83. }
  84. }
  85. }
  86. newObj.default = obj;
  87. if (cache) {
  88. cache.set(obj, newObj);
  89. }
  90. return newObj;
  91. }
  92. /**
  93. * Copyright (c) Meta Platforms, Inc. and affiliates.
  94. *
  95. * This source code is licensed under the MIT license found in the
  96. * LICENSE file in the root directory of this source tree.
  97. */
  98. const THIS_FILE = fs().readFileSync(__filename);
  99. const jestPresetPath = require.resolve('babel-preset-jest');
  100. const babelIstanbulPlugin = require.resolve('babel-plugin-istanbul');
  101. function assertLoadedBabelConfig(babelConfig, cwd, filename) {
  102. if (!babelConfig) {
  103. throw new Error(
  104. `babel-jest: Babel ignores ${_chalk().default.bold(
  105. (0, _slash().default)(path().relative(cwd, filename))
  106. )} - make sure to include the file in Jest's ${_chalk().default.bold(
  107. 'transformIgnorePatterns'
  108. )} as well.`
  109. );
  110. }
  111. }
  112. function addIstanbulInstrumentation(babelOptions, transformOptions) {
  113. if (transformOptions.instrument) {
  114. const copiedBabelOptions = {
  115. ...babelOptions
  116. };
  117. copiedBabelOptions.auxiliaryCommentBefore = ' istanbul ignore next ';
  118. // Copied from jest-runtime transform.js
  119. copiedBabelOptions.plugins = (copiedBabelOptions.plugins ?? []).concat([
  120. [
  121. babelIstanbulPlugin,
  122. {
  123. // files outside `cwd` will not be instrumented
  124. cwd: transformOptions.config.cwd,
  125. exclude: []
  126. }
  127. ]
  128. ]);
  129. return copiedBabelOptions;
  130. }
  131. return babelOptions;
  132. }
  133. function getCacheKeyFromConfig(
  134. sourceText,
  135. sourcePath,
  136. babelOptions,
  137. transformOptions
  138. ) {
  139. const {config, configString, instrument} = transformOptions;
  140. const configPath = [babelOptions.config ?? '', babelOptions.babelrc ?? ''];
  141. return (0, _crypto().createHash)('sha1')
  142. .update(THIS_FILE)
  143. .update('\0', 'utf8')
  144. .update(JSON.stringify(babelOptions.options))
  145. .update('\0', 'utf8')
  146. .update(sourceText)
  147. .update('\0', 'utf8')
  148. .update(path().relative(config.rootDir, sourcePath))
  149. .update('\0', 'utf8')
  150. .update(configString)
  151. .update('\0', 'utf8')
  152. .update(configPath.join(''))
  153. .update('\0', 'utf8')
  154. .update(instrument ? 'instrument' : '')
  155. .update('\0', 'utf8')
  156. .update(process.env.NODE_ENV ?? '')
  157. .update('\0', 'utf8')
  158. .update(process.env.BABEL_ENV ?? '')
  159. .update('\0', 'utf8')
  160. .update(process.version)
  161. .digest('hex')
  162. .substring(0, 32);
  163. }
  164. function loadBabelConfig(cwd, filename, transformOptions) {
  165. const babelConfig = (0, _loadBabelConfig.loadPartialConfig)(transformOptions);
  166. assertLoadedBabelConfig(babelConfig, cwd, filename);
  167. return babelConfig;
  168. }
  169. async function loadBabelConfigAsync(cwd, filename, transformOptions) {
  170. const babelConfig = await (0, _loadBabelConfig.loadPartialConfigAsync)(
  171. transformOptions
  172. );
  173. assertLoadedBabelConfig(babelConfig, cwd, filename);
  174. return babelConfig;
  175. }
  176. function loadBabelOptions(
  177. cwd,
  178. filename,
  179. transformOptions,
  180. jestTransformOptions
  181. ) {
  182. const {options} = loadBabelConfig(cwd, filename, transformOptions);
  183. return addIstanbulInstrumentation(options, jestTransformOptions);
  184. }
  185. async function loadBabelOptionsAsync(
  186. cwd,
  187. filename,
  188. transformOptions,
  189. jestTransformOptions
  190. ) {
  191. const {options} = await loadBabelConfigAsync(cwd, filename, transformOptions);
  192. return addIstanbulInstrumentation(options, jestTransformOptions);
  193. }
  194. const createTransformer = userOptions => {
  195. const inputOptions = userOptions ?? {};
  196. const options = {
  197. ...inputOptions,
  198. caller: {
  199. name: 'babel-jest',
  200. supportsDynamicImport: false,
  201. supportsExportNamespaceFrom: false,
  202. supportsStaticESM: false,
  203. supportsTopLevelAwait: false,
  204. ...inputOptions.caller
  205. },
  206. compact: false,
  207. plugins: inputOptions.plugins ?? [],
  208. presets: (inputOptions.presets ?? []).concat(jestPresetPath),
  209. sourceMaps: 'both'
  210. };
  211. function mergeBabelTransformOptions(filename, transformOptions) {
  212. const {cwd, rootDir} = transformOptions.config;
  213. // `cwd` and `root` first to allow incoming options to override it
  214. return {
  215. cwd,
  216. root: rootDir,
  217. ...options,
  218. caller: {
  219. ...options.caller,
  220. supportsDynamicImport:
  221. transformOptions.supportsDynamicImport ??
  222. options.caller.supportsDynamicImport,
  223. supportsExportNamespaceFrom:
  224. transformOptions.supportsExportNamespaceFrom ??
  225. options.caller.supportsExportNamespaceFrom,
  226. supportsStaticESM:
  227. transformOptions.supportsStaticESM ??
  228. options.caller.supportsStaticESM,
  229. supportsTopLevelAwait:
  230. transformOptions.supportsTopLevelAwait ??
  231. options.caller.supportsTopLevelAwait
  232. },
  233. filename
  234. };
  235. }
  236. return {
  237. canInstrument: true,
  238. getCacheKey(sourceText, sourcePath, transformOptions) {
  239. const babelOptions = loadBabelConfig(
  240. transformOptions.config.cwd,
  241. sourcePath,
  242. mergeBabelTransformOptions(sourcePath, transformOptions)
  243. );
  244. return getCacheKeyFromConfig(
  245. sourceText,
  246. sourcePath,
  247. babelOptions,
  248. transformOptions
  249. );
  250. },
  251. async getCacheKeyAsync(sourceText, sourcePath, transformOptions) {
  252. const babelOptions = await loadBabelConfigAsync(
  253. transformOptions.config.cwd,
  254. sourcePath,
  255. mergeBabelTransformOptions(sourcePath, transformOptions)
  256. );
  257. return getCacheKeyFromConfig(
  258. sourceText,
  259. sourcePath,
  260. babelOptions,
  261. transformOptions
  262. );
  263. },
  264. process(sourceText, sourcePath, transformOptions) {
  265. const babelOptions = loadBabelOptions(
  266. transformOptions.config.cwd,
  267. sourcePath,
  268. mergeBabelTransformOptions(sourcePath, transformOptions),
  269. transformOptions
  270. );
  271. const transformResult = (0, _core().transformSync)(
  272. sourceText,
  273. babelOptions
  274. );
  275. if (transformResult) {
  276. const {code, map} = transformResult;
  277. if (typeof code === 'string') {
  278. return {
  279. code,
  280. map
  281. };
  282. }
  283. }
  284. return {
  285. code: sourceText
  286. };
  287. },
  288. async processAsync(sourceText, sourcePath, transformOptions) {
  289. const babelOptions = await loadBabelOptionsAsync(
  290. transformOptions.config.cwd,
  291. sourcePath,
  292. mergeBabelTransformOptions(sourcePath, transformOptions),
  293. transformOptions
  294. );
  295. const transformResult = await (0, _core().transformAsync)(
  296. sourceText,
  297. babelOptions
  298. );
  299. if (transformResult) {
  300. const {code, map} = transformResult;
  301. if (typeof code === 'string') {
  302. return {
  303. code,
  304. map
  305. };
  306. }
  307. }
  308. return {
  309. code: sourceText
  310. };
  311. }
  312. };
  313. };
  314. exports.createTransformer = createTransformer;
  315. const transformerFactory = {
  316. // Assigned here, instead of as a separate export, due to limitations in Jest's
  317. // requireOrImportModule, requiring all exports to be on the `default` export
  318. createTransformer
  319. };
  320. var _default = transformerFactory;
  321. exports.default = _default;