Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 6: How to disable Webpack and use Sprockets instead

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.

like image 329
Jun Dalisay Avatar asked Mar 18 '19 02:03

Jun Dalisay


People also ask

What are rails Sprockets?

Sprockets is a Ruby library for compiling and serving web assets. Sprockets allows to organize an application's JavaScript files into smaller more manageable chunks that can be distributed over a number of directories and files.


1 Answers

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

like image 96
eikes Avatar answered Oct 02 '22 21:10

eikes