• Stars
    star
    151
  • Rank 246,057 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated almost 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

[DEPRECATED] Grunt task for compiling Facebook React's .jsx templates into .js

[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

  • v0.12.2

    • Move verbose logging to grunt --verbose (#53)
  • v0.12.1

    • Fix issue with Browserify (#46)
  • v0.12.0

    • Update all dependencies & devDependencies
  • v0.11.0

    • Update react-tools to v0.13.0 (#45)
  • v0.10.0

  • v0.9.0

    • Continue compilation despite error. (#31)
  • v0.8.4

    • Add support for harmony via additional options. (#32)
  • v0.8.3

    • Update to react-tools at ^v0.11.0 (#33)
  • v0.8.2

    • Emit react.error for Growl & other notifications (#23)
  • v0.8.1

    • Throw a proper error when React fails (#25)
  • v0.8.0

    • Update to React v0.10.0 (#27)
  • v0.7.0

    • Update to React v0.9.0 (#24)
  • v0.6.0

    • Task changes to allow for flexible file options as found in the grunt-contrib-* projects.
    • Taking hints from grunt-contrib-less to allow for compiling single files separately, dynamic mappings and combining.
    • Removed extension option as this is determined by flexible file matching now.
    • Removed MT time ignoring, this can be easily done with the grunt-newer plugin.
    • Errors are ignored and skipped by default to match how other grunt plugins work.
  • v0.5.2

    • grunt.fail instead of throwing an error (#11)
  • v0.5.1

    • Add file name to errors (#15)
  • v0.5.0

    • Update to react-tools ~v0.5.0
  • v0.4.1

    • Add logging to make it easier catch errors, thanks to @lorefnon (#5)
  • v0.4.0

    • Update to react-tools ~0.4.0, thanks to @Agent-H (#3)
  • v0.3.0

    • No longer uses bin/jsx, thanks to @petehunt (#2)
  • Add ignoreMTime option

  • v0.2.0

    • Add require('grunt-react').browserify() and require('grunt-react').source() for compiling within Node
  • v0.1.0

    • Initial release

Bitdeli Badge

More Repositories

1

click-to-component

Option+Click React components in your browser to instantly open the source in VS Code
TypeScript
1,710
star
2

react-resolver

Async rendering & data-fetching for universal React applications.
JavaScript
1,646
star
3

grunt-angular-templates

Grunt build task to concatenate & pre-load your AngularJS templates
JavaScript
712
star
4

webpack-hot-server-example

Webpack Hot Module Replacement (HMR) Example with Express
JavaScript
367
star
5

polydev

Faster, route-centric development for Node.js apps with built-in Hot Module Replacement.
JavaScript
284
star
6

unique-selector

Given a DOM node, return a unique CSS selector matching only that element
JavaScript
260
star
7

grunt-express-server

Grunt task for running an Express Server that works great with LiveReload + Watch/Regarde
JavaScript
248
star
8

terse-webpack

Simplified, fluent Webpack API with presets. Describe *what* your app needs, not *how*.
JavaScript
215
star
9

start-server-webpack-plugin

Automatically start your server once Webpack's build completes.
JavaScript
158
star
10

per-env

Clean up your package.json with per-NODE_ENV npm scripts
JavaScript
133
star
11

genesis-skeleton

Modern, opinionated, full-stack starter kit for rapid, streamlined application development.
CoffeeScript
106
star
12

if-env

Simplify npm scripts with `if-env ... && npm run this || npm run that`
JavaScript
97
star
13

node-recorder

Simple recording & replaying of HTTP requests for predictable development & testing.
TypeScript
97
star
14

jinja.js

Twig/Jinja/JinJS Client-Side Templating + jQuery Plugin
JavaScript
76
star
15

mdx-site

Static site generator powered by TypeScript, MDX, & React.
TypeScript
51
star
16

next.js-conf-2020

From Front-end to Full Stack with Amplify Framework
JavaScript
38
star
17

reload-server-webpack-plugin

Webpack plugin that automatically (re)starts your server between builds.
JavaScript
38
star
18

bookshelf-manager

Easily wire up models to APIs with supported for complex, nested saving.
JavaScript
34
star
19

universal-webpack

Library to greatly simplify running, watching, testing, & deploying universal apps with webpack.
JavaScript
34
star
20

medium-to-markdown

Convert Medium URLs to Markdown
JavaScript
30
star
21

mvc-to-react

Eric Clemmons' Space City JS 2015 talk – "Moving from MVC to React"
JavaScript
30
star
22

angular-github-demo

AngularJS demo application for HoustonJS
CSS
26
star
23

tech-2016-lightning-talks

10-minute lightning talks about Storybook, Styled Components, MobX, & Jest.
JavaScript
26
star
24

react.lazy-example

Testing out React.lazy (https://github.com/facebook/react/pull/13398)
JavaScript
24
star
25

grunt-angular-modularize

Write AngularJS Modules Once. Build with RequireJS (AMD), Browserify (CommonJS), or simply concat.
JavaScript
23
star
26

hot-module

Reload changed files in Node *without* restarting the server (like nodemon/piping/etc.)
JavaScript
23
star
27

github-semantic-version

Automated semantic version releases powered by Github Issues.
JavaScript
21
star
28

ECVagrantBundle

Symfony2 bundle to generate a working PHP5.3+ Vagrant environment
PHP
21
star
29

codelift

Visual Development for React, Next.js, & Tailwind CSS
17
star
30

de-pagify

Bookmarklet to enable "endless scroll" on popular sites
PHP
16
star
31

performant-3rd-party-widgets

Houston.js demo + slides for babel/webpack/react bundling & optimizations.
JavaScript
15
star
32

diez

Incredibly simple, Dependency Injection for isomorphic Javascript applications.
JavaScript
14
star
33

jest-storybook

Generate Jest snapshots from existing storybooks (similar to storyshots).
JavaScript
13
star
34

react-tooling-challenge

Competition to improve the tooling landscape for all React users.
13
star
35

eslint-config-future

ESLint Shareable Config for ES5/ES6/ES7 Javascript
JavaScript
12
star
36

wordpress-skeleton

[DEPRECATED] Dude, use https://github.com/genesis/wordpress/ !!
PHP
12
star
37

react-router-server-location

A React Router Location for universal apps.
JavaScript
12
star
38

start-cluster

Start a Node cluster by simply running `start-cluster`
JavaScript
11
star
39

graphql-context-services

GraphQL example using Context & Services
JavaScript
11
star
40

graphql-demo

Introduction to GraphQL (http://www.meetup.com/NodejsHouston/events/229815892/)
JavaScript
11
star
41

create-preact-pwa

"yarn create"-friendly version of preact-cli
JavaScript
9
star
42

grunt-verbosity

Adjust verbosity for individual grunt tasks
JavaScript
8
star
43

mootools-namespace

Allows Classes to specify namespace ("My.Fx.Accordion") and extend "Moo.Fx.Accordion", similar to Dojo.provide/require
JavaScript
6
star
44

tech-screen-template

Opinionated (but customizable!) template for tech screen interviews
TypeScript
6
star
45

express-hot-middleware

Simple way to enable hot-reloading in your Express applications.
JavaScript
6
star
46

babel-plugin-react-pure-to-class

https://github.com/gaearon/babel-plugin-react-transform/issues/57#issuecomment-167652102
JavaScript
5
star
47

graphql-mock-object

Easily prototype UIs with dynamic GraphQL objects
JavaScript
5
star
48

amplify-visor

Amplify Visor is a prototype GUI that customers run locally that streamlines the Amplify app-building experience – configuring & deploying Amplify visually, automatically installing dependencies, and reducing developer friction.
JavaScript
5
star
49

sync-github-pivotal

Bi-directional syncing between Pivotal Tracker & GitHub Issues
TypeScript
4
star
50

generator-genesis

Yeoman Generator for Genesis Skeleton
JavaScript
4
star
51

redux-reconnect

Redux hooks for React Resolver & Server-Side Rendering via "reconnect(...)"
3
star
52

create-graphql-app

Add an Amplify Function to `create-react-app`.
JavaScript
3
star
53

begin-react-app

Begin app
JavaScript
2
star
54

react-zones-example

Can Zones magically simplify SSR? http://blog.kwintenp.com/how-the-hell-do-zones-really-work/
2
star
55

vagrant-php

Instantly setup & launch PHP5.x Vagrant machine
PHP
2
star
56

vagrant-shim

Transparently run commands through your Vagrant VM.
PHP
2
star
57

sublime-typescript

DEPRECATED - Use Microsoft's official plugin instead!
2
star
58

ericclemmons

MDX
2
star
59

ericclemmons.com

TypeScript
2
star
60

amplify-flow

Native app for GitHub issue management for Amplify JS
TypeScript
2
star
61

ericclemmons.github.io

Official (Dynamic) Website of Eric Clemmons
CSS
2
star
62

zend-memcached-namespaced

Zend_Cache_Backend_Memcached with Namspace support for clearing specific keys
1
star
63

react-shared-state

Simple <SharedState> component to wrap your app & auto-inject Redux & MobX stores.
1
star
64

angular-lazy-load

Experimenting with lazy-loading angular.modules
JavaScript
1
star
65

microbundle-monorepo

Having an issue with CSS not being extracted when part of a monorepo
CSS
1
star
66

flummox-parent-promise

JavaScript
1
star
67

css-system

JavaScript
1
star
68

genesis-skeleton.com

Official website of Genesis Skeleton
CoffeeScript
1
star
69

mobx-resolver

Prefect MobX data dependencies before rendering React components.
1
star
70

redux-devtools-visual

Easily scrub through a visual history of your application, branch, and repeat!
1
star
71

amplify-js-canary

Canary releases for https://github.com/aws-amplify/amplify-js
1
star
72

amplify-js-issues-4506

https://github.com/aws-amplify/amplify-js/issues/4506
HTML
1
star
73

capistrano-svn-deployment

Example deployment script using Python & Capistrano for PHP apps in SVN
Python
1
star
74

ng-gitboard

Github dashboard powered by Angular.
JavaScript
1
star
75

redux-server

Redux-powered server & middleware for predictably handling requests with immutable responses.
1
star
76

ericclemmons.github.com

Office (Static) Website of Eric Clemmons
1
star