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

Use "babel-preset-react-native" #5214

Closed
wants to merge 24 commits into from

Conversation

skevy
Copy link
Contributor

@skevy skevy commented Jan 9, 2016

Rather than specifying Babel plugins in the .babelrc packaged with react-native, leverage a Babel preset to define the plugins (https://github.com/exponentjs/babel-preset-react-native).

This allows for a much better user experience for those who want (or need) to override options in their project's .babelrc.

Prior to this PR, if a user wanted to use a custom babel-plugin (or a custom set of babel plugins), they'd have either 1) manually override the .babelrc in the react-packager directory (or fork RN), or 2) specify a custom transformer to use when running the packager that loaded their own .babelrc. Note - the custom transformer was necessary because without it, RN's .babelrc options would supersede the options defined in the project's .babelrc...potentially causing issues with plugin ordering.

This PR makes the transformer check for the existence of a project-level .babelrc, and if it it's there, it doesn't use the react-native .babelrc. This prevents any oddities with Babel plugin ordering, and leaves that up to the RN user. The RN user would then just include a .babelrc file in their project that looks like:

{
  "presets": ["react-native"]
}

They can then add whatever other presets or plugins that they'd like (such as the Babel Relay Plugin or a decorators plugin, for example).

Super important note: after the fbjs changes are merged...this is really the final piece of getting Relay running OOTB in RN

/cc @ide @vjeux

@facebook-github-bot
Copy link
Contributor

By analyzing the blame information on this pull request, we identified @tadeuzagallo, @davidaurelio and @kittens to be potential reviewers.

@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 Jan 9, 2016
@skevy skevy force-pushed the babel-preset-react-native branch from e008d2d to 151c85e Compare January 9, 2016 05:25
@facebook-github-bot
Copy link
Contributor

@skevy updated the pull request.

@skevy
Copy link
Contributor Author

skevy commented Jan 9, 2016

@ide @vjeux Should we move babel-preset-react-native under the auspices of the "facebook" or "reactjs" orgs?

@skevy skevy force-pushed the babel-preset-react-native branch 2 times, most recently from 2aedde7 to ed0b665 Compare January 9, 2016 05:41
@skevy
Copy link
Contributor Author

skevy commented Jan 9, 2016

@martinbigio and/or @davidaurelio...vjeux suggested that one of you guys review :)

@satya164
Copy link
Contributor

satya164 commented Jan 9, 2016

Awesome!

@vjeux
Copy link
Contributor

vjeux commented Jan 9, 2016

Can you make a folder inside of this repo like we do for react-native-cli? Would be easier to manage

@skevy
Copy link
Contributor Author

skevy commented Jan 9, 2016

@vjeux that seems reasonable to me. Should it be committed through the shipit bot? Or should it live in OSS only?

@satya164
Copy link
Contributor

Any reason for the rename of '.babelrc'?

@davidaurelio
Copy link
Contributor

Guys, let me discuss this with @martinbigio, we need to sort out one thing ;-)

@ide
Copy link
Contributor

ide commented Jan 10, 2016

@skevy @vjeux: About moving the preset under a folder, sounds good. I'm also happy to add whoever needs to be a collaborator to the repo & npm owners list. I think we can figure that out later.

The main thing I think is important is that the babelrc.json file depends on the preset so that RN's internal configuration stays in sync with people's custom .babelrc files that are using the preset. Also the preset should follow semver, just like any other npm package.

@skevy
Copy link
Contributor Author

skevy commented Jan 11, 2016

@satya164 when @ide and I were talking, we were just thinking that since the RN babelrc is handled custom anyway, via transformer.js, that it should be named with ".json" so there's not a possibility of conflict with any other babelrc's.


function transform(src, filename, options) {
options = options || {};
const projectBabelRCPath = path.resolve(__dirname, '..', '..', '..', '.babelrc');
Copy link
Contributor

Choose a reason for hiding this comment

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

Thinking about this some more, I think the babelrc under the path specified by --projectRoots is what we want to use. That way if you invoke the packager and point it at another directory, we'd correctly use that directory's .babelrc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about that -- but what are your thoughts on if there are multiple project roots?

Copy link
Contributor

Choose a reason for hiding this comment

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

This line only works for OS and if react native is in ${projectDirectory}/node_modules/react-native. I’m a little bit afraid of the apocalyptic potential of this innocent path lookup ;-)

Is anybody outside of Facebook using multiple project roots? The first project root might be a saner choice. Or we can add a command line option.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's default to the first project root, and I'll add a command line option to specify a babelrc path.

@satya164
Copy link
Contributor

@skevy Just to note, I presume some people have a .babelrc like this (I do),

{
  "extends": "react-native/packager/react-packager/.babelrc"
}

Which will break after the rename. Not a big deal, just wanted to note.

@ide
Copy link
Contributor

ide commented Jan 15, 2016

@satya164 Good point. We should mention it under Breaking Changes in the release notes.

@satya164
Copy link
Contributor

@ide Yup.

@@ -16,7 +16,7 @@ var path = require('path');
var _only = [];

function readBabelRC() {
var rcpath = path.join(__dirname, 'react-packager', '.babelrc');
var rcpath = path.join(__dirname, 'react-packager', '.babelrc.json');
Copy link
Contributor

Choose a reason for hiding this comment

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

Let’s remove the leading dot.

ghost pushed a commit that referenced this pull request Jan 21, 2016
Summary:
At the moment we have to disable strict mode for the transform-es2015-modules-commonjs because strict mode leaks to the global scope and breaks the bridge. It was due to the way the polyfills were bundled in the package. To fix it, I wrapped the polyfill modules in an IIFE. Then when strict mode was enabled some polyfills were broken due to strict mode errors so that was fixed too. Also removed the IIFE from the polyfills that included one.

This diff doesn't enable the strict mode transform since some internal facebook modules depend on it not being enabled. When #5214 lands we could make the default babel config shipped with OSS react-native use strict mode modules and facebook could just modify the babel config to disable it if needed.

This will allow removing `"strict": false` from https://github.com/facebook/react-native/blob/master/packager/react-packager/.babelrc#L16

Fixes #5316
Closes #5422

Reviewed By: svcscm

Differential Revision: D2846422

Pulled By: davidaurelio

fb-gh-sync-id: a3e2f8909aa87dabab2b872c61b887e80220fb56
MattFoley pushed a commit to skillz/react-native that referenced this pull request Jan 21, 2016
Summary:
At the moment we have to disable strict mode for the transform-es2015-modules-commonjs because strict mode leaks to the global scope and breaks the bridge. It was due to the way the polyfills were bundled in the package. To fix it, I wrapped the polyfill modules in an IIFE. Then when strict mode was enabled some polyfills were broken due to strict mode errors so that was fixed too. Also removed the IIFE from the polyfills that included one.

This diff doesn't enable the strict mode transform since some internal facebook modules depend on it not being enabled. When facebook#5214 lands we could make the default babel config shipped with OSS react-native use strict mode modules and facebook could just modify the babel config to disable it if needed.

This will allow removing `"strict": false` from https://github.com/facebook/react-native/blob/master/packager/react-packager/.babelrc#L16

Fixes facebook#5316
Closes facebook#5422

Reviewed By: svcscm

Differential Revision: D2846422

Pulled By: davidaurelio

fb-gh-sync-id: a3e2f8909aa87dabab2b872c61b887e80220fb56
});

const result = babel.transform(src, Object.assign({}, babelRC, config));
return Object.assign({}, babelRC, extraConfig);
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't extraConfig be config instead? config doesn't seems to be used anywhere.

@skevy skevy force-pushed the babel-preset-react-native branch from ed0b665 to c5dd703 Compare January 25, 2016 04:58
@facebook-github-bot
Copy link
Contributor

@skevy updated the pull request.

1 similar comment
@facebook-github-bot
Copy link
Contributor

@skevy updated the pull request.

@facebook-github-bot
Copy link
Contributor

@skevy updated the pull request.

@facebook-github-bot
Copy link
Contributor

@skevy updated the pull request.

@skevy skevy force-pushed the babel-preset-react-native branch from a439c19 to e5e3ba7 Compare February 2, 2016 17:38
@facebook-github-bot
Copy link
Contributor

@skevy updated the pull request.

@facebook-github-bot
Copy link
Contributor

@skevy updated the pull request.

@facebook-github-bot
Copy link
Contributor

@skevy updated the pull request.

@ghost ghost closed this in e6cb02d Feb 3, 2016
@chirag04
Copy link
Contributor

chirag04 commented Feb 3, 2016

👍

@vjeux
Copy link
Contributor

vjeux commented Feb 4, 2016

This officially broke react-native for node < 4.0. We should either revert the arrow functions or crash earlier if not using node 4

screen shot 2016-02-04 at 1 49 27 pm

@skevy
Copy link
Contributor Author

skevy commented Feb 4, 2016

@vjeux I definitely think we should crash earlier if not using Node 4. No one should be using less than Node 4, and it already says at the top of your screenshot that React Native runs on Node 4.0 or newer.

@skevy
Copy link
Contributor Author

skevy commented Feb 4, 2016

@vjeux
Copy link
Contributor

vjeux commented Feb 4, 2016

@skevy
Copy link
Contributor Author

skevy commented Feb 4, 2016

@vjeux ah so i guess just exit there if less than node 4.

@vjeux
Copy link
Contributor

vjeux commented Feb 4, 2016

Cool, i'll send a pull request

@bestander
Copy link
Contributor

Be careful with version checks though.
Remember good old browser sniffing or why there is now Windows 9?

On Thursday, 4 February 2016, Adam Miskiewicz notifications@github.com
wrote:

@vjeux https://github.com/vjeux ah so i guess just exit there if less
than node 4.


Reply to this email directly or view it on GitHub
#5214 (comment)
.

@lrettig
Copy link
Contributor

lrettig commented Feb 11, 2016

Is there an ETA on this? Thanks!

@skevy
Copy link
Contributor Author

skevy commented Feb 11, 2016

@lrettig this is already merged in master. It should be in the current RC and will be officially released in 0.20.

@lrettig
Copy link
Contributor

lrettig commented Feb 11, 2016

Thanks. Didn't see it mentioned in the release notes!

On Thu, Feb 11, 2016 at 12:02 PM, Adam Miskiewicz notifications@github.com
wrote:

@lrettig https://github.com/lrettig this is already merged in master.
It should be in the current RC and will be officially released in 0.20.


Reply to this email directly or view it on GitHub
#5214 (comment)
.

@facebook-github-bot
Copy link
Contributor

@skevy updated the pull request.

pglotov pushed a commit to pglotov/react-native that referenced this pull request Mar 15, 2016
Summary:
Rather than specifying Babel plugins in the `.babelrc` packaged with react-native, leverage a Babel preset to define the plugins (https://github.com/exponentjs/babel-preset-react-native).

This allows for a much better user experience for those who want (or need) to override options in their project's `.babelrc`.

Prior to this PR, if a user wanted to use a custom babel-plugin (or a custom set of babel plugins), they'd have either 1) manually override the `.babelrc` in the react-packager directory (or fork RN), or 2) specify a custom transformer to use when running the packager that loaded their own `.babelrc`. Note - the custom transformer was necessary because without it, RN's `.babelrc` options would supersede the options defined in the project's `.babelrc`...potentially causing issues with plugin ordering.

This PR makes the transformer check for the existence of a project-level `.babelrc`, and if it it's there, it _doesn't_ use the react-native `.babelrc`. This prevents any oddities with Babel plug
Closes facebook#5214

Reviewed By: davidaurelio

Differential Revision: D2881814

Pulled By: martinbigio

fb-gh-sync-id: 4168144b7a365fae62bbeed094d8a03a48b4798c
ghost pushed a commit that referenced this pull request Mar 21, 2016
Summary:Since #5422 react-native works with strict mode modules but the transform was not updated since Facebook has some non strict mode compatible internal modules. Now that #5214 has landed and it is easy to change the babel config I think we should enable it by default to make es2015 modules spec compliant.

Someone at Facebook will have to make the internal changes necessary to disable strict mode modules for their projects that use non strict mode compatible modules by including a .babelrc file with
``` json
{
  "presets": [
    "react-native"
  ],
  "plugins": [
    ["transform-es2015-modules-commonjs", { "strict": false, "allowTopLevelThis": true }]
  ]
}
```
before merging this.

We might also want to mention this in the breaking change section for the next release.
Closes #5796

Differential Revision: D3075802

fb-gh-sync-id: e807b67401107e1e944db38453e254025ce0a6c7
shipit-source-id: e807b67401107e1e944db38453e254025ce0a6c7
jeffchienzabinet pushed a commit to jeffchienzabinet/react-native that referenced this pull request Mar 21, 2016
Summary:Since facebook#5422 react-native works with strict mode modules but the transform was not updated since Facebook has some non strict mode compatible internal modules. Now that facebook#5214 has landed and it is easy to change the babel config I think we should enable it by default to make es2015 modules spec compliant.

Someone at Facebook will have to make the internal changes necessary to disable strict mode modules for their projects that use non strict mode compatible modules by including a .babelrc file with
``` json
{
  "presets": [
    "react-native"
  ],
  "plugins": [
    ["transform-es2015-modules-commonjs", { "strict": false, "allowTopLevelThis": true }]
  ]
}
```
before merging this.

We might also want to mention this in the breaking change section for the next release.
Closes facebook#5796

Differential Revision: D3075802

fb-gh-sync-id: e807b67401107e1e944db38453e254025ce0a6c7
shipit-source-id: e807b67401107e1e944db38453e254025ce0a6c7
cpojer pushed a commit to facebook/metro that referenced this pull request Jan 26, 2017
Summary:
Rather than specifying Babel plugins in the `.babelrc` packaged with react-native, leverage a Babel preset to define the plugins (https://github.com/exponentjs/babel-preset-react-native).

This allows for a much better user experience for those who want (or need) to override options in their project's `.babelrc`.

Prior to this PR, if a user wanted to use a custom babel-plugin (or a custom set of babel plugins), they'd have either 1) manually override the `.babelrc` in the react-packager directory (or fork RN), or 2) specify a custom transformer to use when running the packager that loaded their own `.babelrc`. Note - the custom transformer was necessary because without it, RN's `.babelrc` options would supersede the options defined in the project's `.babelrc`...potentially causing issues with plugin ordering.

This PR makes the transformer check for the existence of a project-level `.babelrc`, and if it it's there, it _doesn't_ use the react-native `.babelrc`. This prevents any oddities with Babel plug
Closes facebook/react-native#5214

Reviewed By: davidaurelio

Differential Revision: D2881814

Pulled By: martinbigio

fb-gh-sync-id: 4168144b7a365fae62bbeed094d8a03a48b4798c
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.