Custom CSS not working with CSS Bundling for Rails 7

Playing around with Rails 7 and I don't understand why my custom CSS is not working.

I built new rails app with flag for Bootstrap, which is working fine (CSS and JS, tested with bootstrap modal). These are my default config files:

application.js

// Entry point for the build script in your package.json
import "@hotwired/turbo-rails"
import "./controllers"
import * as bootstrap from "bootstrap"

application.bootstrap.scss

@import 'bootstrap/scss/bootstrap';

package.json

{
  "name": "app",
  "private": "true",
  "dependencies": {
    "@hotwired/stimulus": "^3.0.1",
    "@hotwired/turbo-rails": "^7.1.0",
    "@popperjs/core": "^2.11.2",
    "bootstrap": "^5.1.3",
    "esbuild": "^0.14.23",
    "jquery": "^3.6.0",
    "popper.js": "^1.16.1",
    "sass": "^1.49.9",
    "stimulus": "^3.0.1"
  },
  "scripts": {
    "build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds",
    "build:css": "sass ./app/assets/stylesheets/application.bootstrap.scss ./app/assets/builds/application.css --no-source-map --load-path=node_modules"
  }
}

And I can built CSS in /builds/application.css


Now I want to add custom CSS. This is my process:

  1. Added new file stylesheets/custom.css, with css:
    .my-class {
      color: #fff;
      background-color: #00eb00;
    }
  1. Add import to application.bootstrap.scss

    @import "custom";

  2. yarn run build:css

  3. And now I can see .my-class in builds/application.css

But when I try to use id in HTML, no CSS is added. Why? Should I place it somewhere else?

EDIT: I got it running, but only when I run manually rails assets:precompile and then bin/dev. Why do I need to precompile every time I change something?

1 answer

  • answered 2022-03-30 00:36 Tom

    It worked for me if I added

    //= link custom.css
    

    to app/assets/config/manifest.js

    And I didn't add @import "custom"; to application.bootstrap.scss

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum