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

support react-native field with fallback to browser field #2208

Closed
wants to merge 1 commit into from

Conversation

mvayngrib
Copy link
Contributor

React Native is a lot more powerful an environment than the browser, so we need an alternate mapping, as specified here

An example:

{
  "browser": {
     "./lib/server": false
   },
   "react-native": {
     "dgram": "react-native-udp",
     "fs": "react-native-level-fs"
   },
   "chromeapp": {
     "dgram": "chrome-dgram",
     "fs": "level-filesystem"
   }
}

on the other hand, if "react-native" is not present in package.json, you should fall back to "browser"

other than the one (nesting) test added, the tests are unchanged, just done for both "react-native" and "browser"

(I've implemented react-native-udp and react-native-level-fs, but they obviously don't belong in the traditional "browser" field as they won't run anywhere except in React Native.)

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Aug 3, 2015
@ide
Copy link
Contributor

ide commented Aug 3, 2015

I like this approach more than anything else I've seen so far.

@lightsofapollo
Copy link

+1

@amasad
Copy link
Contributor

amasad commented Sep 8, 2015

Nice!

We're in the process of moving the packager to it's own github repo and we'll be able to accept contributions like this much faster (we can just use the Github merge button). I'm hoping we can release it some time this week, and I'll ping you so we can move this PR there. Thanks!

@mvayngrib
Copy link
Contributor Author

@amasad great, see you there :)

@facebook-github-bot
Copy link
Contributor

@mvayngrib updated the pull request.

@vjeux
Copy link
Contributor

vjeux commented Nov 9, 2015

Awesome, @mvayngrib sorry for the huge wait. Do you mind rebasing and i'll pull it in? Thanks!

@mvayngrib
Copy link
Contributor Author

@vjeux will do after Thurs, under serious time pressure till then. If it's urgent, I can add u as a collaborator on my fork.

@vjeux
Copy link
Contributor

vjeux commented Nov 9, 2015

Not urgent at all, it waited for a long time, can wait a bit more :)

On Nov 9, 2015, at 12:31 PM, Mark Vayngrib [email protected] wrote:

@vjeux will do after Thurs, under serious time pressure till then. If it's urgent, I can add u as a collaborator on my fork.


Reply to this email directly or view it on GitHub.

@chetstone
Copy link
Contributor

This looks like a great fix, but I have a couple of questions. First, packager appears to support building web apps. I haven't used it for that and don't know if anyone else does, but if building for web, will it pick up the browser field instead of the react-native field from the package?

Second, I wonder what folks who use webpack for building react-native will have to do to make use of this field? Perhaps there's a webpack config option? or maybe webpack will have to be modified...
(Just checked: webpack has a resolve.packageMains option that should do the job.)

@facebook-github-bot
Copy link
Contributor

@mvayngrib updated the pull request.

@@ -90,4 +91,8 @@ class Package {
}
}

function getReplacements (pkg) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remove space between fn name and open paren.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May want to do a more robust check below that looks whether pkg[react-native] == null so that it returns empty strings when specified

@@ -90,4 +91,10 @@ class Package {
}
}

function getReplacements(pkg) {
return pkg['react-native'] == null
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any plans to support excludes? (like "./lib/blah.js": false)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Browserify supports it and it's not too hard to add, we could support it eventually, if the need arises.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, with browserify it's pretty much a staple

@facebook-github-bot
Copy link
Contributor

@mvayngrib updated the pull request.

@ide
Copy link
Contributor

ide commented Nov 24, 2015

@vjeux can you or @martinbigio import this then?

@almeynman
Copy link

Finally :)

@vjeux
Copy link
Contributor

vjeux commented Nov 24, 2015

@facebook-github-bot shipit

@facebook-github-bot
Copy link
Contributor

Thanks for importing. If you are an FB employee go to https://our.intern.facebook.com/intern/opensource/github/pull_request/842517002521880/int_phab to review.

@ghost ghost closed this in b86f147 Nov 24, 2015
sunnylqm pushed a commit to sunnylqm/react-native that referenced this pull request Dec 2, 2015
Summary: React Native is a lot more powerful an environment than the browser, so we need an alternate mapping, as specified [here](https://github.com/defunctzombie/node-browser-resolve#browser-field)

An example:
```js
{
  "browser": {
     "./lib/server": false
   },
   "react-native": {
     "dgram": "react-native-udp",
     "fs": "react-native-level-fs"
   },
   "chromeapp": {
     "dgram": "chrome-dgram",
     "fs": "level-filesystem"
   }
}
```

on the other hand, if "react-native" is not present in package.json, you should fall back to "browser"

other than the one (nesting) test added, the tests are unchanged, just done for both "react-native" and "browser"

(I've implemented [react-native-udp](https://npmjs.org/package/react-native-udp) and [react-native-level-fs](https://npmjs.org/package/react-native-level-fs), but they obviously don't belong in the traditional "browser" field as they won't run anywhere except in React Native.)
Closes facebook#2208

Reviewed By: svcscm

Differential Revision: D2691236

Pulled By: vjeux

fb-gh-sync-id: 34041ed50bda4ec07f31d1dc50dcdfa428af2512
Crash-- pushed a commit to Crash--/react-native that referenced this pull request Dec 24, 2015
Summary: React Native is a lot more powerful an environment than the browser, so we need an alternate mapping, as specified [here](https://github.com/defunctzombie/node-browser-resolve#browser-field)

An example:
```js
{
  "browser": {
     "./lib/server": false
   },
   "react-native": {
     "dgram": "react-native-udp",
     "fs": "react-native-level-fs"
   },
   "chromeapp": {
     "dgram": "chrome-dgram",
     "fs": "level-filesystem"
   }
}
```

on the other hand, if "react-native" is not present in package.json, you should fall back to "browser"

other than the one (nesting) test added, the tests are unchanged, just done for both "react-native" and "browser"

(I've implemented [react-native-udp](https://npmjs.org/package/react-native-udp) and [react-native-level-fs](https://npmjs.org/package/react-native-level-fs), but they obviously don't belong in the traditional "browser" field as they won't run anywhere except in React Native.)
Closes facebook#2208

Reviewed By: svcscm

Differential Revision: D2691236

Pulled By: vjeux

fb-gh-sync-id: 34041ed50bda4ec07f31d1dc50dcdfa428af2512
@vespakoen
Copy link

Hey! Thanks a lot for this, I noticed a couple of things that are lacking in this implemenation.

  1. main checks do not include './' + main, here is my quick fix:
      if (replacements && typeof replacements === 'object') {
        main = replacements['./' + main] ||
          replacements['./' + main + '.js'] ||
          replacements['./' + main + '.json'] ||
          replacements['./' + main.replace(/(\.js|\.json)$/, '')] ||
          replacements[main] ||
          replacements[main + '.js'] ||
          replacements[main + '.json'] ||
          replacements[main.replace(/(\.js|\.json)$/, '')] ||
          main;
  1. The "browserify" field is not considered, here is my quick fix:
function getReplacements(pkg) {
  return pkg['react-native'] == null
    ? (pkg.browser == null ? pkg.browserify : pkg.browser)
    : pkg['react-native'];
}

@skevy
Copy link
Contributor

skevy commented Feb 26, 2016

@vespakoen if you'd like to submit a PR to fix this stuff, that would be great!

@vespakoen
Copy link

Unfortunately I found my laptop on the floor this morning and my display is bricked, will get some monitor on monday and make a PR - written on my android

@blackxored
Copy link

What happened to this?

@chetstone
Copy link
Contributor

The PR was accepted and merged into RN 0.17 I believe

On Tue, Jul 19, 2016 at 23:45 Adrian Perez [email protected] wrote:

What happened to this?


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#2208 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AA7l1yZDK2oZQ8PIk9aB-ygpCUKXQt_-ks5qXbXvgaJpZM4FkT9B
.

@blackxored
Copy link

Did it? I must have looked in the wrong place, as in JavaScriptEngine
something. Thanks.

Best Regards,

Adrian Perez
https://adrianperez.codes

On July 20, 2016 at 7:47:35 PM, Chester Wood ([email protected])
wrote:

The PR was accepted and merged into RN 0.17 I believe

On Tue, Jul 19, 2016 at 23:45 Adrian Perez [email protected]
wrote:

What happened to this?


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<
#2208 (comment)
,
or mute the thread
<
https://github.com/notifications/unsubscribe-auth/AA7l1yZDK2oZQ8PIk9aB-ygpCUKXQt_-ks5qXbXvgaJpZM4FkT9B

.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#2208 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAIIvM-taiwgf43gU81S_TdF4ADMCoK-ks5qXl83gaJpZM4FkT9B
.

@DeDuckProject
Copy link

It this working at the moment?

My package.json looks like this:

{
"name": "fetchy",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"setup_project": "npm run step1; npm run step2",
"step1": "rm -rf node_modules/react-native/node_modules/react; rm -rf node_modules/react-native/node_modules/fbjs; rm -rf node_modules/react/node_modules/fbjs",
"step2": "cd node_modules; find . -name .babelrc | grep -v packager | xargs rm"
},
"react-native": {
"domain": false
},
"dependencies": {
"globalize": "^1.1.1",
"moment": "^2.14.1",
"parse": "1.6.14",
"parse-react": "0.5.2",
"react": "^15.3.0",
"react-native": "0.30.0",
"react-datepicker": "^0.29.0",
"react-dom": "^15.3.0",
"react-redux": "^4.4.5",
"react-widgets": "^3.4.3",
"redux": "3.3.1",
"redux-form": "^5.3.2",
"redux-logger": "^2.6.1",
"redux-promise": "^0.5.3",
"redux-thunk": "1.0.3"
},
"devDependencies": {
"babel-core": "^6.11.4",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.11.1",
"chai": "^3.5.0",
"chai-immutable": "^1.6.0",
"css-loader": "^0.23.1",
"extract-text-webpack-plugin": "^1.0.1",
"globalize-webpack-plugin": "^0.3.5",
"jsdom": "^9.4.1",
"mocha": "^3.0.0",
"node-sass": "^3.8.0",
"react-hot-loader": "^1.3.0",
"sass-loader": "^4.0.0",
"style-loader": "^0.13.1",
"webpack": "^1.13.1",
"webpack-dev-server": "^1.14.1"
}
}

and then in my code:

var domain = require('domain');
console.log(domain);

and I get this:

"Requiring unknown module "domain"...." message. any idea?

screenshot 2016-08-15 18 51 19

@mvayngrib
Copy link
Contributor Author

facebookarchive/node-haste#77 for browser exclude support has been merged, but node-haste has not been republished to npm since. You can try overriding installing node-haste from github to react-native's node-haste dep locally

@gitim
Copy link
Contributor

gitim commented Aug 22, 2016

Have the same error #9518, since node-haste was brought back to the react-native 667aaa4 overriding installing node-haste from github doesn't make sense.

cpojer pushed a commit to facebook/metro that referenced this pull request Jan 26, 2017
Summary: React Native is a lot more powerful an environment than the browser, so we need an alternate mapping, as specified [here](https://github.com/defunctzombie/node-browser-resolve#browser-field)

An example:
```js
{
  "browser": {
     "./lib/server": false
   },
   "react-native": {
     "dgram": "react-native-udp",
     "fs": "react-native-level-fs"
   },
   "chromeapp": {
     "dgram": "chrome-dgram",
     "fs": "level-filesystem"
   }
}
```

on the other hand, if "react-native" is not present in package.json, you should fall back to "browser"

other than the one (nesting) test added, the tests are unchanged, just done for both "react-native" and "browser"

(I've implemented [react-native-udp](https://npmjs.org/package/react-native-udp) and [react-native-level-fs](https://npmjs.org/package/react-native-level-fs), but they obviously don't belong in the traditional "browser" field as they won't run anywhere except in React Native.)
Closes facebook/react-native#2208

Reviewed By: svcscm

Differential Revision: D2691236

Pulled By: vjeux

fb-gh-sync-id: 34041ed50bda4ec07f31d1dc50dcdfa428af2512
benjamn added a commit to apollographql/apollo-client that referenced this pull request Feb 18, 2020
React Native used to have Map and Set polyfills that relied on tagging
objects with a special __MAP_POLYFILL_INTERNAL_HASH__ property, which
breaks down when the object to be tagged happens to be frozen.

In 833072e (#3964) I introduced a
workaround to make sure objects got tagged before becoming non-extensible,
which robustly solved the problem for any Map and Set polyfills that rely
on an object tagging strategy.

Note that Apollo Client freezes objects only in development (using
maybeDeepFreeze), so the problem fixPolyfills.ts was intended to solve was
only ever a problem in development.

I also submitted a PR to React Native that make their Map and Set
polyfills store non-extensible objects in an array, rather than attempting
to tag them: facebook/react-native#21492. Those
changes were first released in React Native 0.59.0, so technically the
fixPolyfills.ts logic should not be necessary for anyone using that
version or later.

Since then, React Native has stopped using any polyfills for Map and Set
(yay!), so the need for the workaround has been even further reduced:
facebook/react-native@93b9ac7

Those changes were first released in React Native 0.61.0, which is still
the latest minor version (0.61.5 is latest). I'm not sure how many people
are still using older versions of React Native, or what sort of LTS
policies they have. Expo SDK 36 uses 0.61.4, for what it's worth:
https://docs.expo.io/versions/latest/sdk/overview/

In any case, I think we can eliminate these polyfills from the default
bundle, as long as we take some care to include them when bundling for
React Native. This strategy uses a combination of techniques for selective
bundling in React Native:
https://facebook.github.io/react-native/docs/platform-specific-code#platform-specific-extensions
facebook/react-native#2208
benjamn added a commit to apollographql/apollo-client that referenced this pull request Feb 18, 2020
React Native used to have Map and Set polyfills that relied on tagging
objects with a special __MAP_POLYFILL_INTERNAL_HASH__ property, which
breaks down when the object to be tagged happens to be frozen.

In 833072e (#3964) I introduced a
workaround to make sure objects got tagged before becoming non-extensible,
which robustly solved the problem for any Map and Set polyfills that rely
on an object tagging strategy.

Note that Apollo Client freezes objects only in development (using
maybeDeepFreeze), so the problem fixPolyfills.ts was intended to solve was
only ever a problem in development.

I also submitted a PR to React Native that make their Map and Set
polyfills store non-extensible objects in an array, rather than attempting
to tag them: facebook/react-native#21492. Those
changes were first released in React Native 0.59.0, so technically the
fixPolyfills.ts logic should not be necessary for anyone using that
version or later.

Since then, React Native has stopped using any polyfills for Map and Set
(yay!), so the need for the workaround has been even further reduced:
facebook/react-native@93b9ac7

Those changes were first released in React Native 0.61.0, which is still
the latest minor version (0.61.5 is latest). I'm not sure how many people
are still using older versions of React Native, or what sort of LTS
policies they have. Expo SDK 36 uses 0.61.4, for what it's worth:
https://docs.expo.io/versions/latest/sdk/overview/

In any case, I think we can eliminate these polyfills from the default
bundle, as long as we take some care to include them when bundling for
React Native. This strategy uses a combination of techniques for selective
bundling in React Native:
https://facebook.github.io/react-native/docs/platform-specific-code#platform-specific-extensions
facebook/react-native#2208
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.