4

I am starting a new Rails 6 application. If I understood correctly, Webpack(er) (gem webpacker) has replaced Sprockets as the new standard for including/minifying JS (source). Furthermore, Rails 6 now both requires Node.js and Yarn.

Am I correct in assuming that the Node.js and Yarn dependencies are only due to the inclusion of Webpack, or do other components of Rails 6 also need them?

Are there any possible drawbacks in removing Webpack and Node and Yarn from the Rails 6 app and continuing to use the Rails Asset Pipeline (apart from missing Webpack features)?

chrisma
  • 262
  • 2
  • 11
  • 1
    Sprockets still relies on Node.js (or another JS runtime) to do stuff like CSS and JS compression. "Are there any additional drawbacks?". Well you're painting yourself into a corner as most gem packages of frontend stuff will cease to be maintained/updated. – max Oct 21 '20 at 15:24
  • Thanks for the answer! For CSS and JS compression it's possible to include a JS runtime like `mini_racer` or `therubyracer` (as was the case with Rails 5). Good point about the gem packages! – chrisma Oct 23 '20 at 09:15
  • The maintainence of sprockets itself is also a concern, I haven't heard that much about it but will it continue to be maintained and will it be compatible with future versions of Rails? – max Oct 23 '20 at 09:40
  • 1
    I know that DHH sees them coexisting but Webpacker can easily do CSS/SCSS and images which is really what Sprockets would still be used for and I can see why you would want to streamline and make it less confusing especially for beginners by just having a single pipeline. – max Oct 23 '20 at 09:43
  • That is exactly the point! I was hoping to make it easier/less complex for our students, as they would also not have to install node.js and yarn. – chrisma Oct 23 '20 at 09:47
  • There is a huge advantage for students in learning Yarn and installing Node though as a front-end package manager is really a fact of life in 2020. Node is also available as a point and click installer on most platforms and can be easier to setup then for example Rhino or V8 that therubyracer uses. The main issue really with webpacker in Rails 6 is that the documentation is dismal. – max Oct 23 '20 at 12:01

2 Answers2

9

It's possible to run Rails 6 without the webpacker gem, Node.js and Yarn (see this Rails issue).

However, the --skip-webpack-install option of Rails new still includes the webpackergem in the Gemfile and sets up the resulting project with webpacker configuration (only rails webpacker:install is not run).

If the Rails Asset Pipeline using Sprockets is to be used, the --skip-javascript option is recommended and manual changes are necessary, in particular:

  • Add <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> to /app/views/layouts/application.html.erb
  • Create /app/assets/javascripts/application.js (contents, e.g. here)
  • Add //= link_directory ../javascripts .js to app/assets/config/manifest.js
chrisma
  • 262
  • 2
  • 11
3

Yes, you can drop gems from Gemfile, delete the created node_modules folder and package.json file.

After that run bundle to clean up the Gemfile.lock and start code the old way with normal views and templates. Mention, that you will have to add js engine like Google V8.

In fact webpacker and node are not required to run a rails 6 application.

nbennke
  • 54
  • 5
  • Thanks for your answer! I've tried it out and Rails 6 seems to use node out of the box solly for JS compression, the old asset pipeline can still be used. I've opened a rails issue with some more details: https://github.com/rails/rails/issues/40438 – chrisma Oct 23 '20 at 09:44