cssbundling-rails icon indicating copy to clipboard operation
cssbundling-rails copied to clipboard

Can't import gem vendored Sass stylesheets

Open sedubois opened this issue 2 years ago • 7 comments

When following the sass-rails to cssbundling-rails upgrade instructions (step 2 of the guide), the default build:css script fails for (legacy) imports of stylesheets embedded within Ruby gems (within their vendor/assets/stylesheets), e.g. @import "foundation/functions";. There is an error Error: Can't find stylesheet to import.

How can gem vendored Sass stylesheets be made available to the sass CLI?

sedubois avatar Mar 02 '22 12:03 sedubois

I have also used a small gem flag-icons-rails with Rails 4 which provides the country flags as images and a few scss styles for them.

It is implemented as a rails::Engine and adds the paths to assets:path:

module FlagIconsRails
  module Rails
    class Engine < ::Rails::Engine
      initializer 'flag-icons-rails.assets.precompile' do |app|
        %w[stylesheets images].each do |sub|
          app.config.assets.paths << root.join('app', 'assets', sub).to_s
        end
      end
    end
  end
end

adding @import "flag-icon" to app/assets/stylesheets/application.bootstrap.scss give only the error: Can't find stylesheet to import when running rake assets:precompile

As far as I understand rake assets:precompile calls yarn sass which is a Javascript node module, thus has nothing to do with rails or Rails:Engine. ?!?

( BTW the RailsGuides does not mention yarn at all )

So how can I include SASS stylesheets and images from a gem in Rails 7 ?

KDGundermann avatar Apr 26 '22 11:04 KDGundermann

Using normal CSS files from gems with url seems to work, e.g. @import url(foo/bar.css), where you can basically place anything in the url helper that rake assets:reveal outputs (at least when using Propshaft). The file is put trough the compiler of propshaft, and the file will be updated with the digest in its path, e.g. url("/assets/foo/bar-48af5572e05314eee7ab2f264966821632ea3913.css");.

However this only works for vendored CSS, as this will just trigger another GET request from the browser to fetch the other stylesheet, which then is applied by the browser. Using this for SASS is not possible, as the browser can't use SASS.

I tried changing the Propshaft::Compilers::CssAssetUrls class, such that SASS assets are inlined instead of imported by URL, however the compile step of propshaft is called after the yarn build:css step, i.e. it's operating on compiled CSS and as such this did not work.

Adding each gem to the load path isn't really that useful, is there really no other way to use the vendored SASS files?

Adrian-Hirt avatar Jun 29 '22 11:06 Adrian-Hirt

I have a node_module referencing another modules sass file and sass won't compile because it cant find file. (yes the package is there. no I can't edit the third party import.

@import 'node_modules/scut/dist/scut'

here's the cli line

"build:css": "sass ./app/assets/stylesheets/application.sass.scss ./app/assets/builds/application.css --no-source-map --load-path=node_modules

MrNagoo avatar May 25 '23 20:05 MrNagoo

Using normal CSS files from gems with url seems to work, e.g. @import url(foo/bar.css), where you can basically place anything in the url helper that rake assets:reveal outputs (at least when using Propshaft). The file is put trough the compiler of propshaft, and the file will be updated with the digest in its path, e.g. url("/assets/foo/bar-48af5572e05314eee7ab2f264966821632ea3913.css");.

However this only works for vendored CSS, as this will just trigger another GET request from the browser to fetch the other stylesheet, which then is applied by the browser. Using this for SASS is not possible, as the browser can't use SASS.

I tried changing the Propshaft::Compilers::CssAssetUrls class, such that SASS assets are inlined instead of imported by URL, however the compile step of propshaft is called after the yarn build:css step, i.e. it's operating on compiled CSS and as such this did not work.

Adding each gem to the load path isn't really that useful, is there really no other way to use the vendored SASS files?

I found that there's no such thing as "rake assets:reveal"

mices avatar Jun 03 '23 05:06 mices

I have a node_module referencing another modules sass file and sass won't compile because it cant find file. (yes the package is there. no I can't edit the third party import.

@import 'node_modules/scut/dist/scut'

here's the cli line

"build:css": "sass ./app/assets/stylesheets/application.sass.scss ./app/assets/builds/application.css --no-source-map --load-path=node_modules

We have the same issue. Any updates on this?

jnsntm avatar Jun 11 '24 09:06 jnsntm

I have a node_module referencing another modules sass file and sass won't compile because it cant find file. (yes the package is there. no I can't edit the third party import. @import 'node_modules/scut/dist/scut' here's the cli line "build:css": "sass ./app/assets/stylesheets/application.sass.scss ./app/assets/builds/application.css --no-source-map --load-path=node_modules

We have the same issue. Any updates on this?

I ended up switching to dartsass-rails and not use cssbundling-rails. dartsass:build has no issues.

MrNagoo avatar Jun 25 '24 18:06 MrNagoo

@MrNagoo I was using dartsass-rails but couldn't get my image_urls to work in my scss files. I tried to move to cssbundling-rails but it's been a nightmare and I can't get any of my variable imports (previously working with @use "config"; for example) to work. How do you do image urls with dartsass-rails?

agirlnamedsophia avatar Sep 13 '24 15:09 agirlnamedsophia