/webpack_rails_demo

Setting up webpack with Ruby on Rails: a basic demo

Primary LanguageRuby

#Webpack with Rails Demo

This demonstration was written to go with the related blog post, Setting Up Webpack With Rails

HOW TO SET UP WEBPACK WITH RAILS:

  1. Set up your initial Rails project as usual. 1
  2. npm init to generate your package.json
  3. sudo npm install webpack --save to install webpack, and similarly install any other libraries or webpack loaders you want. 2-4
  4. Decide whether to check your dependencies into version control.
  5. Write your webpack.config. 5 (Documentation here.)
  6. In app/assets/javascripts/application.js change the require statements to require your bundled JS file instead of requiring all files in the directory:
require_tree . => require main.bundle
  1. Whatever script initializes your JS application, (often init.js or index.js or my_app.js), require that in your webpack entry point.
  2. Use CommonJS or AMD statements to load dependencies in your JS files. 6-8
  3. webpack -wc to generate or re-generate your bundled JS. 9

To run the project locally (assuming you have Ruby 2.1.1 and Rails 4):

  • bundle
  • rails s