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