#Webpack with Rails Demo
This demonstration was written to go with the related blog post, Setting Up Webpack With Rails
- Set up your initial Rails project as usual. 1
npm init
to generate yourpackage.json
sudo npm install webpack --save
to install webpack, and similarly install any other libraries or webpack loaders you want. 2-4- Decide whether to check your dependencies into version control.
- Write your webpack.config. 5 (Documentation here.)
- 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
- Whatever script initializes your JS application, (often
init.js
orindex.js
ormy_app.js
), require that in your webpack entry point. - Use CommonJS or AMD statements to load dependencies in your JS files. 6-8
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