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

[0.32,0.33] Browser field in package.json not working #9854

Closed
Bhullnatik opened this issue Sep 12, 2016 · 5 comments
Closed

[0.32,0.33] Browser field in package.json not working #9854

Bhullnatik opened this issue Sep 12, 2016 · 5 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@Bhullnatik
Copy link
Contributor

Issue Description

When using a module which implement browser: {} in their package.json, the corresponding module(s) specified in the package.json will return null (Ref 1 and Ref 2) instead of an empty object as per the spec. This then throws an Unknown module error because the packager assumes that it didn't find a module when getting null (Ref).

I tracked the issue down to the new version of node-haste being used in React Native since the 0.32 version (cc @davidaurelio), but I'm not sure of how to fix it myself.

Steps to Reproduce / Code Snippets

Import a package using the browser field in package.json to ignore modules. https://github.com/yahoo/intl-messageformat for example (Ref package.json and incriminating line)

Expected Results

it should import an empty object instead of throwing an error.

Additional Information

  • React Native version: [0.32, 0.33]
  • Platform(s) (iOS, Android, or both?): packager
  • Operating System (macOS, Linux, or Windows?): *
@brentvatne
Copy link
Collaborator

cc @davidaurelio

@philikon
Copy link
Contributor

I'm seeing this problem too with https://github.com/philikon/ReactNativify

@philikon
Copy link
Contributor

philikon commented Sep 15, 2016

To summarize this problem:

  1. Package.redirectRequire() will return false if the browser field for the dependency is false in that package's package.json: https://github.com/facebook/react-native/blob/0.32-stable/packager/react-packager/src/node-haste/Package.js#L84-L87
  2. Based on that false return value, we return null for the respective module when resolving the dependency: https://github.com/facebook/react-native/blob/0.32-stable/packager/react-packager/src/node-haste/DependencyGraph/ResolutionRequest.js#L342-L345
  3. Later, we filter the dependencies and don't include any that are null (and we have no mocks in the default configuration) for the value that we pass to ResolutionResponse.setResolvedDependencyPairs(): https://github.com/facebook/react-native/blob/0.32-stable/packager/react-packager/src/node-haste/DependencyGraph/ResolutionRequest.js#L161-L178
  4. So by the time we rewrite the require() call in the bundle, we no longer have an record of that dependency, so we simply do not end up rewriting the call: https://github.com/facebook/react-native/blob/0.32-stable/packager/react-packager/src/Resolver/index.js#L197-L204

Without knowing too much of the internals of the packager, I think an elegant way to fix this would be to provide an empty module and return that instead of doing nothing in (3). Then the machinery in (4) simply does its normal thing, rewriting the require() call to require(27) where 27 would be the module ID of the empty module.

Thoughts, @davidaurelio?

@davidaurelio
Copy link
Contributor

Yeah, I guess the time to fix this has finally come :-P

as @philikon suggested, doing the following should work:

  1. Add an empty file somewhere, maybe next to ResolutionRequest. Or maybe in DependencyGraph/assets
  2. Instead of returning null, return the absolute path to that file.

If anybody wants to send a PR, just ping me on Messenger and I will shipit. I don’t always read github notifications. Otherwise I will do it tomorrow, London time.

@ghost ghost closed this as completed in 5710b23 Sep 16, 2016
@philikon
Copy link
Contributor

Thanks much for the fix, @davidaurelio!

mikelambert pushed a commit to mikelambert/react-native that referenced this issue Sep 19, 2016
Summary:
This adds support for `false` values in `package.json` `"browser"` and `"react-native"` mappings.
All `false` values are not longer silently ignored, but redirected to an empty file.

Fixes facebook#9854 facebook#9518

Reviewed By: bestander

Differential Revision: D3876521

fbshipit-source-id: 96d1ba03518812bc88c51672c374956eabd40c9b
cykler pushed a commit to NewStore/react-native that referenced this issue Oct 11, 2016
Summary:
This adds support for `false` values in `package.json` `"browser"` and `"react-native"` mappings.
All `false` values are not longer silently ignored, but redirected to an empty file.

Fixes facebook#9854 facebook#9518

Reviewed By: bestander

Differential Revision: D3876521

fbshipit-source-id: 96d1ba03518812bc88c51672c374956eabd40c9b
berrytj pushed a commit to mdcollab/react-native that referenced this issue Feb 1, 2017
Summary:
This adds support for `false` values in `package.json` `"browser"` and `"react-native"` mappings.
All `false` values are not longer silently ignored, but redirected to an empty file.

Fixes facebook#9854 facebook#9518

Reviewed By: bestander

Differential Revision: D3876521

fbshipit-source-id: 96d1ba03518812bc88c51672c374956eabd40c9b
@facebook facebook locked as resolved and limited conversation to collaborators May 24, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 19, 2018
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

5 participants