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