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

Can't import gem vendored Sass stylesheets #81

Open
sedubois opened this issue Mar 2, 2022 · 7 comments
Open

Can't import gem vendored Sass stylesheets #81

sedubois opened this issue Mar 2, 2022 · 7 comments

Comments

@sedubois
Copy link
Contributor

sedubois commented Mar 2, 2022

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?

@KDGundermann
Copy link

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 ?

@Adrian-Hirt
Copy link

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?

@MrNagoo
Copy link

MrNagoo commented May 25, 2023

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

@mices
Copy link

mices commented Jun 3, 2023

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"

@jnsntm
Copy link

jnsntm commented Jun 11, 2024

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?

@MrNagoo
Copy link

MrNagoo commented Jun 25, 2024

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.

@agirlnamedsophia
Copy link

@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?

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

7 participants