Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a custom Babel transform with React-Native

I need to use babel-plugin-transform-decorators-legacy with React-Native to enable @decorators. How does one configure React-Native / Babel to make this possible?

This is related to my previous question about how to get @decorators working in React-Native: https://stackoverflow.com/a/34271636/941058

like image 204
kingdango Avatar asked Dec 16 '15 00:12

kingdango


People also ask

How do you use Babel in React Native?

React Native itself uses this Babel preset by default when transforming your app's source code. If you wish to use a custom Babel configuration by writing a babel. config. js file in your project's root directory, you must specify all the plugins necessary to transform your code.

How do you create .babelrc file in React Native?

You can create our own “. babelrc” file at the root of your project. To make sure that you use the default babel configuration that comes with React Native, install babel-preset-react-native. On top of this you have to install another module: codegen.

Where is .babelrc in React Native?

babelrc in your react native folder in your node_modules folder. This file: react-native/packager/react-packager/. babelrc. Put it before the syntax-class-properties.


2 Answers

Use official Babel presets

Install the official Babel presets for React Native applications:

npm i babel-preset-react-native --save-dev

Edit your .babelrc:

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

Prior RN v21.0: Extend the original .babelrc from react-native

To avoid manipulating files in the node_modules directory, you should extend the original .babelrc.

By keeping your dependencies clean, there won't be any issues with upgrades or sharing the project.

{
    "extends": "react-native/packager/react-packager/.babelrc",
}
like image 65
purii Avatar answered Sep 17 '22 23:09

purii


You would install the plugin at your projects root level then add the plugin to the .babelrc in node_modules/react-native/packager/react-packager/.babelrc

The issue with this work around to using @decorators is if you are working on a team for the app every developer will need to make the change to .babelrc since node_modules should not be checked into source.

like image 26
rmevans9 Avatar answered Sep 17 '22 23:09

rmevans9