-
Notifications
You must be signed in to change notification settings - Fork 219
/
textEncodingTransformation.js
41 lines (36 loc) · 1.42 KB
/
textEncodingTransformation.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
const { readFileSync } = require('fs');
const semver = require('semver');
const fileToTransform = 'node_modules/react-native-qrcode-svg/src/index.js';
const upstreamTransformer = (() => {
try {
return require("@expo/metro-config/babel-transformer");
} catch (error) {
try {
return require("@react-native/metro-babel-transformer");
} catch (error) {
return require("metro-react-native-babel-transformer");
}
}
})();
function createTransformer(transformer) {
return async ({src, filename, ...rest}) => {
if (filename === fileToTransform) {
const packageJsonPath = require.resolve('react-native/package.json');
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
const ReactNativeVersion = packageJson.version;
// React Native versions below 0.75 do not include a global TextEncoder implementation.
// To ensure compatibility with these older versions, we add a polyfill using the 'text-encoding' library.
if (semver.lt(ReactNativeVersion, '0.75.0')) {
return transformer.transform({
src: `global.TextEncoder = require('text-encoding').TextEncoder;\n${src}`,
filename,
...rest
});
}
}
return transformer.transform({src, filename, ...rest});
};
}
module.exports = {
transform: createTransformer(upstreamTransformer),
};