Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed the text-encondig code line #227

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

gedu
Copy link

@gedu gedu commented Dec 20, 2024

Details

Adds a custom transformer to support React Native versions below 0.75, which require the TextEncoder polyfill. The transformer modifies the src/index.js file of the library to include the necessary polyfill.

What this fixes

This PR addresses an issue where the text-encoding library was being included unnecessarily in the bundle for React Native versions 0.75 and above. By conditionally adding the polyfill only when needed, this change reduces the bundle size and improves performance.

Checklist

  • I have described the bug/issue
  • I have provided reproduction in Example App
  • I have tested that solution works on Example App on all platforms:
    • Android
    • iOS
    • Web

Screenshots/Videos

Screenshot 2024-12-20 at 17 59 26

Copy link

github-actions bot commented Dec 20, 2024

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@gedu
Copy link
Author

gedu commented Dec 20, 2024

I have read the CLA Document and I hereby sign the CLA

CLABotify added a commit to Expensify/CLA that referenced this pull request Dec 20, 2024
Make sure your project has a `metro.config.js` file. If not, create one at the root of your project.

```js
const { getDefaultConfig } = require("expo/metro-config");
Copy link

@szymonrybczak szymonrybczak Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here there's usage of expo/metro-config inside next code snippet there's @react-native/metro-config which can be confusing, let's say explicitly that the first one is for Expo projects and second is for Bare React Native

README.md Outdated
return config;
})();
```
Merge the contents from your project's metro.config.js file with this config (create the file if it does not exist already).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Merge the contents from your project's metro.config.js file with this config (create the file if it does not exist already).
Merge the contents from your project's metro.config.js file with this config.

For Bare React Native it's inside a default template :)

README.md Outdated
Comment on lines 199 to 201

const { assetExts, sourceExts } = defaultConfig.resolver;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const { assetExts, sourceExts } = defaultConfig.resolver;
const { assetExts, sourceExts } = defaultConfig.resolver;

useless?

Copy link

@szymonrybczak szymonrybczak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, left few nits comments


// 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 (major === '0' && minor < '75') {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if React Native will be 1.0? 🙈 maybe we can leverage semver package here since we're in Node.js runtime

Suggested change
if (major === '0' && minor < '75') {
if (semver.lt(version, '0.75.0')) {


module.exports = {
transform: createTransformer(upstreamTransformer),
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: missing new line

@AbdulrhmnGhanem
Copy link

AbdulrhmnGhanem commented Dec 21, 2024

Wouldn't telling the users to install text-encoding and adding to the globals if they are on RN < 0.75 be simpler? I don't see the added value of the custom transformation, am I missing something?
If I am a first time user, IMHO I would look for something with less overhead.

@gedu
Copy link
Author

gedu commented Dec 23, 2024

@AbdulrhmnGhanem Thanks for the feedback! I agree that installing a library is simpler, and in mine, users need to tweak their Metro config if they're on RN < 0.75, meaning that it requires a bit of knowledge of it, and everything happens at build time.
This avoids runtime checks and the potential issues with useEffect, like crashes if a component runs before the polyfill is loaded.
@mountiny Happy to hear your thoughts!

@AbdulrhmnGhanem
Copy link

Thanks for taking the time to respond!

My initial PR is flawed too. I don't think we need an effect if we are going to say it explicitly in the docs:

If you are on RN < 0.75 install the text-encoding package and add the following two lines to import the package

import QRCode from 'react-native-qrcode-svg'

global.TextEncoder = require("text-encoding").TextEncoder;

@mountiny
Copy link

@gedu I am not sure if I understand the build considerations in our case, we are on 0.75 and we will switch to 0.76. We can note this in the Readme, but we are not sure if we need to worry about it from the App perspective. Am I missing something?

@mountiny mountiny requested a review from thesahindia December 23, 2024 14:42
@gedu
Copy link
Author

gedu commented Dec 23, 2024

On Exfy all should be ok

@mountiny mountiny requested review from parasharrajat and removed request for thesahindia December 23, 2024 15:14
package.json Outdated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update lock file as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for the example app.

@parasharrajat
Copy link
Member

parasharrajat commented Dec 23, 2024

Screenshots

🔲 iOS / native

23.12.2024_23.26.08_REC.mp4

🔲 MacOS / Chrome

23.12.2024_21.41.46_REC.mp4

🔲 Android / native

23.12.2024_21.54.39_REC.mp4

@parasharrajat
Copy link
Member

@gedu There are small requests but the changes looks good to me.

@gedu
Copy link
Author

gedu commented Dec 23, 2024

@parasharrajat I will review the npm version, Probably on a follow-up PR we can increase the RN version on the Example app.

@parasharrajat
Copy link
Member

@gedu Ok, No problem. But we can atleast fix the lock files as they do not match the package.json files. I see changes on npm install and yarn install for both the main pkg and example app.

@gedu
Copy link
Author

gedu commented Dec 24, 2024

@parasharrajat I Updated the package-lock.json from root, on the Example app, what should I update? I didn't change dependencies there.

@gedu
Copy link
Author

gedu commented Dec 24, 2024

On Exfy is working fine for 0.75

qrcode-svg-exfy.mp4

@parasharrajat
Copy link
Member

parasharrajat commented Dec 24, 2024

@gedu Looks like svg version is mismatched on yarn.lock file on Example app. But this is a no blocker for me.

image

Copy link
Member

@parasharrajat parasharrajat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎀 👀 🎀 C+ reviewed

@parasharrajat
Copy link
Member

All yours @mountiny

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants