1

I'm using the simple_calendar gem and it's working great. I'm having trouble getting it to import the stylesheet by doing

application.css

*= require simple_calendar

or

application.tailwind.css

@import "simple_calendar";

I see that the css file exists in the gem at https://github.com/excid3/simple_calendar/blob/main/app/assets/stylesheets/simple_calendar.scss and I can get it work by copying and pasting this into my own css file but I'm hoping I can import it instead.

1
  • Do you know about import maps in rails 7 like rails 7 introduced importmaps to compile assets like javascript or css? Commented Sep 5, 2024 at 6:28

1 Answer 1

1

For such a small sass file you'll be better off just copy-pasting it, you'll want to customize the looks anyway.


dartsass-rails

This is the official rails 7 way of compiling sass files before sprockets picks it up as plain css.

bundle add dartsass-rails
bin/rails dartsass:install  
// app/assets/application.scss

@import "simple_calendar";

You have to run bin/dev or bin/rails dartsass:watch to compile application.scss to app/assets/builds/application.css which is then used by sprockets.


dartsass-sprockets

This is an unofficial replacement of the deprecated sassc-rails, which lets you use sprockets directives as normal for sass files:

bundle add dartsass-sprockets
/* app/assets/application.css */

/*
 *= require simple_calendar
 */

Tailwind

Last time I checked there is no easy way to import files from gems into tailwind, because it is not aware of any asset paths from sprockets. You can use relative and absolute paths:

/* app/assets/application.tailwind.css */

@import "/home/alex/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/simple_calendar-3.0.4/app/assets/stylesheets/simple_calendar.scss";

This could be made better with symlinks:

# config/initializers/gem_assets.rb

%w[simple_calendar].each do |name|
  system "mkdir -p app/assets/gems/#{name}"
  system "ln -snf `bundle show #{name}`/app/assets app/assets/gems/#{name}/assets"
end

# don't need this to be in asset paths again
Rails.application.config.after_initialize do
  Rails.configuration.assets.paths.delete Rails.root.join("app/assets/gems").to_s
end

which will symlink assets directory from simple_calendar:

app/
└─ assets/
   └─ gems/
      └─ simple_calendar/
         └─ assets/ -> /home/alex/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/simple_calendar-3.0.4/app/assets/
            └─ stylesheets/
               └─ simple_calendar.scss

which is way better than an absolute path with hardcoded versions:

/* app/assets/application.tailwind.css */

@import "../gems/simple_calendar/assets/stylesheets/simple_calendar.scss";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.