CSS Bundler
Overviewâ
CM Admin includes a CSS bundling system that compiles Sass/SCSS stylesheets. The bundler handles both the core CM Admin stylesheets and your application's custom styles
The system is built around a shared helper module CmAdminCssBundler that provides common functionality for running yarn-based CSS build processes.
How It Worksâ
The CSS bundler follows these steps:
- Install dependencies â Runs
yarn installin the CM Admin engine directory - Execute build script â Runs the specified yarn script (e.g.,
build:css,watch:css) - Compile stylesheets â Uses Sass to compile SCSS files to CSS with optimized flags
Application Setupâ
Step 1 â Ensure application stylesheet existsâ
Create or update your main application stylesheet:
// app/assets/stylesheets/application.scss
NOTES:
- File should be scss
- No File on application should have, remove from
app/assets/stylesheets/application.scssandcm_admin/app/assets/stylesheets/cm_admin/custom.css
*= require_tree .
*= require_self
- you can include any other stylesheets using
@importdirective in application.scss, it is the entry point for all your stylesheets.
Step 2 â Remove sassc-rails gemâ
Remove the sassc-rails gem from your Gemfile since CM Admin's CSS bundler handles Sass compilation:
# Gemfile
# Remove this line
gem 'sassc-rails'
Then run bundle install to update your dependencies.
Step 3 â Remove stylesheet inclusion from manifest.jsâ
// app/assets/config/manifest.js
//= link_directory ../stylesheets .css
Step 4 â Add CSS build to manifest.jsâ
// app/assets/config/manifest.js
//= link_tree ../builds
Step 5 - Add .keep file to builds directoryâ
# Create .keep file to ensure directory is tracked by git
touch app/assets/builds/.keep
Step 6 - Update gitignoreâ
/app/assets/builds/*
!/app/assets/builds/.keep
Custom Layout CSS Filesâ
If your application uses multiple layouts (e.g., a separate layout for public-facing pages, landing pages, or embedded views), you can register additional SCSS files to be compiled alongside the default CM Admin stylesheets.
Configurationâ
Add the layout_css_files option in your CM Admin initializer:
# config/initializers/zcm_admin.rb
CmAdmin.configure do |config|
config.layout = 'admin'
config.layout_css_files = ['public.scss', 'landing.scss']
end
Each entry should be a path relative to app/assets/stylesheets/. For example, 'public.scss' maps to app/assets/stylesheets/public.scss.
How It Worksâ
During the CSS build, each file listed in layout_css_files is compiled from its source location to app/assets/builds/ with the same base name:
| Source | Output |
|---|---|
app/assets/stylesheets/public.scss | app/assets/builds/public.css |
app/assets/stylesheets/landing.scss | app/assets/builds/landing.css |
These files are compiled using the same Sass flags and load paths as the core CM Admin stylesheets, so you can use @import directives referencing CM Admin or node module dependencies.
Subdirectory Supportâ
You can also point to files inside subdirectories:
config.layout_css_files = ['layouts/public.scss', 'layouts/embedded.scss']
| Source | Output |
|---|---|
app/assets/stylesheets/layouts/public.scss | app/assets/builds/public.css |
app/assets/stylesheets/layouts/embedded.scss | app/assets/builds/embedded.css |
Note: The output file name is derived from the base name only, regardless of subdirectory depth.
Rake Commandsâ
Build Commandsâ
# Build all CSS files for production (already enhanced in assets:precompile rake task)
rake cm_admin:scss:build
These commands:
- Compile
cm_admin.css.scssâcm_admin.css - Compile
cm_session.css.scssâcm_session.css - Compile
application.scssâapplication.css - Place outputs in
app/assets/builds/
Watch Commandsâ
# Watch for changes and auto-recompile during development
rake cm_admin:scss:watch
These commands start the Sass compiler in watch mode, automatically recompiling when source files change.
Integration with Assets Precompileâ
The CSS build automatically runs during assets:precompile:
rake assets:precompile
This ensures your CSS is always up-to-date when deploying to production.
File Structureâ
your-app/
âââ app/assets/
â âââ builds/ # Compiled CSS output (generated)
â â âââ cm_admin/
â â â âââ cm_admin.css
â â â âââ cm_session.css
â â âââ application.css
Troubleshootingâ
Common Issuesâ
"yarn install failed"
- Ensure Node.js and Yarn are installed
- Check that the CM Admin engine directory exists and is accessible
Compiled CSS not updating
- Run
rake cm_admin:scss:buildmanually to force rebuild - Check file permissions in
app/assets/builds/