Including ES6 in Rails applications can be very useful. Using JavaScript packages that are only available/maintained in npm has traditionally been a problem with Rails application. This documents an approach chosen among a couple of different options.
We settled upon sprockets-commoner over webpack and browserify because it integrates with the Rails asset pipeline and least modifies the natural development of JavaScript components.
-
add
'expose window.[component_name]'to the top of ES6 class files- if class needs to be available on
window, seereact-railsexample below
- if class needs to be available on
-
add
gem 'sprockets-commoner'to Gemfile -
add
.babelrcto project root-
add other presets as necessary (eg. react)
{ presets: [ "es2015" ] }
-
-
add NPM dependencies to
package.json(including react-dom, react-chartjs, etc.)"dependencies": { "babel-core": "^6.14.0", "babel-generator": "^6.14.0", "babel-helpers": "^6.8.0", "babel-polyfill": "^6.7.4", "babel-types": "^6.14.0", "babel-preset-es2015": "^6.9.0", "babel-runtime": "^6.6.1", }
-
add
app/assets/config/manifest.jsfile//= link_tree ../images //= link_directory ../javascripts .js //= link_directory ../stylesheets .css
-
rename .jsx files to .js
-
move propTypes out of class, and change syntax to
[class_name].propTypes = { ... } -
modify
application.js(NOTE: no space between last import and window.React)//= require jquery //= require turbolinks //= require react_ujs // require_tree ./components import 'babel-polyfill' import React from "react" import ReactDOM from "react-dom" import DoughnutChart from './components/DoughnutChart' import MyForm from './components/MyForm' window.React = React window.ReactDOM = ReactDOM
- add imports for React, ReactDOM, individual components (if individually importing)
- set values on
windowfor React and ReactDOM require jqueryrequire react_ujs(if usingreact-railsgem and view helper)
-
add
gem 'react-rails'to Gemfile -
use
react-railshelper method in views= react_component('ScorecardsDoughnutChart', graph_data)
Note: When deploying to Heroku, you will need to turn on an additional buildpack for node.
- sprockets-commoner - serve assets
- react-rails - provides Rails helper
- Teaspoon works with mocha and jasmine
- add
gem 'teaspoon-mocha'andgem 'teaspoon-bundle'toGemfile(the latter is required becausesprockets-commonerdoes not recognize teaspoon by default, see here) - run
rails generate teaspoon:install - edit
teaspoon_env.rbto includesuite.boot_partial = 'bundle_boot' - rename all tests to end with
_spec.js - run teaspoon with
RAILS_ENV=test rake teaspoon - override default
teaspoonrake task to force RAILS_ENV=test, and ensure teaspoon tests run withrake