227 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
		
		
			
		
	
	
			227 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
|  | 'use strict'; | ||
|  | 
 | ||
|  | function _crypto() { | ||
|  |   const data = _interopRequireDefault(require('crypto')); | ||
|  | 
 | ||
|  |   _crypto = function _crypto() { | ||
|  |     return data; | ||
|  |   }; | ||
|  | 
 | ||
|  |   return data; | ||
|  | } | ||
|  | 
 | ||
|  | function _fs() { | ||
|  |   const data = _interopRequireDefault(require('fs')); | ||
|  | 
 | ||
|  |   _fs = function _fs() { | ||
|  |     return data; | ||
|  |   }; | ||
|  | 
 | ||
|  |   return data; | ||
|  | } | ||
|  | 
 | ||
|  | function _path() { | ||
|  |   const data = _interopRequireDefault(require('path')); | ||
|  | 
 | ||
|  |   _path = function _path() { | ||
|  |     return data; | ||
|  |   }; | ||
|  | 
 | ||
|  |   return data; | ||
|  | } | ||
|  | 
 | ||
|  | function _core() { | ||
|  |   const data = require('@babel/core'); | ||
|  | 
 | ||
|  |   _core = function _core() { | ||
|  |     return data; | ||
|  |   }; | ||
|  | 
 | ||
|  |   return data; | ||
|  | } | ||
|  | 
 | ||
|  | function _chalk() { | ||
|  |   const data = _interopRequireDefault(require('chalk')); | ||
|  | 
 | ||
|  |   _chalk = function _chalk() { | ||
|  |     return data; | ||
|  |   }; | ||
|  | 
 | ||
|  |   return data; | ||
|  | } | ||
|  | 
 | ||
|  | function _slash() { | ||
|  |   const data = _interopRequireDefault(require('slash')); | ||
|  | 
 | ||
|  |   _slash = function _slash() { | ||
|  |     return data; | ||
|  |   }; | ||
|  | 
 | ||
|  |   return data; | ||
|  | } | ||
|  | 
 | ||
|  | function _interopRequireDefault(obj) { | ||
|  |   return obj && obj.__esModule ? obj : {default: obj}; | ||
|  | } | ||
|  | 
 | ||
|  | function _objectSpread(target) { | ||
|  |   for (var i = 1; i < arguments.length; i++) { | ||
|  |     var source = arguments[i] != null ? arguments[i] : {}; | ||
|  |     var ownKeys = Object.keys(source); | ||
|  |     if (typeof Object.getOwnPropertySymbols === 'function') { | ||
|  |       ownKeys = ownKeys.concat( | ||
|  |         Object.getOwnPropertySymbols(source).filter(function(sym) { | ||
|  |           return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
|  |         }) | ||
|  |       ); | ||
|  |     } | ||
|  |     ownKeys.forEach(function(key) { | ||
|  |       _defineProperty(target, key, source[key]); | ||
|  |     }); | ||
|  |   } | ||
|  |   return target; | ||
|  | } | ||
|  | 
 | ||
|  | function _defineProperty(obj, key, value) { | ||
|  |   if (key in obj) { | ||
|  |     Object.defineProperty(obj, key, { | ||
|  |       value: value, | ||
|  |       enumerable: true, | ||
|  |       configurable: true, | ||
|  |       writable: true | ||
|  |     }); | ||
|  |   } else { | ||
|  |     obj[key] = value; | ||
|  |   } | ||
|  |   return obj; | ||
|  | } | ||
|  | 
 | ||
|  | const THIS_FILE = _fs().default.readFileSync(__filename); | ||
|  | 
 | ||
|  | const jestPresetPath = require.resolve('babel-preset-jest'); | ||
|  | 
 | ||
|  | const babelIstanbulPlugin = require.resolve('babel-plugin-istanbul'); // Narrow down the types
 | ||
|  | 
 | ||
|  | const createTransformer = (options = {}) => { | ||
|  |   options = _objectSpread({}, options, { | ||
|  |     caller: { | ||
|  |       name: 'babel-jest', | ||
|  |       supportsStaticESM: false | ||
|  |     }, | ||
|  |     compact: false, | ||
|  |     plugins: (options && options.plugins) || [], | ||
|  |     presets: ((options && options.presets) || []).concat(jestPresetPath), | ||
|  |     sourceMaps: 'both' | ||
|  |   }); | ||
|  | 
 | ||
|  |   function loadBabelConfig(cwd, filename) { | ||
|  |     // `cwd` first to allow incoming options to override it
 | ||
|  |     const babelConfig = (0, _core().loadPartialConfig)( | ||
|  |       _objectSpread( | ||
|  |         { | ||
|  |           cwd | ||
|  |         }, | ||
|  |         options, | ||
|  |         { | ||
|  |           filename | ||
|  |         } | ||
|  |       ) | ||
|  |     ); | ||
|  | 
 | ||
|  |     if (!babelConfig) { | ||
|  |       throw new Error( | ||
|  |         `babel-jest: Babel ignores ${_chalk().default.bold( | ||
|  |           (0, _slash().default)(_path().default.relative(cwd, filename)) | ||
|  |         )} - make sure to include the file in Jest's ${_chalk().default.bold( | ||
|  |           'transformIgnorePatterns' | ||
|  |         )} as well.`
 | ||
|  |       ); | ||
|  |     } | ||
|  | 
 | ||
|  |     return babelConfig; | ||
|  |   } | ||
|  | 
 | ||
|  |   return { | ||
|  |     canInstrument: true, | ||
|  | 
 | ||
|  |     getCacheKey( | ||
|  |       fileData, | ||
|  |       filename, | ||
|  |       configString, | ||
|  |       {config, instrument, rootDir} | ||
|  |     ) { | ||
|  |       const babelOptions = loadBabelConfig(config.cwd, filename); | ||
|  |       const configPath = [ | ||
|  |         babelOptions.config || '', | ||
|  |         babelOptions.babelrc || '' | ||
|  |       ]; | ||
|  |       return _crypto() | ||
|  |         .default.createHash('md5') | ||
|  |         .update(THIS_FILE) | ||
|  |         .update('\0', 'utf8') | ||
|  |         .update(JSON.stringify(babelOptions.options)) | ||
|  |         .update('\0', 'utf8') | ||
|  |         .update(fileData) | ||
|  |         .update('\0', 'utf8') | ||
|  |         .update(_path().default.relative(rootDir, filename)) | ||
|  |         .update('\0', 'utf8') | ||
|  |         .update(configString) | ||
|  |         .update('\0', 'utf8') | ||
|  |         .update(configPath.join('')) | ||
|  |         .update('\0', 'utf8') | ||
|  |         .update(instrument ? 'instrument' : '') | ||
|  |         .update('\0', 'utf8') | ||
|  |         .update(process.env.NODE_ENV || '') | ||
|  |         .update('\0', 'utf8') | ||
|  |         .update(process.env.BABEL_ENV || '') | ||
|  |         .digest('hex'); | ||
|  |     }, | ||
|  | 
 | ||
|  |     process(src, filename, config, transformOptions) { | ||
|  |       const babelOptions = _objectSpread( | ||
|  |         {}, | ||
|  |         loadBabelConfig(config.cwd, filename).options | ||
|  |       ); | ||
|  | 
 | ||
|  |       if (transformOptions && transformOptions.instrument) { | ||
|  |         babelOptions.auxiliaryCommentBefore = ' istanbul ignore next '; // Copied from jest-runtime transform.js
 | ||
|  | 
 | ||
|  |         babelOptions.plugins = (babelOptions.plugins || []).concat([ | ||
|  |           [ | ||
|  |             babelIstanbulPlugin, | ||
|  |             { | ||
|  |               // files outside `cwd` will not be instrumented
 | ||
|  |               cwd: config.rootDir, | ||
|  |               exclude: [] | ||
|  |             } | ||
|  |           ] | ||
|  |         ]); | ||
|  |       } | ||
|  | 
 | ||
|  |       const transformResult = (0, _core().transformSync)(src, babelOptions); | ||
|  | 
 | ||
|  |       if (transformResult) { | ||
|  |         const code = transformResult.code, | ||
|  |           map = transformResult.map; | ||
|  | 
 | ||
|  |         if (typeof code === 'string') { | ||
|  |           return { | ||
|  |             code, | ||
|  |             map | ||
|  |           }; | ||
|  |         } | ||
|  |       } | ||
|  | 
 | ||
|  |       return src; | ||
|  |     } | ||
|  |   }; | ||
|  | }; | ||
|  | 
 | ||
|  | const transformer = _objectSpread({}, createTransformer(), { | ||
|  |   // Assigned here so only the exported transformer has `createTransformer`,
 | ||
|  |   // instead of all created transformers by the function
 | ||
|  |   createTransformer | ||
|  | }); | ||
|  | 
 | ||
|  | module.exports = transformer; |