37

I'm migrating a Rails 5.2 app to 6.0 for ActionMailbox, ActionText, and multiple databases. However, I don't know webpack and would like to use Sprockets instead.

How do I properly remove webpack from Rails 6 and install Sprockets? rails new app installs webpack files right away. Is there a way to default to sprockets easily?

Rails (Ruby) was supposed to be convention over configuration, but Webpack (Javascript) cancels this by adding a lot of config and complexity.

Jun Dalisay
  • 1,165
  • 2
  • 12
  • 26

2 Answers2

46

I found this link helpful, I will crosspost here:

Use option --skip-webpack-install when generating a new app

Change Gemfile, remove webpacker, add:

gem 'sass-rails', '>= 5'
gem 'sprockets', '~> 4'
gem 'sprockets-rails', :require => 'sprockets/railtie'

Then

bundle update sass-rails sprockets # if you are updating 
bundle install # or simply install

If you are using sprockets 4, change app/assets/config/manifest.js to:

//= link_tree ../images
//= link application.js
//= link application.css

If you are using sprockets 3, add to config/initializers/assets.rb:

# Rails.application.config.assets.precompile += %w( application.js )

Restore app/assets/javascripts/application.js:

//= require rails-ujs
//= require turbolinks
//= require_tree .

Change javascript_pack_tag to javascript_include_tag in app/views/layout/application.html.erb

eikes
  • 4,811
  • 2
  • 31
  • 31
  • Works for me (https://github.com/ecovillage/cabler/commit/c0329c77613faaf747004986e2df1eb5b5cb35b8) – Felix Feb 17 '20 at 08:13
  • 5
    One thing to note: if using the `--skip-keeps` option when generating the Rails app, it doesn't create the `app/assets/images` folder, which crashes Sprockets without mentioning why. Running ```touch app/assets/images/.keep``` fixes that. I wasted a few hours on that hurdle! – Goulven Feb 20 '20 at 15:53
  • This doesn't work for me, since I have an `application.scss` file. Here is my manifest.js: ```//= link_tree ../images //= link_directory ../javascripts .js //= link_directory ../stylesheets .scss //= link application.scss``` – drweird Apr 01 '20 at 18:33
16

If you want to skip adding webpacker gem when generating new rails application, use --skip-javascript (since Webpacker the default JavaScript compiler for Rails 6).

as noted from this reference

widjajayd
  • 6,090
  • 4
  • 30
  • 41