Gradle plugin for React
This is a very simple Gradle plugin to transform JSX sources into JavaScript. It was inspired by and is using the gradle-node-plugin from Sten Roger Sandvik.
React is
A JAVASCRIPT LIBRARY FOR BUILDING USER INTERFACES
developed at Facebook and Instagram.
Installing the plugin
Releases of this plugin are hosted at BinTray (http://bintray.com) and is part of jcenter repository. Setup the plugin like this:
Gradle versions since 2.1
plugins {
id 'net.eikehirsch.react' version '0.4.1'
}
Gradle versions below 2.1
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'net.eikehirsch.react:gradle-react-plugin:0.4.1'
}
}
apply plugin: 'net.eikehirsch.react'
The plugin will also apply gradle-node-plugin for Node and NPM related tasks. (see http://github/srs/gradle-node-plugin for details).
Using the plugin
Simply run
./gradlew jsx
to transform any js file in src/main/react
. The resulting files will be stored at build/react
.
Configure the plugin
To change the defaults you can put all your settings into the jsx
namespace:
jsx {
sourcesDir = 'src/react'
destDir = 'out'
// optional
options {
extension = 'jsx' // js is the default extension
}
}
Create your own jsx task
You can define the input and output folders without using the extension namespace like this:
task myJSX( type: JSXTask ) {
sourcesDir = 'src/react'
destDir = 'out'
// optional
options {
extension = 'js' // js is the default extension
}
}
(You can try this at the configuration example project)
Include jsx with the build
If you want to have your jsx sources transformed everytime you build your project, you could do something like this:
processResources.dependsOn jsx
// for older gradle versions (e.g. 1.4) use:
processResources.dependsOn 'jsx'
Building the Plugin
To build the plugin, just type the following command:
./gradlew clean build
Acknowledgments
- Thanks to Sten R. Sandvik for his node plugin
- that's all for now.