Code Review

rn-nodeify 이용하기

RabbitOnEarth 2024. 2. 15. 21:43

React Native 프로젝트에서 bitcoinjs-lib 같은 crypto 관련된 라이브러리들을 사용하려면, 그 내부에서 보통 core node module이 사용되기 때문에 바로 안되고 다음과 같은 도구를 추가로 사용해야한다

 

 

rn-nodeify

Run after npm install and you can use node core modules and npm modules that use them in your React Native app.. Latest version: 10.3.0, last published: 3 years ago. Start using rn-nodeify in your project by running `npm i rn-nodeify`. There are 16 other p

www.npmjs.com

 

사용법이나 예제는 readme에 잘 나와있고, 어떻게 구현했는지 코드를 약간 살펴보면

 

1. install 시에는 간단히 패키지 내에 있는 shim.js 파일을 복사해서 넣어준다

https://github.com/tradle/rn-nodeify/blob/338d8d6ba8438403093e9409e9a9d88ad884926f/cmd.js#L245-L258

 

2. hack 시 node_modules를 돌면서 package.json 에 browser와 react-native field를 추가하고, pkg-hacks.js 에 정의되어있는 몇가지 module에 대해서는 추가로 코드 수정까지 진행한다

https://github.com/tradle/rn-nodeify/blob/338d8d6ba8438403093e9409e9a9d88ad884926f/cmd.js#L82-L92

https://github.com/tradle/rn-nodeify/blob/338d8d6ba8438403093e9409e9a9d88ad884926f/pkg-hacks.js

 

현재 react-native 진영은 exports support에 대한 계획을 진행중인데 0.73.3 기준으로 여전히 experimental 인데다가, 동작이 완전하지 않은듯하다. 저 계획 및 migration이 완료된다면 rn-nodeify 도 맞춰서 변경해서 사용하거나, 다른 방법을 찾아봐야할 듯 하다.

 

https://reactnative.dev/blog/2023/06/21/package-exports-support

 

Package Exports Support in React Native · React Native

With the release of React Native 0.72, Metro — our JavaScript build tool — now includes beta support for the package.json "exports" field. When enabled, it adds the following functionality:

reactnative.dev