This repository has been archived by the owner on May 25, 2021. It is now read-only.
forked from facebook/fbt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jest-preprocessor.js
55 lines (50 loc) · 1.56 KB
/
jest-preprocessor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* Copyright 2004-present Facebook. All Rights Reserved.
*
* @format
* @noflow
*/
const babel = require('@babel/core');
const createCacheKeyFunction = require('fbjs-scripts/jest/createCacheKeyFunction');
const path = require('path');
const cacheKeyPackages = [
'babel-preset-fbjs',
'babel-plugin-fbt',
'babel-plugin-fbt-runtime',
].map(name =>
path.join(
path.dirname(
// Find the actual module root from the package.json file,
// otherwise, the result may be incorrect if a custom "main" path was set.
// See https://stackoverflow.com/a/49455609/104598
require.resolve(path.join(name, 'package.json')),
),
'package.json',
),
);
// This is basically fbjs-scripts/jest/preprocessor, but with the
// ability to specify additional plugins
function createTransformer(opts /*: Object */ = {}) {
return {
process(src /*: string */, filename /*: string */) {
const options = {
presets: [
['@babel/react', {throwIfNamespace: false}],
require('babel-preset-fbjs'),
],
plugins: opts.plugins || [],
filename,
retainLines: true,
};
return babel.transform(src, options).code;
},
// Generate a cache key that is based on the contents of this file,
// babel-preset-fbjs, and the plugins passed in as options and fbjs (used as
// a proxy for determining if the preset has changed configuration at all).
getCacheKey: createCacheKeyFunction([__filename].concat(cacheKeyPackages)),
};
}
module.exports = {
...createTransformer(),
createTransformer,
};