Home

Awesome

[DEPRECATED] grunt-react

Build Status Dependencies devDependencies

Grunt task for compiling Facebook React's JSX templates into JavaScript.

It also works great with grunt-browserify!


DEPRECATION NOTICE

On June 12th, 2015, the React team has deprecated JSTransform and react-tools, which this project uses:

http://facebook.github.io/react/blog/2015/06/12/deprecating-jstransform-and-react-tools.html

Please use grunt-babel instead.


Getting Started

This plugin requires Grunt ~0.4.0

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-react --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-react');

The "react" task

Overview

In your project's Gruntfile, add a section named react to the data object passed into grunt.initConfig().

grunt.initConfig({
  react: {
    single_file_output: {
      files: {
        'path/to/output/dir/output.js': 'path/to/jsx/templates/dir/input.jsx'
      }
    },
    combined_file_output: {
      files: {
        'path/to/output/dir/combined.js': [
          'path/to/jsx/templates/dir/input1.jsx',
          'path/to/jsx/templates/dir/input2.jsx'
        ]
      }
    },
    dynamic_mappings: {
      files: [
        {
          expand: true,
          cwd: 'path/to/jsx/templates/dir',
          src: ['**/*.jsx'],
          dest: 'path/to/output/dir',
          ext: '.js'
        }
      ]
    }
  },
})

Options

These options are passed to react-tools.

options.extension

Type: String Default value: js

Extension of files to search for JSX-syntax & convert to JS.

options.ignoreMTime

Type: Boolean Default value: false

Speed up compilation of JSX files by skipping files not modified since last pass.

options.harmony

Type: Boolean Default value: false

Turns on JS transformations such as ES6 Classes.

options.sourceMap

Type: Boolean Default value: false

Append inline source map at the end of the transformed source

Turns on JS transformations such as ES6 Classes.

options.es6module

Type: Boolean Default value: false

Allows use of ES6 module syntax. This option does not affect ES6 transformations enabled or disabled by options.harmony.


Recommended Usage

Writing your applications in CommonJS format will allow you to use Browserify to concatenate your files. Plus, with grunt-react, your templates will be converted from JSX to JS automatically!

First, install grunt-browserify to your project:

npm install grunt-browserify --save-dev

Second, register grunt-browserify in your Gruntfile:

grunt.loadNpmTasks('grunt-browserify');

Finally, add the following task to your Gruntfile:

browserify:     {
  options:      {
    transform:  [ require('grunt-react').browserify ]
  },
  app:          {
    src:        'path/to/source/main.js',
    dest:       'path/to/target/output.js'
  }
}

You've successfully concatenated your JSX & JS files into one file!


Usage Examples

Recommended Options

I recommend naming your React Components with the .jsx extension:

/**
 * @jsx React.DOM
 */

var MyComponent = React.createClass({
  ...
  render: function() {
    return (
      <p>Howdy</p>
    );
  }
});

Then, set your Gruntfile to use:

grunt.initConfig({
  react: {
    files: {
      expand: true,
      cwd: 'path/to/jsx/templates/dir',
      src: ['**/*.jsx'],
      dest: 'path/to/output/dir',
      ext: '.js'
    }
  },
})

This will output the following:

/**
 * @jsx React.DOM
 */

var MyComponent = React.createClass({displayName: 'MyComponent',
  render: function() {
    return (
      React.DOM.p(null, "Howdy")
    );
  }
});

Troubleshooting

If you encounter a file compilation error, you can run grunt --verbose to see specifics about each file being transformed.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

Bitdeli Badge