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

React Native Packager - Module replacement? #2

Open
jacobrosenthal opened this issue Sep 14, 2015 · 10 comments
Open

React Native Packager - Module replacement? #2

jacobrosenthal opened this issue Sep 14, 2015 · 10 comments

Comments

@jacobrosenthal
Copy link
Owner

Im not sure how/if module replacement is supposed to work in react packager. Or maybe we need to look into https://github.com/mjohnston/react-native-webpack-server ?

For now Im (sadly) editing files manually

//var noble = require('noble');
var noble = require('react-native-ble');

But its a deep rabbit hole. The eddystone-url-encoding module does a json require without specifying the extension which the react native packager doesnt seem to support

// var prefixes = require('./prefixes');
var prefixes = require('./prefixes.json');
// var suffixes = require('./suffixes');
var suffixes = require('./suffixes.json');
@jacobrosenthal
Copy link
Owner Author

Im not sure why, but utilizing the react-native-webpack-server's example this works properly, but not when with a react native generated template and following their readme. Tracking here mjohnston/react-native-webpack-server#75

For now, Ive added an example based on their example thats working great.

@jacobrosenthal
Copy link
Owner Author

react-native-webpack-server seems poorly supported now, and react is holding steadfast on their packager.
Ive killed the react-native-webpack-server in the example for now, which means no aliasing again.
facebook/react-native#5917 and https://productpains.com/post/react-native/packager-support-resolvealias-ala-webpack/ need your upvotes.

@jacobrosenthal
Copy link
Owner Author

This was solved (I dont know what release) by using the default packager and the browser field in package.json

  "browser": {
    "noble": "react-native-ble"
  },

@jacobrosenthal
Copy link
Owner Author

Current packager only alias's one level deep it would seem. So we can use var noble = require('noble') in our main file, but if we var Bean = require('ble-bean'); which does its own noble require, it fails out because its trying to us actual noble :(

@jacobrosenthal
Copy link
Owner Author

jacobrosenthal commented Apr 28, 2016

Here is the current issue that is asking for global aliasing. Please bump
facebook/react-native#6253

@jacobrosenthal
Copy link
Owner Author

I have a PR in with preliminary deep shim support facebook/react-native#9899 PRs and help wanted.

Also it seems like theres a lot of movement around rn-nodeify, a nifty hack rewriting files locally in order to get around fixing the react native packager. Ive favored this solution in the readmes for now while I continue to work on my solution

@jacobrosenthal
Copy link
Owner Author

@jacobrosenthal
Copy link
Owner Author

"The React Native Packager has a built-in option, extraNodeModules, which seems to act as a module alias mapping. I couldn't find much information about this option, but here's the PR where it was added: facebookarchive/node-haste#69. Here's my rn-cli.config.js:"
philikon/ReactNativify#4

@jacobrosenthal
Copy link
Owner Author

jacobrosenthal commented Sep 25, 2018

Another day another set of options. I found https://github.com/tleunen/babel-plugin-module-resolver which should allow something like

{
	'presets': ['module:metro-react-native-babel-preset'],
  	'plugins': [
    ['module-resolver', {
	    'extensions': ['.ios.js', '.android.js', '.js', '.json'],
	    'alias': {
	      'noble': 'react-native-ble'
	    }
    }]
  ]
}

However in practice it also recursively rewrites react-native-ble itself so the noble/with-bindings becomes react-native-ble/with-bindings ...
It doesnt have an includes or excludes like say webpack tleunen/babel-plugin-module-resolver#301
but the regex or function
https://github.com/tleunen/babel-plugin-module-resolver/blob/master/DOCS.md#regular-expressions or
https://github.com/tleunen/babel-plugin-module-resolver/blob/master/DOCS.md#passing-a-substitute-function might be able to be hacked somehow?

Otherwise @phated has a concept for using the metro field in package.json here

  "metro": {
    "resolver": {
      "resolverMainFields": [
        "react-native",
        "browser",
        "main"
      ]
    }
  }

Then pushing these bindings directly inside of noble and then adding to nobles package.json

  "react-native": {
    "./lib/resolve-bindings.js": "./lib/resolve-bindings"
  },

which could swap in resolve-bindings.ios.js or resolve-bindings.android.js if we move all this code upstream. Though noble needs cross platform code changes swapping events/ and inherits instead of util and events

@jacobrosenthal
Copy link
Owner Author

jacobrosenthal commented Sep 26, 2018

Oh odd.. you actually dont need any of the metro/react-native key stuff in either project, itll automatically pick up the bindings because noble doesnt specify an extension here
https://github.com/noble/noble/blob/c4cd18a7a429bb832f6c4ca793be3faf9af884e9/index.js#L2

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

No branches or pull requests

1 participant