-
Notifications
You must be signed in to change notification settings - Fork 24.5k
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
[Packager] Conditional require
with RN Packager
#3463
Comments
BTW, webpack doesn't have any special graceful handling for this. webpack resolves all requirements statically - the reason this doesn't cause issues in webpack is that node-libs-browser provides a IMO the correct way to handle this would be to add another field in This would allow doing for "react-native": {
"domain": false,
"whatwg-fetch": false
} for example. |
cc @ide who I feel like has shared opinions in various places about the
|
The "react-native" field in package.json seems reasonable to me and there's precedent with the "browser" field. The important property is that the packager can statically determine what to put in the JS bundle(s). What we don't want are conditional let Promise = (navigator.product === 'ReactNative') ? require('promise') : global.Promise; The problem here is that |
👍 |
@vjeux you had some objections to the |
@skevy would be nice if you could lay out the plan that we chatted about so that we can refer to it and have someone implement it :) |
Yep. So, one thing we could do is make the packager aware of the In order to handle dependencies like "domain", which is what's causing the particular problem I posted about, webpack uses this: https://github.com/webpack/node-libs-browser. @vjeux mentioned he doesn't really want to maintain a list like this, which I get, but it would be the easiest way for a user to not have to worry about certain weird requires. Honestly, I think the right way forward on this is both something like node-libs-browser for RN that we could build into the packager as well as the ability to override certain packages using |
Rather than bake the mappings into the packager, I'd much prefer the packager to support a way for people to provide their own mappings. This way we don't need to maintain a list of mappings, stave off PRs modifying the packager to change a mapping, and afford users the choice of mappings. I expect a few people will write blog posts or wikis on the mappings that work for them and get the word out that way. I do want to keep the semantics of |
I want there to be a way for people to provide their own mappings. Huge proponent of that. But in this case, FBJS (which will be used by RN at some point, presumably during the React 0.14 migration) uses
in their Idk...I hear what you're saying...I just worry about the ease of use of the pattern. For Relay for instance, people should be able to |
What do you mean "bake the mappings into the packager"? The In principle something like this could be annoying to manage for shared libraries, but I think in practice for managing e.g. |
@taion my sentiment is that packager.js shouldn't hardcode a mapping from "os" to "os-browserify", for example. The mappings should live in the package.json files of react-native, fbjs, and any other npm package and be handled the same way. @skevy thanks for the clarification - I think we're on the same page. |
Alright so I guess we should work on a "react-native" package.json directive PR. Shouldn't be too hard. Guess I'll add it to my ever growing list. But if anyone else wants to tackle it, go for it! 👍 |
I think we're all on the same page, then. Per The only other question is whether it'd make sense to use |
There's a bunch of unnecessary stuff in node-libs-browser. On several occasions I've repeatedly come to the conclusion that making Node libs work in a client environment is less useful than it sounds i.e. I don't think it's directionally correct for the RN project to endorse node-libs-browser. Pulling in polyfills as needed is a better approach for professional-quality projects. |
Oh - someone already implemented this: #2208 What are the chances we could move that onto somebody's radar for review? |
also cc @mvayngrib, the author of #2208 |
@ide I disagree that enabling a Node-like environment in RN is not useful. The Node community is huge, prolific, and this is one way of engaging them more in RN. The browserify project did a similar thing to great effect. That said, node-libs-browser would undoubtedly need to be tweaked to something that works in RN. I currently recreate a Node env in RN with a few shims for core modules (react-native-level-fs, react-native-udp, asyncstorage-down), plus some hacks around packager, and it immediately lets me use existing node libs from the level ecosystem, streaming, networking, bittorrent, blockchain, etc, which don't work otherwise. People could be creating Popcorn Time or the like with RN, and this is holding them back. So personally, I'd love to see Node in RN! |
Things referenced in this issue are closed and merged. Closing. |
The packager's dependency resolver doesn't respect conditional requires, and now, due to 793c356, these errors fail loudly.
As an example of this problem, in libs that use FBJS's promise polyfill, there is a deep dep on
asap
, which has this line: https://github.com/kriskowal/asap/blob/master/raw.js#L81The packager fails hard on this (even though the require is conditional and not applicable here), with an error that looks like this:
The text was updated successfully, but these errors were encountered: