Search code examples
gruntjsgulpreactjsecmascript-harmony

Turn on Harmony mode -- how do I do that in order to run ReactJS v0.13?


I am following this comment where I am told to turn on harmony mode in order to use destructing assignments for ReactJS v0.13.

I have googled around and cannot find a good answer on how to turn this on.

If turning on Harmony mode is a bad idea, what is the highest version for ReactJS I must use instead?

UPDATE development workflow will either use grunt or gulp.


Solution

  • There isn't a single answer to your question as it depends on your development build workflow.

    If you were using node-jsx npm package for example:

    require('node-jsx').install({harmony: true});
    

    If you used the command line compiler:

    JSX filename -harmony
    

    There is a solution for webpack, grunt, etc.

    WebPack for example uses a loader flag:

    // webpack.config.js
    module.exports = {
      entry: './main.js',
      output: {
        filename: 'bundle.js'       
      },
      module: {
        loaders: [
          { test: /\.jsx$/, loader: 'jsx-loader?harmony' }
        ]
      },
      resolve: {
        // you can now require('file') instead of require('file.jsx')
        extensions: ['', '.js', '.json', '.jsx'] 
      }
    };
    

    There's an option for gulp that works like this:

    var gulp = require('gulp');
    var react = require('gulp-react');
    
    gulp.task('default', function () {
        return gulp.src('template.jsx')
            .pipe(react({harmony: true}))  // enable harmony features
            .pipe(gulp.dest('dist'));
    });