#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 initto generate yourpackage.jsonsudo npm install webpack --saveto 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.jschange 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.jsorindex.jsormy_app.js), require that in your webpack entry point. - Use CommonJS or AMD statements to load dependencies in your JS files. 6-8
webpack -wcto generate or re-generate your bundled JS. 9
To run the project locally (assuming you have Ruby 2.1.1 and Rails 4):
bundlerails s