1

Rails 4.1+, so there isn't built-in support for an :assets group

I want to keep Heroku's precompile on push behaviour, but don't want the asset gems loaded by the rails server. We don't use any kind of inline coffeescript or scss template rendering in the app, only in the assets, so it's just wasted memory at runtime.

I've played around with extending the rake task, configuring sprocket-rails, and even changing application.js to application.js.erb and adding things like

//= <% require 'jquery-rails' %> 
//= require 'jquery'

but still get these errors:

Sprockets::FileNotFound: couldn't find file 'jquery'

This comment from a rails issue complaining about the same problem also doesn't work for me now, and it's a couple of years old so perhaps something changed that breaks it? Or am I doing it wrong?

If I keep the asset gems in the default Gemfile group everything works fine.

The point here is to not have them loaded in the production environment, but to have

RAILS_ENV=production rake assets:precompile task 

load them before it executes (and fails because of missing libraries)

nruth
  • 1,068
  • 7
  • 22
  • Can you add `require: false` to the gem declaration, then explicitly `require 'jquery-rails'` in your modified rake task? – kevinthompson Jun 27 '15 at 18:19
  • Yeah that seems like it should work, but it didn't work for me. It seems like sprockets ignores anything I load. I'm interested to hear if anyone else has it working. – nruth Jun 27 '15 at 19:26
  • 2
    the following seems to work if not for heroku on-push compile: posts from [rails issue tracker](https://github.com/rails/rails/commit/49c4af43ec5819d8f5c1a91f9b84296c927ce6e7#commitcomment-4449619) 1. reinstate the assets group in your gemfile, and put jquery-rails etc in there 2. compile with RAILS_ENV=production RAILS_GROUPS=assets bundle exec rake assets:precompile 3. restore Bundler.require(*Rails.groups(assets: %w[development test])) in config/application.rb – nruth Jun 27 '15 at 20:17
  • sprockets seems to build its own environment (whatever that is) and [has some configuration options](https://github.com/rails/sprockets-rails), but I couldn't get them to work. The docs aren't written, but it says "config.assets.configure Invokes block with environment when the environment is initialized. Allows direct access to the environment instance and lets you lazily load libraries only needed for asset compiling.". – nruth Jun 27 '15 at 20:19
  • Did you eventually had this working? I'm trying to achieve the very same thing. – dgilperez Nov 07 '18 at 16:49
  • No I'm afraid not. I imagine the assets story is quite different these days, but comments in the github issue link to these SO questions: https://stackoverflow.com/questions/39756206/remove-development-gems-from-production-with-bundler-and-rails-4 https://stackoverflow.com/questions/16406204/why-did-rails4-drop-support-for-assets-group-in-the-gemfile#17221248 – nruth Nov 08 '18 at 17:12

0 Answers0