Use case #1: compiling scss files
I like using scss files, useful for class nesting, variables, and few other things. Hanami 1 did that out of the box IIRC, so I wanted to be able to do it in Hanami 2, while keeping the official assets pipeline
After a few search in the guide + forum + a bit of ChatGPT, here is what works for me:
- Add the dedicated plugin for esbuild (this will update package.json)
npm install --save-dev esbuild-sass-plugin sass
- Load the plugin for asset compilation in
app/config/assets.js
(orslices/your_slice/config/assets.js
if you want to configure one specific slice only)
import * as assets from "hanami-assets";
import { sassPlugin } from "esbuild-sass-plugin";
await assets.run({
esbuildOptionsFn: (args, esbuildOptions) => {
// Modify the esbuild options to include the sass plugin
esbuildOptions.plugins = [
...(esbuildOptions.plugins || []), // preserve any existing plugins
sassPlugin() // add sass plugin
];
return esbuildOptions;
}
});
- Import the scss file in the
app/assets/js/app.js
file (orslices/your_slice/assets/js/app.js
if you’re in a specific slice)
import "../css/app.css";
import "../css/your_file.scss";
This part was the most counter-intuitive for me as I was handling some stylesheet files, but I understand that it’s part of the compilation pipeline
With that in place, your main app.css compiled file, that you can load in your layout with <%= stylesheet_tag "app" %>
will have the content of both app.css
and your_file.scss
files