Skip to content

Commit 9baa83f

Browse files
authored
Merge pull request #227 from gedu/gedu/text_decoding_update
2 parents f329418 + 0bb23d9 commit 9baa83f

File tree

6 files changed

+96
-7
lines changed

6 files changed

+96
-7
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,46 @@ Read more details in the dedicated [README](/Example/README.md).
165165
### Dependencies
166166

167167
* [node-qrcode](https://github.com/soldair/node-qrcode)
168+
169+
### Integrating react-native-qrcode-svg with React Native versions below 0.75
170+
This library works seamlessly with React Native versions 0.75 and above. However, if you're using an older version of React Native (below 0.75), you might need to apply a custom transformation to your project's metro.config.js file to ensure compatibility with the TextEncoder API.
171+
172+
Here's how to configure the transformer for both Expo and React Native projects:
173+
174+
#### Setting Up the Transformer:
175+
Make sure your project has a `metro.config.js` file. If not, create one at the root of your project.
176+
177+
#### Expo Projects:
178+
```js
179+
const { getDefaultConfig } = require("expo/metro-config");
180+
181+
module.exports = (() => {
182+
const config = getDefaultConfig(__dirname);
183+
184+
const { transformer } = config;
185+
186+
config.transformer = {
187+
...transformer,
188+
babelTransformerPath: require.resolve("react-native-qrcode-svg/textEncodingTransformation")
189+
};
190+
191+
return config;
192+
})();
193+
```
194+
Merge the contents from your project's metro.config.js file with this config.
195+
196+
#### React Native Projects:
197+
```js
198+
const { getDefaultConfig, mergeConfig } = require("@react-native/metro-config");
199+
200+
const defaultConfig = getDefaultConfig(__dirname);
201+
202+
const config = {
203+
transformer: {
204+
babelTransformerPath: require.resolve("react-native-qrcode-svg/textEncodingTransformation"),
205+
},
206+
};
207+
208+
module.exports = mergeConfig(defaultConfig, config);
209+
```
210+

babel.config.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
module.exports = {
2-
presets: ["module:metro-react-native-babel-preset"],
2+
presets:
3+
[
4+
"module:metro-react-native-babel-preset",
5+
{
6+
disableImportExportTransform: true,
7+
},
8+
],
39
};

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-qrcode-svg",
3-
"version": "6.3.12",
3+
"version": "6.3.13",
44
"description": "A QR Code generator for React Native based on react-native-svg and javascript-qrcode.",
55
"main": "index.js",
66
"types": "index.d.ts",
@@ -36,7 +36,8 @@
3636
"src",
3737
"babel.config.js",
3838
"index.d.ts",
39-
"index.js"
39+
"index.js",
40+
"textEncodingTransformation.js"
4041
],
4142
"keywords": [
4243
"react-native",

src/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
global.TextEncoder = require("text-encoding").TextEncoder;
2-
31
import React, { useMemo } from "react";
42
import Svg, {
53
Defs,

textEncodingTransformation.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const { readFileSync } = require('fs');
2+
const semver = require('semver');
3+
4+
const fileToTransform = 'node_modules/react-native-qrcode-svg/src/index.js';
5+
6+
const upstreamTransformer = (() => {
7+
try {
8+
return require("@expo/metro-config/babel-transformer");
9+
} catch (error) {
10+
try {
11+
return require("@react-native/metro-babel-transformer");
12+
} catch (error) {
13+
return require("metro-react-native-babel-transformer");
14+
}
15+
}
16+
})();
17+
18+
function createTransformer(transformer) {
19+
return async ({src, filename, ...rest}) => {
20+
if (filename === fileToTransform) {
21+
const packageJsonPath = require.resolve('react-native/package.json');
22+
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
23+
const ReactNativeVersion = packageJson.version;
24+
25+
// React Native versions below 0.75 do not include a global TextEncoder implementation.
26+
// To ensure compatibility with these older versions, we add a polyfill using the 'text-encoding' library.
27+
if (semver.lt(ReactNativeVersion, '0.75.0')) {
28+
return transformer.transform({
29+
src: `global.TextEncoder = require('text-encoding').TextEncoder;\n${src}`,
30+
filename,
31+
...rest
32+
});
33+
}
34+
}
35+
return transformer.transform({src, filename, ...rest});
36+
};
37+
}
38+
39+
module.exports = {
40+
transform: createTransformer(upstreamTransformer),
41+
};

0 commit comments

Comments
 (0)