philikon / ReactNativify

How to use node.js libraries in React Native
242 stars 23 forks source link

Possible cleaner solution (but must clone react-native) #2

Closed aleclarson closed 7 years ago

aleclarson commented 7 years ago

If you install any polyfills into the react-native package, you can avoid overriding the transformer.

#1. Clone react-native
git clone https://github.com/facebook/react-native.git
cd react-native

#2. Install the polyfill
npm install --save path-browserify

#3. Create a haste module for the polyfill
mkdir Libraries/NodeJS
touch Libraries/NodeJS/path.js

Set the contents of Libraries/NodeJS/path.js to:

/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 *
 * @providesModule path
 */

module.exports = require('../../node_modules/path-browserify');

WDYT? Is this not preferable?

philikon commented 7 years ago

Thanks for the input! Sorry for getting back so late, I totally didn't see this.

module.exports = require('../../node_modules/path-browserify');

Why not module.exports = require('path-browserify');?

WDYT? Is this not preferable?

You'd have to create a Haste module per node built-in module. I guess that's not a big deal, could easily automate it. You don't even have to clone react-native from git, you could just patch it inside your node_modules/ folder.

In the end, the ReactNativify approach is pretty lightweight. A small babel transform, an additional transform.js file, and a rn-cli.config.js file. No patches necessary, no extra files you need to ensure are always there, plus slightly smaller generated code. And if React Native ever gives up Haste, at least for the Open Source port, this approach will continue to work.

aleclarson commented 7 years ago

@philikon Interesting stuff, thanks!