UNPKG

6.98 kBJavaScriptView Raw
1'use strict';
2
3function _crypto() {
4 const data = require('crypto');
5
6 _crypto = function () {
7 return data;
8 };
9
10 return data;
11}
12
13function path() {
14 const data = _interopRequireWildcard(require('path'));
15
16 path = function () {
17 return data;
18 };
19
20 return data;
21}
22
23function fs() {
24 const data = _interopRequireWildcard(require('graceful-fs'));
25
26 fs = function () {
27 return data;
28 };
29
30 return data;
31}
32
33function _core() {
34 const data = require('@babel/core');
35
36 _core = function () {
37 return data;
38 };
39
40 return data;
41}
42
43function _chalk() {
44 const data = _interopRequireDefault(require('chalk'));
45
46 _chalk = function () {
47 return data;
48 };
49
50 return data;
51}
52
53function _slash() {
54 const data = _interopRequireDefault(require('slash'));
55
56 _slash = function () {
57 return data;
58 };
59
60 return data;
61}
62
63var _loadBabelConfig = require('./loadBabelConfig');
64
65function _interopRequireDefault(obj) {
66 return obj && obj.__esModule ? obj : {default: obj};
67}
68
69function _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
78function _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 */
117const THIS_FILE = fs().readFileSync(__filename);
118
119const jestPresetPath = require.resolve('babel-preset-jest');
120
121const babelIstanbulPlugin = require.resolve('babel-plugin-istanbul'); // Narrow down the types
122
123const 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 supportsStaticESM: false,
134 ...inputOptions.caller
135 },
136 compact: false,
137 plugins:
138 (_inputOptions$plugins = inputOptions.plugins) !== null &&
139 _inputOptions$plugins !== void 0
140 ? _inputOptions$plugins
141 : [],
142 presets: ((_inputOptions$presets = inputOptions.presets) !== null &&
143 _inputOptions$presets !== void 0
144 ? _inputOptions$presets
145 : []
146 ).concat(jestPresetPath),
147 sourceMaps: 'both'
148 };
149
150 function loadBabelConfig(cwd, filename, transformOptions) {
151 var _transformOptions$sup, _transformOptions$sup2;
152
153 // `cwd` first to allow incoming options to override it
154 const babelConfig = (0, _loadBabelConfig.loadPartialConfig)({
155 cwd,
156 ...options,
157 caller: {
158 ...options.caller,
159 supportsDynamicImport:
160 (_transformOptions$sup =
161 transformOptions === null || transformOptions === void 0
162 ? void 0
163 : transformOptions.supportsDynamicImport) !== null &&
164 _transformOptions$sup !== void 0
165 ? _transformOptions$sup
166 : options.caller.supportsDynamicImport,
167 supportsStaticESM:
168 (_transformOptions$sup2 =
169 transformOptions === null || transformOptions === void 0
170 ? void 0
171 : transformOptions.supportsStaticESM) !== null &&
172 _transformOptions$sup2 !== void 0
173 ? _transformOptions$sup2
174 : options.caller.supportsStaticESM
175 },
176 filename
177 });
178
179 if (!babelConfig) {
180 throw new Error(
181 `babel-jest: Babel ignores ${_chalk().default.bold(
182 (0, _slash().default)(path().relative(cwd, filename))
183 )} - make sure to include the file in Jest's ${_chalk().default.bold(
184 'transformIgnorePatterns'
185 )} as well.`
186 );
187 }
188
189 return babelConfig;
190 }
191
192 return {
193 canInstrument: true,
194
195 getCacheKey(fileData, filename, configString, cacheKeyOptions) {
196 const {config, instrument, rootDir} = cacheKeyOptions;
197 const babelOptions = loadBabelConfig(
198 config.cwd,
199 filename,
200 cacheKeyOptions
201 );
202 const configPath = [
203 babelOptions.config || '',
204 babelOptions.babelrc || ''
205 ];
206 return (0, _crypto().createHash)('md5')
207 .update(THIS_FILE)
208 .update('\0', 'utf8')
209 .update(JSON.stringify(babelOptions.options))
210 .update('\0', 'utf8')
211 .update(fileData)
212 .update('\0', 'utf8')
213 .update(path().relative(rootDir, filename))
214 .update('\0', 'utf8')
215 .update(configString)
216 .update('\0', 'utf8')
217 .update(configPath.join(''))
218 .update('\0', 'utf8')
219 .update(instrument ? 'instrument' : '')
220 .update('\0', 'utf8')
221 .update(process.env.NODE_ENV || '')
222 .update('\0', 'utf8')
223 .update(process.env.BABEL_ENV || '')
224 .digest('hex');
225 },
226
227 process(src, filename, config, transformOptions) {
228 const babelOptions = {
229 ...loadBabelConfig(config.cwd, filename, transformOptions).options
230 };
231
232 if (
233 transformOptions === null || transformOptions === void 0
234 ? void 0
235 : transformOptions.instrument
236 ) {
237 babelOptions.auxiliaryCommentBefore = ' istanbul ignore next '; // Copied from jest-runtime transform.js
238
239 babelOptions.plugins = (babelOptions.plugins || []).concat([
240 [
241 babelIstanbulPlugin,
242 {
243 // files outside `cwd` will not be instrumented
244 cwd: config.rootDir,
245 exclude: []
246 }
247 ]
248 ]);
249 }
250
251 const transformResult = (0, _core().transformSync)(src, babelOptions);
252
253 if (transformResult) {
254 const {code, map} = transformResult;
255
256 if (typeof code === 'string') {
257 return {
258 code,
259 map
260 };
261 }
262 }
263
264 return src;
265 }
266 };
267};
268
269const transformer = {
270 ...createTransformer(),
271 // Assigned here so only the exported transformer has `createTransformer`,
272 // instead of all created transformers by the function
273 createTransformer
274};
275module.exports = transformer;