Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sprockets 4.0: alternative CSS processors? #661

Closed
dometto opened this issue Dec 30, 2019 · 9 comments
Closed

Sprockets 4.0: alternative CSS processors? #661

dometto opened this issue Dec 30, 2019 · 9 comments

Comments

@dometto
Copy link
Contributor

dometto commented Dec 30, 2019

Is it still possible to use a different CSS processor than SASSC on Sprockets 4.0? It looks like Sprockets will try to load sassc even when an alternative processor is specified. When I try to use either :yui or :sass, I get the following error:

2019-12-30 16:47:40 - LoadError - no such file to load -- sassc:
	org/jruby/RubyKernel.java:970:in `require'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/autoload/sassc.rb:2:in `<main>'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/sassc_processor.rb:1:in `(root)'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/sassc_processor.rb:42:in `initialize'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/sassc_processor.rb:27:in `instance'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/sassc_processor.rb:31:in `call'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/processor_utils.rb:84:in `block in call_processor'
	org/jruby/RubyArray.java:1885:in `reverse_each'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/processor_utils.rb:66:in `call_processors'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/processor_utils.rb:65:in `block in call_processors'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/processor_utils.rb:22:in `CompositeProcessor'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/processor_utils.rb:33:in `call'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/processor_utils.rb:84:in `block in call_processor'
	org/jruby/RubyArray.java:1885:in `reverse_each'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/processor_utils.rb:66:in `call_processors'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/processor_utils.rb:65:in `call_processors'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/loader.rb:182:in `block in load_from_unloaded'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/loader.rb:59:in `load'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/loader.rb:335:in `fetch_asset_from_dependency_cache'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/loader.rb:43:in `load'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/cached_environment.rb:44:in `load'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/bundle.rb:27:in `call'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/processor_utils.rb:84:in `block in call_processor'
	org/jruby/RubyArray.java:1885:in `reverse_each'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/processor_utils.rb:66:in `call_processors'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/processor_utils.rb:65:in `call_processors'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/loader.rb:182:in `block in load_from_unloaded'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/loader.rb:59:in `load'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/loader.rb:335:in `fetch_asset_from_dependency_cache'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/loader.rb:43:in `load'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/cached_environment.rb:44:in `load'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/base.rb:81:in `find_asset'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/environment.rb:31:in `find_asset'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-4.0.0/lib/sprockets/base.rb:119:in `[]'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-helpers-1.2.1/lib/sprockets/helpers.rb:351:in `find_asset_path'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-helpers-1.2.1/lib/sprockets/helpers.rb:145:in `asset_path'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-helpers-1.2.1/lib/sprockets/helpers.rb:158:in `asset_tag'
	~/.rvm/gems/jruby-9.2.0.0@gollum/gems/sprockets-helpers-1.2.1/lib/sprockets/helpers.rb:180:in `block in stylesheet_tag'

Sprockets environment:

  module Assets
    MANIFEST = %w(app.js editor.js app.css criticmarkup.css fileview.css ie7.css print.css *.png *.jpg *.svg *.eot *.ttf *.woff *.woff2)
    ASSET_URL = 'gollum/assets'
    
    def self.sprockets(dir = File.dirname(File.expand_path(__FILE__)))
      env = Sprockets::Environment.new
      env.append_path ::File.join(dir, 'public/gollum/stylesheets/')
      env.append_path ::File.join(dir, 'public/gollum/javascript')
      env.append_path ::File.join(dir, 'public/gollum/images')
      env.append_path ::File.join(dir, 'public/gollum/fonts')

      env.js_compressor  = :uglify
      env.css_compressor = :yui

      env
    end
  end
end

Example app

As you can see I'm trying to get this to run with gollum, which uses sinatra and not Rails. Example branch: https://github.com/repotag/gollum/tree/sprockets-example

To run: bundle install && bundle exec gollum path/to/repo, visit http://127.0.0.1:4567

@ahorek
Copy link
Contributor

ahorek commented Jan 9, 2020

in sprockets 4 sassc is the default transformer. sass gem should still work, but you have to register it manually 33d0a6f

note that sass gem is no longer maintained https://sass-lang.com/blog/ruby-sass-is-deprecated

:yui is just a compressor, but you still need a sass > css transformer, an alternative could be https://github.com/sass/dart-sass but there's no integration for ruby (yet)

for now, sassc is the only reasonable option

@ahorek
Copy link
Contributor

ahorek commented Aug 26, 2021

I guess, there's no (at least clean) way to replace it entirely

hacky ways

note that sprockets gem is not maintained anymore, so even if you fix the code by yourself, there's little chance I'll ever be released.

if your intention is just to switch back to the original sass gem, just forget about it, it doesn't worth the effort.

@dmolesUC
Copy link

@ahorek I deleted my previous comment since I figured out that I was actually seeing asset caching, not a failure to recognize my registered processor. Calling Sprockets.register_transformer in config/initializers/assets.rb worked, after rails assets:clobber and rm -r tmp/cache.

Thanks for the information that Sprockets isn't maintained any more — I knew that JavaScript integration in Rails had moved over to Webpack but most of the Rails documentation and discussion I've seen seems to at least imply that Sprockets is still the preferred solution for CSS. (My one attempt at using Webpack for both I found it so brutally slow to recompile SCSS in development that I was hoping to switch back.)

At any rate, knowing that, I'm not likely to put any more effort into my sass-embedded implementation, but for anyone who should happen to come along, it's in this gist and it's MIT-licensed, so feel free to take it and run with it. It works for very simple cases (self-contained SCSS in app/assets/stylesheets) but probably won't work with anything that can't be resolved relatively due to sass-contrib/sass-embedded-host-ruby#2.

@ahorek
Copy link
Contributor

ahorek commented Aug 26, 2021

WOW, thank you for letting me know about this project! Someone finally did it. A complete wrapper in Sprockets should be trivial and this could be a real maintained alternative to the sassc gem.
I'm not sure if Sprockets maintainers will be willing to include it. Even if Sprockets wasn't officially abandoned (it's still part of Rails), development is silent for years. But I'll look into this, it's promising.

btw: I did a quick benchmark, dart-sass is about 20% slower than sassc, which is expected and actually better than I thought.

@dmolesUC
Copy link

dmolesUC commented Sep 1, 2021

@ahorek For my gist to really to become a first-class implementation, somebody would have to go through the dart-sass configuration options and map them to Sprockets configs, make sure more complex scenarios work like importing SCSS from vendor stylesheets (e.g. bootstrap, jQuery, whatever), etc. I don't have the bandwidth to do that, but if this is useful to someone who does, that's great.

Also sass-contrib/sass-embedded-host-ruby#2 is fixed now, so that's good. I think that project is where most of the real work is that makes this possible, honestly, and it's only a few months old, so I'm not surprised the Sprockets implementation hasn't been done till now.

@ntkme
Copy link
Contributor

ntkme commented Apr 4, 2022

A drop-in solution that replaces sassc with sass-embedded (dart-sass-embedded): https://github.com/ntkme/sassc-embedded-shim-ruby

@jacob-carlborg-apoex
Copy link

If you want to use Sass, you can use https://github.com/rails/dartsass-rails. Seems to be the new preferred way to integrate Sass into a Rails application. I ran into the same issue when trying to use dartsass-rails. The solution was to remove any referenced CSS files from the manifest file (app/assets/config/manifest.js), see rails/dartsass-rails#12.

@rafaelfranca
Copy link
Member

Yes, You can register alternative CSS processors, you just need to do it using https://github.com/rails/sprockets/blob/main/lib/sprockets.rb#L172-L173

@rafaelfranca rafaelfranca closed this as not planned Won't fix, can't repro, duplicate, stale Jun 23, 2022
@dometto
Copy link
Contributor Author

dometto commented Aug 21, 2022

A drop-in solution that replaces sassc with sass-embedded (dart-sass-embedded): https://github.com/ntkme/sassc-embedded-shim-ruby

Thanks @ntkme!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants