Stop duplicating component CSS with Less @import (reference)
Use Less @import (reference) to import variables and mixins without duplicating shared CSS rules across compiled stylesheets.
17 Dec 2025, 01:59 UTC

The problem: shared Less styles keep copying themselves
Your design system ships a tokens.less file with color variables, spacing mixins, and base component styles. Every component file imports it with plain @import "tokens.less". That works for variables, but Less also inlines every CSS rule from tokens.less into each component's compiled stylesheet. If ten components import the same base styles, you ship those rules ten times.
The useful takeaway: use @import (reference) to make variables and mixins available without emitting their CSS unless you explicitly use them.
How @import (reference) differs from plain @import
A plain Less @import pulls in the target file and copies all of its rules into the output. An @import (reference) import makes the imported file's variables, mixins, and selectors available to the importing file, but it does not emit those rules into the compiled CSS on its own. The rules only appear if the importing file explicitly extends or references them.
This is a Less.js directive, not valid plain CSS. Editor tooling may flag the syntax, and behavior depends on the installed Less compiler version.
Worked example: reference a token file without copying its rules
Create a shared token file named design-tokens.less:
@brand-blue: #0066cc;
.button-reset {
border: none;
background: transparent;
padding: 0;
font: inherit;
color: inherit;
cursor: pointer;
}
Create a component entry file named primary-button.less that imports the tokens by reference:
@import (reference) "design-tokens.less";
.primary-button {
background-color: @brand-blue;
padding: 8px 16px;
border-radius: 4px;
&:extend(.button-reset all);
}
Run the Less compiler from your project terminal:
lessc primary-button.less primary-button.css
Where to run: a terminal in the project root. Permissions: read access to the Less files and write access to the output directory. Placeholders: replace primary-button.less and primary-button.css with your actual paths. Expected check: primary-button.css should contain the reset properties inside .primary-button, but no standalone .button-reset rule at the top level. Risk: the command overwrites primary-button.css if it already exists; use a new output name or back up the file first.
For comparison, change the import to plain @import "design-tokens.less" and recompile. The plain import emits a standalone .button-reset block in addition to the component styles, duplicating the reset rules wherever the file is imported.
Limitations and how to verify
@import (reference) is not a replacement for bundler tree-shaking. If you extend every referenced selector indiscriminately, you can still bloat the compiled CSS. It is also Less-specific, so team members unfamiliar with the directive may need context, and syntax support should be confirmed against your compiler version.
Practical verification:
- Create two entry files: one using
@import (reference)and one using plain@import, both pulling in the same shared file. - Run
lessc --versionto confirm the compiler version, then compile both entries. - Compare the emitted CSS. The reference version should omit top-level rules from the shared file unless explicitly extended.
- Inspect the compiled bundle in browser developer tools to confirm reference styles appear only where the importing file uses them.
Closing recommendation
Start with one shared Less file, compile it both ways, and compare the output. If you see duplicated component styles across bundles, switch that import to @import (reference) and extend only the selectors you actually need. Measure the compiled CSS size before and after to confirm the change is worthwhile.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.