UNPKG

5.26 kBJavaScriptView Raw
1'use strict';
2
3function _crypto() {
4 const data = _interopRequireDefault(require('crypto'));
5
6 _crypto = function _crypto() {
7 return data;
8 };
9
10 return data;
11}
12
13function _fs() {
14 const data = _interopRequireDefault(require('fs'));
15
16 _fs = function _fs() {
17 return data;
18 };
19
20 return data;
21}
22
23function _path() {
24 const data = _interopRequireDefault(require('path'));
25
26 _path = function _path() {
27 return data;
28 };
29
30 return data;
31}
32
33function _core() {
34 const data = require('@babel/core');
35
36 _core = function _core() {
37 return data;
38 };
39
40 return data;
41}
42
43function _chalk() {
44 const data = _interopRequireDefault(require('chalk'));
45
46 _chalk = function _chalk() {
47 return data;
48 };
49
50 return data;
51}
52
53function _slash() {
54 const data = _interopRequireDefault(require('slash'));
55
56 _slash = function _slash() {
57 return data;
58 };
59
60 return data;
61}
62
63function _interopRequireDefault(obj) {
64 return obj && obj.__esModule ? obj : {default: obj};
65}
66
67function _objectSpread(target) {
68 for (var i = 1; i < arguments.length; i++) {
69 var source = arguments[i] != null ? arguments[i] : {};
70 var ownKeys = Object.keys(source);
71 if (typeof Object.getOwnPropertySymbols === 'function') {
72 ownKeys = ownKeys.concat(
73 Object.getOwnPropertySymbols(source).filter(function(sym) {
74 return Object.getOwnPropertyDescriptor(source, sym).enumerable;
75 })
76 );
77 }
78 ownKeys.forEach(function(key) {
79 _defineProperty(target, key, source[key]);
80 });
81 }
82 return target;
83}
84
85function _defineProperty(obj, key, value) {
86 if (key in obj) {
87 Object.defineProperty(obj, key, {
88 value: value,
89 enumerable: true,
90 configurable: true,
91 writable: true
92 });
93 } else {
94 obj[key] = value;
95 }
96 return obj;
97}
98
99const THIS_FILE = _fs().default.readFileSync(__filename);
100
101const jestPresetPath = require.resolve('babel-preset-jest');
102
103const babelIstanbulPlugin = require.resolve('babel-plugin-istanbul'); // Narrow down the types
104
105const createTransformer = (options = {}) => {
106 options = _objectSpread({}, options, {
107 caller: {
108 name: 'babel-jest',
109 supportsStaticESM: false
110 },
111 compact: false,
112 plugins: (options && options.plugins) || [],
113 presets: ((options && options.presets) || []).concat(jestPresetPath),
114 sourceMaps: 'both'
115 });
116
117 function loadBabelConfig(cwd, filename) {
118 // `cwd` first to allow incoming options to override it
119 const babelConfig = (0, _core().loadPartialConfig)(
120 _objectSpread(
121 {
122 cwd
123 },
124 options,
125 {
126 filename
127 }
128 )
129 );
130
131 if (!babelConfig) {
132 throw new Error(
133 `babel-jest: Babel ignores ${_chalk().default.bold(
134 (0, _slash().default)(_path().default.relative(cwd, filename))
135 )} - make sure to include the file in Jest's ${_chalk().default.bold(
136 'transformIgnorePatterns'
137 )} as well.`
138 );
139 }
140
141 return babelConfig;
142 }
143
144 return {
145 canInstrument: true,
146
147 getCacheKey(
148 fileData,
149 filename,
150 configString,
151 {config, instrument, rootDir}
152 ) {
153 const babelOptions = loadBabelConfig(config.cwd, filename);
154 const configPath = [
155 babelOptions.config || '',
156 babelOptions.babelrc || ''
157 ];
158 return _crypto()
159 .default.createHash('md5')
160 .update(THIS_FILE)
161 .update('\0', 'utf8')
162 .update(JSON.stringify(babelOptions.options))
163 .update('\0', 'utf8')
164 .update(fileData)
165 .update('\0', 'utf8')
166 .update(_path().default.relative(rootDir, filename))
167 .update('\0', 'utf8')
168 .update(configString)
169 .update('\0', 'utf8')
170 .update(configPath.join(''))
171 .update('\0', 'utf8')
172 .update(instrument ? 'instrument' : '')
173 .update('\0', 'utf8')
174 .update(process.env.NODE_ENV || '')
175 .update('\0', 'utf8')
176 .update(process.env.BABEL_ENV || '')
177 .digest('hex');
178 },
179
180 process(src, filename, config, transformOptions) {
181 const babelOptions = _objectSpread(
182 {},
183 loadBabelConfig(config.cwd, filename).options
184 );
185
186 if (transformOptions && transformOptions.instrument) {
187 babelOptions.auxiliaryCommentBefore = ' istanbul ignore next '; // Copied from jest-runtime transform.js
188
189 babelOptions.plugins = (babelOptions.plugins || []).concat([
190 [
191 babelIstanbulPlugin,
192 {
193 // files outside `cwd` will not be instrumented
194 cwd: config.rootDir,
195 exclude: []
196 }
197 ]
198 ]);
199 }
200
201 const transformResult = (0, _core().transformSync)(src, babelOptions);
202
203 if (transformResult) {
204 const code = transformResult.code,
205 map = transformResult.map;
206
207 if (typeof code === 'string') {
208 return {
209 code,
210 map
211 };
212 }
213 }
214
215 return src;
216 }
217 };
218};
219
220const transformer = _objectSpread({}, createTransformer(), {
221 // Assigned here so only the exported transformer has `createTransformer`,
222 // instead of all created transformers by the function
223 createTransformer
224});
225
226module.exports = transformer;