Skip to content

Sprockets error when using additional CSS files #117

@olivier-thatch

Description

@olivier-thatch

The README says:

you should also consider simply referring to your other CSS files directly, instead of bundling them all into one big file. It's better for caching, and it's simpler to setup. You can refer to other CSS files by expanding the stylesheet_link_tag in application.html.erb like so: <%= stylesheet_link_tag "application", "other", "styles", "data-turbo-track": "reload" %>.

Here's what I did:

  1. Create a new Rails app with rails new my_app -j esbuild --css tailwind
  2. Create a new CSS file app/assets/stylesheets/foo.css
  3. Change the stylesheet_link_tag in application.html.erb like so: <%= stylesheet_link_tag "application", "foo", "data-turbo-track": "reload" %>

This causes a Sprockets::Rails::Helper::AssetNotPrecompiledError exception with the following error message:

Asset `more.css` was not declared to be precompiled in production.
Declare links to your assets in `app/assets/config/manifest.js`.

  //= link foo.css

and restart your server

I can fix the error by editing manifest.js to include the additional stylesheet, e.g. with //= link_directory ../stylesheets .css, but is this the correct approach? The cssbundling-rails gem explicitly removes this line from manifest.js as part of its install task:

say "Stop linking stylesheets automatically"
gsub_file "app/assets/config/manifest.js", "//= link_directory ../stylesheets .css\n", ""

I realize this is probably more of an issue with Sprockets rather than with cssbundling-rails itself, but the boundary's not very clear. What is the correct approach to add additional CSS files when using cssbundling-rails?

  1. Re-add //= link_directory ../stylesheets .css to manifest.js?
  2. Add only the additional CSS files, e.g. with //= link ../stylesheets/foo.css?
  3. Something else?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions