Skip to content

feat: add $assets.classes for CSS modules - #88

Merged
lukeed merged 8 commits into
mainfrom
feat/css-module-classes
Oct 26, 2026
Merged

lukeed merged 8 commits into
mainfrom
feat/css-module-classes

Conversation

@lukeed

@lukeed lukeed commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Summary

CSS modules shipped hashed class names that no page could read. This adds $assets.classes(), and fixes the bugs that came up on the way.

{{#var css = await $assets.classes('button.module.css')}}
<bongo:style src="button.module.css"></bongo:style>
<button class="{{ css.primary }}">Save</button>

Features

  • $assets.classes(filename) returns the class names of a .module.css file, as its <bongo:style> ships them. It compiles the module on first use, and the bundle takes over that compile, so each module compiles once a build. It hashes what it read, so a page that reads the classes renders again when the module or its imports change. A file that is not a .module.css fails the build.

Fixes

  • Every page that linked a CSS module rendered on every build. LightningCSS gives the class map in a new order each run, and the build hashes that map. On a 10k-page site with a module, a no-change build took 2.37 s instead of 0.24 s. The map is now sorted.
  • Composed classes lost names. A class now gets the names of each class it composes, through every level (a composes b composes c).
  • An unresolved script import was only a warning, so the script shipped the import as written and failed in the browser. It now fails the build, and names the import, the file, and the line. URL imports stay as written.
  • Missing-asset errors read asset!, rendering …; the ! is gone.

Docs

  • /css/: new sections on @import, composing a class of another module, and scripts that import a stylesheet. The CSS modules section shows $assets.classes().
  • <bongo:script>: new Imports section.
  • $assets.classes in Variables, the incremental build rules in CLI, and the readme.

Notes

  • The loader utilities move to module level, so the $assets of compiled partials keeps no build alive (build: the last build stays in memory after build() returns #59).
  • A site that maps bare imports with an import map now fails the build, since bongo has no external option.
  • A class from a file that a CSS module @imports gets a hash, but is not in $assets.classes(). The docs say to compose it, or to link plain CSS with its own <bongo:style>.

Performance

bun run bench on main and on this branch, plus hyperfine runs on the 10k-page bench site, on an M1 Max.

case main branch
no modules, fresh build 2.56 s 2.57 s (1.00 ± 0.06)
no modules, no-change build 266 ms 248 ms
module linked, fresh build 2.44 s 2.46 s
module linked, no-change build 2.37 s 239 ms
$assets.classes() on every page, fresh build — 2.44 s
$assets.classes() on every page, no-change build — 243 ms

The output of the bench site is byte for byte the same (sha256 749c3b333f7eb60d). Without modules, the differences follow the order of the runs, not the code. I ran the same main code in the repo and in a worktree: system time differs by about 15% by location alone, so it is not from this branch. No bench row, since speed without modules is the same.

Test plan

  • bun run build, bun test (899 pass), bunx tsc -p ., bunx tsc -p build
  • Unit tests: sorted class map, composes through every level, one compile and hash for each build, the bundle takes over the compile of classes(), unresolved and URL imports
  • E2E builds: class names match the linked and inline stylesheet with --minify, in an output file, a new class reaches the page after an edit, a no-change build renders no page, errors name the missing or non-module file, an unresolved import fails the build
  • bongo dev: an edit to a module adds a class to the served page, and it matches the served stylesheet
  • The docs, the example site, the bench site, and the template build
  • Docs pages checked in a browser

CSS modules shipped hashed class names that no page could read.
$assets.classes() compiles the module as its <bongo:style> will, and
the bundle takes over that compile, so it runs once. A page that reads
the classes renders again when the module or its imports change.

A class that composes others now gives all of their names. The loader
utilities move to module level, so $assets keeps no build alive.
A missing asset read 'Missing "x" asset!, rendering ...' once the build
named the page.
$assets.classes read and hashed the module file on each call, so a
layout that reads it did so for every page. Each module now compiles
and hashes once a build, as LightningCSS reads it.
LightningCSS lists only the classes that a class composes directly. A
class that composes one that composes another missed the last one's
names, and so its styles.
LightningCSS gives the classes of a CSS module in a new order each run.
The build hashes that map to tell if a page must render again, so each
page that linked a CSS module rendered on every build, with no change.
A script import that did not resolve was only a warning, so the script
shipped the import as written and failed in the browser. The bundler
tests now reset the registry before each test, as another test file can
leave a registration behind.
The CSS page had no section on plain @import, on composing a class of
another CSS module, or on scripts that import a stylesheet.
@lukeed lukeed changed the title feat: add $assets.classes for CSS modules feat: add $assets.classes for CSS modules Oct 26, 2026
The file name alone now decides if a stylesheet is a CSS module. With
`global`, a .module.css file shipped its classes as written, while
$assets.classes() gave the hashed names, and nothing failed.
@lukeed
lukeed merged commit 7c09cb1 into main Oct 26, 2026
4 checks passed
@lukeed
lukeed deleted the feat/css-module-classes branch October 26, 2026 22:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant