sass/sassc-rails

Problems when other gems depend on sass-rails

constantm opened this issue ยท 17 comments

I've been struggling to get sassc to work with Rails for quite some time but with little luck. Tonight I thought I'd give it a try again, but a quick Google lead me to this repo. Woo!

Unfortunately I can't see any sort of increase in the speed that my scss files compile after changing them. Is there some extra configuration necessary after installing the gem?

Hey! This gem is very much a work in progress. Your mileage may vary.

It seems that your project isn't using sassc-rails to compile. Could you try some of the following:

  • remove sass-rails
  • after bundling, double check that sass-rails is not present
  • remove cache assets folder
  • spring stop? restart server?

Had a quick look and it seems that sass-rails is still getting included because of ActiveAdmin's dependency on it. I forked ActiveAdmin and replaced the dependencies on sass-rails with sassc and it works perfectly now. Obviously that's not the best way to go about doing things - is there some way that you're aware of to do this in a more sustainable way?

@constantm I've released version 0.0.6, which I believe addressed this problem. If you have both installed, sassc-rails should override sass-rails. Please give it a shot and let me know how it goes!

I had to revert the changes here, i'm trying this out:

in an initializer: config/initializers/sassc_rails.rb

Rails.application.assets.register_engine '.sass', SassC::Rails::SassTemplate
Rails.application.assets.register_engine '.scss', SassC::Rails::ScssTemplate

I ran into some more issues with this. I had to add the following to the initializer in the previous comment:

# config/initializers/sassc_rails.rb

require "sprockets/engines"

module Extensions
  module Sprockets
    module Engines
      def register_engine(ext, klass)
        return if [
          Sass::Rails::SassTemplate,
          Sass::Rails::ScssTemplate
        ].include?(klass)

        super
      end
    end
  end
end

Sprockets::Base.send(:prepend, Extensions::Sprockets::Engines)

Rails.application.assets.register_engine '.sass', SassC::Rails::SassTemplate
Rails.application.assets.register_engine '.scss', SassC::Rails::ScssTemplate

i'd welcome anyone else's thoughts on a more permanent solution - do we simply include the about in the sassc-rails railtie?

@bolandrm here you go :)

Testing against my rails project, this issue does not seem to be resolved yet.

I can help to investigate. In my project the problem was that a sass rails initializer was setting up sprocket engines.

In your case were sprocket engines pointing to sass instead of sassc? If so, than can you check separately whether sass-rails initializer was ran or not.

If not then you need to track where they're registered from (like you did with your monkeypatch of register_engine) .
If yes, then tell me what rails version do you use? And then I'll check whether a different railtie loading mechanism is used there compared to latest rails.

The workaround doesn't help for me, btw. I've placed it in config/initializers, register_engine never gets called for Sass::Rails.

I have a cleaner workaround to propose:

# Gemfile
gem 'sass-rails', require: false
gem 'sassc-rails'

This ensures that sass-rails is not automatically require'd, making sassc-rails take over. This of course won't work if a gem explicitly requires sass-rails.

updated to this:

# config/initializers/sassc_rails.rb

require "sprockets/engines"

module Extensions
  module Sprockets
    module Engines
      def register_engine(ext, klass)
        return if [
          Sass::Rails::SassTemplate,
          Sass::Rails::ScssTemplate
        ].include?(klass)

        super
      end
    end
  end
end

Sprockets::Base.send(:prepend, Extensions::Sprockets::Engines)

# Rails.application.assets.register_engine '.sass', SassC::Rails::SassTemplate
# Rails.application.assets.register_engine '.scss', SassC::Rails::ScssTemplate

Rails.application.config.assets.configure do |env|
  env.register_engine '.sass', SassC::Rails::SassTemplate
  env.register_engine '.scss', SassC::Rails::ScssTemplate
end

@bolandrm last comment
and

gem 'sass-rails', require: false
gem 'sassc-rails', github: 'sass/sassc-rails'

worked fine

Hi! Has a permanent fix for this issue been made? I really want to use SassC in my project!

config/initializers/sassc_rails.rb does not work for me. It has conflicts with slim-rails. Since in my project the only gem that requires sass-rails is activeadmin, I updated the dependency of activeadmin and it works really well: activeadmin with sassc-rails

jmdfm commented

register_engine has been deprecated in Sprockets 3, so I had to use the following initializer to work with Rails 5 and Sprockets 4:

Rails.application.config.assets.configure do |env|
  env.register_mime_type 'text/css', extensions: ['.scss'], charset: :css
  env.register_mime_type 'text/css', extensions: ['.css.scss'], charset: :css
  env.register_preprocessor 'text/css', SassC::Rails::ScssTemplate
end

The initializer isn't working for me. I get an error: uninitialized constant SassC

Any idea how I could fix this ? I would really like to use sassc.