@monochromatic-dev/build-tool-css #
CSS build tool that bundles @import statements across monorepo packages and
processes custom @mixin/@apply syntax into expanded CSS.
Why this exists #
No single CSS tool handles all three requirements at once:
- Monorepo-aware
@importresolution: off-the-shelf CSS tools resolve relative paths only, notnode_modulesor package.jsonexports - Custom
@mixin/@applyprocessing: no standard plugin provides the mixin semantics this monorepo needs - Browser-compatible: the mixin pipeline runs in both Node.js and browser environments (no native binary dependencies, no process globals)
Parsing sits on @monochromatic-dev/module-css-edit,
the workspace's byte-preserving CSS CST over the @csstools/css-tokenizer
spec tokenizer.
Untouched CSS survives byte-exactly,
comments and author formatting included.
See doc/troubleshooting/css-tooling.md for the full tooling chronicle, including the 2026-07 parser survey that led here from postcss.
Usage #
CLI #
build-css src/main.css dist/bundle.css
Build a file (Node) #
import { buildCss, } from '@monochromatic-dev/build-tool-css';
const css = await buildCss({
input: 'src/main.css',
output: 'dist/bundle.css',
},);
Expand mixins in memory (browser-safe) #
For consumers that already have CSS text in memory, such as web components with Shadow DOM styles defined as JavaScript strings:
import { expandCssMixins, } from '@monochromatic-dev/build-tool-css/ts';
import mixinSource from './mixins.css' with { type: 'text', };
const expanded = expandCssMixins({
css: `
.close { @apply --reset-button; @apply --touch-target; }
.pill { @apply --pill; }
`,
mixinCss: mixinSource,
},);
expandCssMixins collects definitions from mixinCss and from css itself
(inline definitions win on name collision),
expands nested references,
replaces every @apply,
and strips the definitions from the output.
No filesystem access,
no process globals.
CSS syntax #
Mixin definitions #
@mixin --flex-center {
display: flex;
align-items: center;
justify-content: center;
}
Definitions are removed from the output. A definition without a name or without structural body content is an error.
Mixin application #
.component {
@apply --flex-center;
background: var(--primary);
}
@apply rules are replaced with the referenced mixin's body.
Bodies may contain declarations,
nested rules
(with or without &),
and further @apply references.
Nested mixins #
Mixins can reference other mixins via @apply.
Each mixin expands exactly once;
circular references throw CircularCssMixinError
naming the exact reference chain
(for example --a -> --b -> --a).
@mixin --card {
@apply --flex-center;
padding: 1rem;
border: 1px solid gray;
}
Cross-package imports #
Imports resolve through package.json exports mappings,
node_modules lookup,
and direct file paths:
/* Via exports field */
@import '@monochromatic-dev/style-monochromatic/index.css';
/* Via direct file path (package has no exports field) */
@import '@some-package/src/tokens.css';
Specifiers are read from parsed tokens,
so trailing conditions such as layer(base) or media queries
never corrupt the target.
Each file inlines once;
circular imports resolve to nothing on revisit.
Build pipeline #
- Resolve and bundle:
@importat-rules are resolved (relative paths, package.jsonexports, barenode_modulesspecifiers) and replaced by the parsed contents of their files, recursively - Collect mixins:
@mixindefinitions move into a registry and leave the tree - Expand mixin bodies:
nested
@applybetween definitions resolve by memoized recursion with an explicit trail (exact cycle reporting) - Inline
@apply: document references splice in registry bodies, shared by reference thanks to the immutable CST - Write output: final CSS string written to disk
Module structure #
index.ts: public surface (buildCss,expandCssMixins, error classes)build.ts: file pipeline orchestrationexpand.ts: in-memory mixin pipelineimport.ts:@importinlining with monorepo-aware resolutionmixin.ts: mixin collection and expansion engine (internal)errors.ts:UnknownCssMixinError,CircularCssMixinErrorpackage-resolver.ts:node_modulesandexportsresolution. Its upward walk stays hand-rolled and synchronous: it runs inside the synchronoustransformStylesheetvisitor fromcss-edit, which has no async form, and it probes through this package's in-memory registry, so the asyncfindRootwalker of@monochromatic-dev/module-fs-pathcannot replace it.specifier.ts: specifier classification helpersfs.ts: adaptive file reader (in-memory registry withnode:fsfallback)fs-registry.ts: in-memoryMapfor browser-side file storagecli.ts:build-cssbinary entry point
Testing #
# Build, then run all module-test files in this package
mise run //package/build-tool/css:buildAndTest
Integration tests exercise two CSS import resolution strategies using fixture packages:
exportsfield:test-css-importingimports fromtest-css-imported(hasexportsin package.json)- Direct file path:
test-css-importing-filepathimports fromtest-css-imported-no-exports(noexportsfield)
Both strategies run identical assertions:
import resolution,
mixin removal,
@apply expansion,
nested mixin inlining,
and output file writing.
Error paths and import dedup run on disposable temp directories.