@hamur/css
The first-party CSS module. Bun's CSS bundler has no loader for Sass, Less or Stylus, so the module compiles .scss, .sass, .less and .styl files with the app's own compiler and hands the CSS to Bun, which goes on as for a .css file: @import, nesting, prefixes, minifying and the class names of a *.module.scss file. It also runs your PostCSS plugins (PostCSS) and compiles the style blocks of Vue and Svelte components. The Styling page shows how the pieces fit together; the sections below start with Sass and then cover Less and Stylus. Install it with the compiler, add it to modules and set its options under the cssCompilers key.
bun add @hamur/css sass
// hamur.config.ts
import css from "@hamur/css"
export default defineConfig({
modules: [css],
cssCompilers: { sass: { additionalData: '@use "#app/styles/vars" as *;', loadPaths: ["styles"] } },
})
default
The module, as defineModule({ meta: { name: "css", configKey: "cssCompilers" } }). Its setup checks cssCompilers, adds the Bun plugin hamur-css to the client and server builds and to both dev builds, and sets the style preprocessor that the vue and svelte adapters use for <style lang="scss"> and <style lang="sass">.
The compiler.
sass-embedded, elsesass, imported from the app's root when the first Sass file loads, never at setup; the modern API withstyle: "expanded"(Bun minifies). With neither installed, the build fails at that file:app/main.scss: @hamur/css needs "sass" or "sass-embedded" installed in the app.The builds. In the client build and in
hamur devan imported.scssor.sassfile (the indented syntax) is compiled to CSS; in the server build it is an empty module, as a.cssfile is.Imports.
@useand@importresolve relative to the file, then through the app'spackage.jsonimports(@use "#app/styles/vars"), thenpkg:through the compiler'sNodePackageImporter(@use "pkg:bootstrap/scss/bootstrap"), then from each ofloadPaths.Errors. A Sass error fails the build at the file, line and column where it is, which may be a partial, with the lines of
additionalDataleft out of the count:app/styles/_vars.scss:2:10: Undefined variable. 2 | color: $nope; | ^No source map. Bun ignores a source map from a plugin and writes none for CSS, so the compiled CSS does not map back to the
.scssfile; the error position above is how a mistake is found.hamur dev. An edit of a partial reaches the open page without a restart. The module keeps each result with the hash of the file, of every file the compiler read and ofadditionalData, so an edit of an unrelated file does not compile the stylesheet again.
Without the module, importing a Sass file fails the build with app/main.scss: Sass needs the @hamur/css module: install @hamur/css and sass and add "@hamur/css" to modules.
CssCompilers, StyleData
| Key | Type | Default |
|---|---|---|
sass | { additionalData?: StyleData; loadPaths?: string[] } | none: additionalData is prepended to each Sass file, on a line of its own; loadPaths are directories from the app's root that @use also looks in |
less, stylus | { additionalData?: StyleData; loadPaths?: string[] } | none: for .less and .styl files as for Sass, loadPaths being the compiler's paths; an @import of a path that starts with an imports key resolves through it |
postcss | boolean | { config?: string; plugins?: unknown[] } | unset: on when rootDir has postcss.config.js, .mjs or .ts; false ignores the file; config is that file's path from the root; plugins run in order instead of a file. PostCSS runs on each style of the app and its layers (.css and the output of the other compilers, Vue and Svelte style blocks) before Bun's CSS loader, never on node_modules |
StyleData is string | ((source: string, file: string) => string | Promise<string>): a function gets the file's text and absolute path and returns the text to prepend. The load errors are cssCompilers.css is not a compiler; use sass, less, stylus or postcss, cssCompilers.sass.additionalData must be a string or a function (got 1), cssCompilers.sass.loadPaths must be an array of strings (got "styles") and cssCompilers.postcss must be a boolean or { config?, plugins? } (got "yes").
Less and Stylus
.less files go through less and .styl files through stylus, each imported from the app's root when the first file of its language loads, with the same additionalData and loadPaths as Sass (loadPaths is the compiler's paths). An @import of a path that starts with a key of the app's package.json imports resolves through it. A compiler error names the file, the line and the column, as Sass's do. The missing-compiler errors are app/a.less: @hamur/css needs "less" installed in the app and app/b.styl: @hamur/css needs "stylus" installed in the app. Without the module in modules, the build says Less needs the @hamur/css module: install @hamur/css and less and add "@hamur/css" to modules (Stylus and stylus for .styl). A *.module.less or *.module.styl file compiles and then is a CSS module in Bun's bundler, in the client build.
PostCSS
cssCompilers.postcss is a transform of the build, not a loader: it runs after whichever plugin or loader produced the CSS, before Bun's CSS loader, and only on the client build. It handles .css, .scss, .sass, .less and .styl files and Vue and Svelte style blocks, so it also runs on the output of another build plugin. It skips node_modules and every file outside the app's and the layers' srcDir.
- On or off.
trueor an object turns it on. Unset, it is on exactly whenrootDirholdspostcss.config.js,.mjsor.ts.falseignores the file. - Where the plugins come from.
plugins(instances, run in order) replaces the file; otherwise the file's default export, an object or a function of{ env }that returns one, whosepluginsis an array or a map of package names to options, resolved from the app's root.configis the file's path from the root and is watched, so an edit restartshamur dev. - Errors.
postcssmust be installed:postcss.config.mjs: @hamur/css needs "postcss" installed in the app(without a file the message has no file name). A syntax error is reported at the file, line and column. A plugin the file names that is not installed ispostcss.config.mjs: plugin "x" is not installed in the app. - Logs. On with nothing to run:
[hamur] cssCompilers.postcss is on and has no plugins: add postcss.config.mjs or cssCompilers.postcss.plugins, once. A plugin that Bun's CSS bundler already covers (autoprefixer,postcss-nesting,postcss-preset-env,postcss-import) logs once[hamur] postcss plugin "autoprefixer" is not needed: Bun's CSS bundler already prefixes, and still runs.
Vue and Svelte style blocks
When @hamur/css is in modules it sets the style preprocessor that @hamur/adapter-vue and @hamur/adapter-svelte call for a <style lang="scss">, sass, less, styl or stylus block, so neither adapter imports the module or a compiler. A second module that sets one throws hamur/kit: a style preprocessor is already set by "@hamur/css". Any other lang throws @hamur/css compiles scss, sass, less, styl and stylus style blocks (got lang "foo"). A position inside a block is a line of the block, not of the component.