-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
58 lines (47 loc) · 1.75 KB
/
index.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
56
57
58
const gqlLoader = require('graphql-tag/loader');
const semver = require('semver');
let upstreamTransformer = null;
const reactNativeVersionString = require('react-native/package.json').version;
const metroVersion = semver(require('metro/package.json').version).minor;
const reactNativeMinorVersion = semver(reactNativeVersionString).minor;
if (metroVersion >= 51) {
upstreamTransformer = require('metro-react-native-babel-transformer/src/index');
} else if (reactNativeMinorVersion >= 56) {
upstreamTransformer = require('metro/src/reactNativeTransformer');
} else if (reactNativeMinorVersion >= 52) {
upstreamTransformer = require('metro/src/transformer');
} else if (reactNativeMinorVersion >= 0.47) {
upstreamTransformer = require('metro-bundler/src/transformer');
} else if (reactNativeMinorVersion === 0.46) {
upstreamTransformer = require('metro-bundler/build/transformer');
} else {
// handle RN <= 0.45
const oldUpstreamTransformer = require('react-native/packager/transformer');
upstreamTransformer = {
transform({ src, filename, options }) {
return oldUpstreamTransformer.transform(src, filename, options);
},
};
}
const gqlTransform = gqlLoader.bind({
cacheable: () => null,
});
function transform(src, filename, options) {
if (typeof src === 'object') {
// handle RN >= 0.46
({ src, filename, options } = src);
}
// Do custom transformations
let result = src;
if (filename.endsWith('.gql') || filename.endsWith('.graphql')) {
result = gqlTransform(result);
}
const babelCompileResult = upstreamTransformer.transform({
src: result,
filename,
options,
});
// Pass the transformed source to the original react native transformer
return babelCompileResult;
}
module.exports.transform = transform;