Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting reactify and browserify to work with ES6

I have a browserify task that is configured like so:

module.exports = function(grunt) {

  grunt.config.set('browserify', {
    dev: {
      src: 'assets/js/main.jsx',
      dest: '.tmp/public/js/main.js',
      options: {
        debug: true,
        extensions: ['.jsx'],
        transform: ['reactify']
      }
    }
  });

  grunt.loadNpmTasks('grunt-browserify');
};

I tried configuring it to use es6 this way:

module.exports = function(grunt) {

  grunt.config.set('browserify', {
    dev: {
      src: 'assets/js/main.jsx',
      dest: '.tmp/public/js/main.js',
      options: {
        debug: true,
        extensions: ['.jsx'],
        transform: ['reactify', {'es6': true}]
      }
    }
  });

  grunt.loadNpmTasks('grunt-browserify');
};

This causes an error though:

Error: path must be a string

I can't understand from the docs how to do this given that I don't want to configure the transform in my package.json. Any help would be appreciated.

like image 950
fraxture Avatar asked Feb 25 '15 12:02

fraxture


2 Answers

I was missing a bracket after the transform option. This works:

module.exports = function(grunt) {

  grunt.config.set('browserify', {
    dev: {
      src: 'assets/js/main.jsx',
      dest: '.tmp/public/js/main.js',
      options: {
        debug: true,
        extensions: ['.jsx'],
        transform: [
          [ 'reactify', {'es6': true} ]
        ]
      }
    }
  });

  grunt.loadNpmTasks('grunt-browserify');
};
like image 193
fraxture Avatar answered Sep 25 '22 12:09

fraxture


Alternatively, you can also compile ES6 modules simply (without Grunt/Gulp) by using watchify.

In package.json, add the following:

{
  "scripts": {
    "build": "watchify -o build/bundle.js -v -d assets/js/main.jsx"
  },
  "devDependencies": {
    "browserify": "^10.2.4",
    "envify": "^3.4.0",
    "reactify": "^1.1.1",
    "watchify": "^3.2.2"
  },
  "browserify": {
    "transform": [
      ["reactify", {"es6": true}],
      "envify"
    ]
  }
}

In your terminal/command prompt, run npm run-script build.

like image 25
Jon Saw Avatar answered Sep 21 '22 12:09

Jon Saw