1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | function _crypto() {
|
9 | const data = require('crypto');
|
10 |
|
11 | _crypto = function () {
|
12 | return data;
|
13 | };
|
14 |
|
15 | return data;
|
16 | }
|
17 |
|
18 | function path() {
|
19 | const data = _interopRequireWildcard(require('path'));
|
20 |
|
21 | path = function () {
|
22 | return data;
|
23 | };
|
24 |
|
25 | return data;
|
26 | }
|
27 |
|
28 | function _core() {
|
29 | const data = require('@babel/core');
|
30 |
|
31 | _core = function () {
|
32 | return data;
|
33 | };
|
34 |
|
35 | return data;
|
36 | }
|
37 |
|
38 | function _chalk() {
|
39 | const data = _interopRequireDefault(require('chalk'));
|
40 |
|
41 | _chalk = function () {
|
42 | return data;
|
43 | };
|
44 |
|
45 | return data;
|
46 | }
|
47 |
|
48 | function fs() {
|
49 | const data = _interopRequireWildcard(require('graceful-fs'));
|
50 |
|
51 | fs = function () {
|
52 | return data;
|
53 | };
|
54 |
|
55 | return data;
|
56 | }
|
57 |
|
58 | function _slash() {
|
59 | const data = _interopRequireDefault(require('slash'));
|
60 |
|
61 | _slash = function () {
|
62 | return data;
|
63 | };
|
64 |
|
65 | return data;
|
66 | }
|
67 |
|
68 | var _loadBabelConfig = require('./loadBabelConfig');
|
69 |
|
70 | function _interopRequireDefault(obj) {
|
71 | return obj && obj.__esModule ? obj : {default: obj};
|
72 | }
|
73 |
|
74 | function _getRequireWildcardCache(nodeInterop) {
|
75 | if (typeof WeakMap !== 'function') return null;
|
76 | var cacheBabelInterop = new WeakMap();
|
77 | var cacheNodeInterop = new WeakMap();
|
78 | return (_getRequireWildcardCache = function (nodeInterop) {
|
79 | return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
80 | })(nodeInterop);
|
81 | }
|
82 |
|
83 | function _interopRequireWildcard(obj, nodeInterop) {
|
84 | if (!nodeInterop && obj && obj.__esModule) {
|
85 | return obj;
|
86 | }
|
87 | if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
|
88 | return {default: obj};
|
89 | }
|
90 | var cache = _getRequireWildcardCache(nodeInterop);
|
91 | if (cache && cache.has(obj)) {
|
92 | return cache.get(obj);
|
93 | }
|
94 | var newObj = {};
|
95 | var hasPropertyDescriptor =
|
96 | Object.defineProperty && Object.getOwnPropertyDescriptor;
|
97 | for (var key in obj) {
|
98 | if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
|
99 | var desc = hasPropertyDescriptor
|
100 | ? Object.getOwnPropertyDescriptor(obj, key)
|
101 | : null;
|
102 | if (desc && (desc.get || desc.set)) {
|
103 | Object.defineProperty(newObj, key, desc);
|
104 | } else {
|
105 | newObj[key] = obj[key];
|
106 | }
|
107 | }
|
108 | }
|
109 | newObj.default = obj;
|
110 | if (cache) {
|
111 | cache.set(obj, newObj);
|
112 | }
|
113 | return newObj;
|
114 | }
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | const THIS_FILE = fs().readFileSync(__filename);
|
123 |
|
124 | const jestPresetPath = require.resolve('babel-preset-jest');
|
125 |
|
126 | const babelIstanbulPlugin = require.resolve('babel-plugin-istanbul');
|
127 |
|
128 | function assertLoadedBabelConfig(babelConfig, cwd, filename) {
|
129 | if (!babelConfig) {
|
130 | throw new Error(
|
131 | `babel-jest: Babel ignores ${_chalk().default.bold(
|
132 | (0, _slash().default)(path().relative(cwd, filename))
|
133 | )} - make sure to include the file in Jest's ${_chalk().default.bold(
|
134 | 'transformIgnorePatterns'
|
135 | )} as well.`
|
136 | );
|
137 | }
|
138 | }
|
139 |
|
140 | function addIstanbulInstrumentation(babelOptions, transformOptions) {
|
141 | if (transformOptions.instrument) {
|
142 | const copiedBabelOptions = {...babelOptions};
|
143 | copiedBabelOptions.auxiliaryCommentBefore = ' istanbul ignore next ';
|
144 |
|
145 | copiedBabelOptions.plugins = (copiedBabelOptions.plugins || []).concat([
|
146 | [
|
147 | babelIstanbulPlugin,
|
148 | {
|
149 |
|
150 | cwd: transformOptions.config.cwd,
|
151 | exclude: []
|
152 | }
|
153 | ]
|
154 | ]);
|
155 | return copiedBabelOptions;
|
156 | }
|
157 |
|
158 | return babelOptions;
|
159 | }
|
160 |
|
161 | function getCacheKeyFromConfig(
|
162 | sourceText,
|
163 | sourcePath,
|
164 | babelOptions,
|
165 | transformOptions
|
166 | ) {
|
167 | const {config, configString, instrument} = transformOptions;
|
168 | const configPath = [babelOptions.config || '', babelOptions.babelrc || ''];
|
169 | return (0, _crypto().createHash)('md5')
|
170 | .update(THIS_FILE)
|
171 | .update('\0', 'utf8')
|
172 | .update(JSON.stringify(babelOptions.options))
|
173 | .update('\0', 'utf8')
|
174 | .update(sourceText)
|
175 | .update('\0', 'utf8')
|
176 | .update(path().relative(config.rootDir, sourcePath))
|
177 | .update('\0', 'utf8')
|
178 | .update(configString)
|
179 | .update('\0', 'utf8')
|
180 | .update(configPath.join(''))
|
181 | .update('\0', 'utf8')
|
182 | .update(instrument ? 'instrument' : '')
|
183 | .update('\0', 'utf8')
|
184 | .update(process.env.NODE_ENV || '')
|
185 | .update('\0', 'utf8')
|
186 | .update(process.env.BABEL_ENV || '')
|
187 | .digest('hex');
|
188 | }
|
189 |
|
190 | function loadBabelConfig(cwd, filename, transformOptions) {
|
191 | const babelConfig = (0, _loadBabelConfig.loadPartialConfig)(transformOptions);
|
192 | assertLoadedBabelConfig(babelConfig, cwd, filename);
|
193 | return babelConfig;
|
194 | }
|
195 |
|
196 | async function loadBabelConfigAsync(cwd, filename, transformOptions) {
|
197 | const babelConfig = await (0, _loadBabelConfig.loadPartialConfigAsync)(
|
198 | transformOptions
|
199 | );
|
200 | assertLoadedBabelConfig(babelConfig, cwd, filename);
|
201 | return babelConfig;
|
202 | }
|
203 |
|
204 | function loadBabelOptions(
|
205 | cwd,
|
206 | filename,
|
207 | transformOptions,
|
208 | jestTransformOptions
|
209 | ) {
|
210 | const {options} = loadBabelConfig(cwd, filename, transformOptions);
|
211 | return addIstanbulInstrumentation(options, jestTransformOptions);
|
212 | }
|
213 |
|
214 | async function loadBabelOptionsAsync(
|
215 | cwd,
|
216 | filename,
|
217 | transformOptions,
|
218 | jestTransformOptions
|
219 | ) {
|
220 | const {options} = await loadBabelConfigAsync(cwd, filename, transformOptions);
|
221 | return addIstanbulInstrumentation(options, jestTransformOptions);
|
222 | }
|
223 |
|
224 | const createTransformer = userOptions => {
|
225 | var _inputOptions$plugins, _inputOptions$presets;
|
226 |
|
227 | const inputOptions =
|
228 | userOptions !== null && userOptions !== void 0 ? userOptions : {};
|
229 | const options = {
|
230 | ...inputOptions,
|
231 | caller: {
|
232 | name: 'babel-jest',
|
233 | supportsDynamicImport: false,
|
234 | supportsExportNamespaceFrom: false,
|
235 | supportsStaticESM: false,
|
236 | supportsTopLevelAwait: false,
|
237 | ...inputOptions.caller
|
238 | },
|
239 | compact: false,
|
240 | plugins:
|
241 | (_inputOptions$plugins = inputOptions.plugins) !== null &&
|
242 | _inputOptions$plugins !== void 0
|
243 | ? _inputOptions$plugins
|
244 | : [],
|
245 | presets: ((_inputOptions$presets = inputOptions.presets) !== null &&
|
246 | _inputOptions$presets !== void 0
|
247 | ? _inputOptions$presets
|
248 | : []
|
249 | ).concat(jestPresetPath),
|
250 | sourceMaps: 'both'
|
251 | };
|
252 |
|
253 | function mergeBabelTransformOptions(filename, transformOptions) {
|
254 | var _transformOptions$sup,
|
255 | _transformOptions$sup2,
|
256 | _transformOptions$sup3,
|
257 | _transformOptions$sup4;
|
258 |
|
259 | const {cwd} = transformOptions.config;
|
260 |
|
261 | return {
|
262 | cwd,
|
263 | ...options,
|
264 | caller: {
|
265 | ...options.caller,
|
266 | supportsDynamicImport:
|
267 | (_transformOptions$sup = transformOptions.supportsDynamicImport) !==
|
268 | null && _transformOptions$sup !== void 0
|
269 | ? _transformOptions$sup
|
270 | : options.caller.supportsDynamicImport,
|
271 | supportsExportNamespaceFrom:
|
272 | (_transformOptions$sup2 =
|
273 | transformOptions.supportsExportNamespaceFrom) !== null &&
|
274 | _transformOptions$sup2 !== void 0
|
275 | ? _transformOptions$sup2
|
276 | : options.caller.supportsExportNamespaceFrom,
|
277 | supportsStaticESM:
|
278 | (_transformOptions$sup3 = transformOptions.supportsStaticESM) !==
|
279 | null && _transformOptions$sup3 !== void 0
|
280 | ? _transformOptions$sup3
|
281 | : options.caller.supportsStaticESM,
|
282 | supportsTopLevelAwait:
|
283 | (_transformOptions$sup4 = transformOptions.supportsTopLevelAwait) !==
|
284 | null && _transformOptions$sup4 !== void 0
|
285 | ? _transformOptions$sup4
|
286 | : options.caller.supportsTopLevelAwait
|
287 | },
|
288 | filename
|
289 | };
|
290 | }
|
291 |
|
292 | return {
|
293 | canInstrument: true,
|
294 |
|
295 | getCacheKey(sourceText, sourcePath, transformOptions) {
|
296 | const babelOptions = loadBabelConfig(
|
297 | transformOptions.config.cwd,
|
298 | sourcePath,
|
299 | mergeBabelTransformOptions(sourcePath, transformOptions)
|
300 | );
|
301 | return getCacheKeyFromConfig(
|
302 | sourceText,
|
303 | sourcePath,
|
304 | babelOptions,
|
305 | transformOptions
|
306 | );
|
307 | },
|
308 |
|
309 | async getCacheKeyAsync(sourceText, sourcePath, transformOptions) {
|
310 | const babelOptions = await loadBabelConfigAsync(
|
311 | transformOptions.config.cwd,
|
312 | sourcePath,
|
313 | mergeBabelTransformOptions(sourcePath, transformOptions)
|
314 | );
|
315 | return getCacheKeyFromConfig(
|
316 | sourceText,
|
317 | sourcePath,
|
318 | babelOptions,
|
319 | transformOptions
|
320 | );
|
321 | },
|
322 |
|
323 | process(sourceText, sourcePath, transformOptions) {
|
324 | const babelOptions = loadBabelOptions(
|
325 | transformOptions.config.cwd,
|
326 | sourcePath,
|
327 | mergeBabelTransformOptions(sourcePath, transformOptions),
|
328 | transformOptions
|
329 | );
|
330 | const transformResult = (0, _core().transformSync)(
|
331 | sourceText,
|
332 | babelOptions
|
333 | );
|
334 |
|
335 | if (transformResult) {
|
336 | const {code, map} = transformResult;
|
337 |
|
338 | if (typeof code === 'string') {
|
339 | return {
|
340 | code,
|
341 | map
|
342 | };
|
343 | }
|
344 | }
|
345 |
|
346 | return sourceText;
|
347 | },
|
348 |
|
349 | async processAsync(sourceText, sourcePath, transformOptions) {
|
350 | const babelOptions = await loadBabelOptionsAsync(
|
351 | transformOptions.config.cwd,
|
352 | sourcePath,
|
353 | mergeBabelTransformOptions(sourcePath, transformOptions),
|
354 | transformOptions
|
355 | );
|
356 | const transformResult = await (0, _core().transformAsync)(
|
357 | sourceText,
|
358 | babelOptions
|
359 | );
|
360 |
|
361 | if (transformResult) {
|
362 | const {code, map} = transformResult;
|
363 |
|
364 | if (typeof code === 'string') {
|
365 | return {
|
366 | code,
|
367 | map
|
368 | };
|
369 | }
|
370 | }
|
371 |
|
372 | return sourceText;
|
373 | }
|
374 | };
|
375 | };
|
376 |
|
377 | const transformer = {
|
378 | ...createTransformer(),
|
379 |
|
380 |
|
381 | createTransformer
|
382 | };
|
383 | var _default = transformer;
|
384 | exports.default = _default;
|