镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit 154b7c7

Browse files
committed
fix: emit real CSS for compile-class groups under the hashed selector
The :cw: compile-class transformer rewrote markup to class="cw-<hash>" but the build dumped the group's raw utilities into the main class set — utilities the transformer had just REMOVED from the markup. The hashed class had zero declarations and compiled elements rendered completely unstyled. Compiled groups now expand through the shortcut machinery under their own selector (variants included, so hover:underline in a group becomes .cw-<hash>:hover), with an idempotency guard independent of the class cache since the hashed name also reaches the regular generate() path via the transformed markup.
1 parent f1925b5 commit 154b7c7

3 files changed

Lines changed: 68 additions & 10 deletions

File tree

‎packages/crosswind/src/build.ts‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,25 @@ export async function build(config: CrosswindConfig): Promise<BuildResult> {
3838
classes.add(cls)
3939
}
4040

41-
// If using compile class transformer, also add compiled utilities
42-
if (transformer) {
43-
const compiledClasses = transformer.getCompiledClasses()
44-
for (const [, { utilities }] of compiledClasses) {
45-
for (const utility of utilities) {
46-
classes.add(utility)
47-
}
48-
}
49-
}
50-
5141
// Generate CSS
5242
const generator = new CSSGenerator(config)
5343

5444
for (const className of classes) {
5545
generator.generate(className)
5646
}
5747

48+
// Compiled class groups (:cw: markers) emit under their own hashed
49+
// selector. Previously the group's utilities were dumped into the main
50+
// class set instead — but the transformer had just REMOVED those class
51+
// names from the markup, so the rewritten `class="cw-<hash>"` matched
52+
// nothing and elements rendered unstyled.
53+
if (transformer) {
54+
const compiledClasses = transformer.getCompiledClasses()
55+
for (const [, { className, utilities }] of compiledClasses) {
56+
generator.generateCompiledClass(className, utilities)
57+
}
58+
}
59+
5860
// Preflight CSS is now added by generator.toCSS()
5961
const css = generator.toCSS(config.includePreflight !== false, config.minify)
6062
const duration = performance.now() - startTime

‎packages/crosswind/src/generator.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,7 @@ export class CSSGenerator {
14541454
private noMatchCache: Set<string> = new Set()
14551455
private usedKeyframes: Set<string> = new Set()
14561456
private variantHandledCache: Map<string, boolean> = new Map()
1457+
private compiledGenerated: Set<string> = new Set()
14571458
// While expanding a shortcut, rules emit under the shortcut's own class
14581459
// name (with variants applied to it: `.btn:hover`) instead of the
14591460
// sub-utility's selector, which the markup never carries.
@@ -1540,6 +1541,26 @@ export class CSSGenerator {
15401541
this.shortcutSelectorRaw = prev
15411542
}
15421543

1544+
/**
1545+
* Generate rules for a compiled class group (compile-class transformer):
1546+
* every utility in the group emits under the compiled class's own
1547+
* selector, exactly like a shortcut. Without this the transformer
1548+
* rewrote markup to `class="cw-<hash>"` while the CSS only contained the
1549+
* original utility selectors — the compiled class had no styles at all.
1550+
*/
1551+
generateCompiledClass(className: string, utilities: string[]): void {
1552+
// Own idempotency guard: the hashed class name usually ALSO reaches the
1553+
// regular generate() path (the scanner extracts it from the transformed
1554+
// markup), which emits nothing but claims the classCache slot — an
1555+
// early-return on classCache here would skip the real expansion.
1556+
if (this.compiledGenerated.has(className)) {
1557+
return
1558+
}
1559+
this.compiledGenerated.add(className)
1560+
this.classCache.add(className)
1561+
this.expandShortcut(className, utilities, new Set([className]))
1562+
}
1563+
15431564
/**
15441565
* Generate rules for a single utility class (no cache or shortcut handling)
15451566
*/

‎packages/crosswind/test/transformer-compile-class.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,38 @@ describe('Compile Class Transformer', () => {
285285
})
286286
})
287287
})
288+
289+
describe('compiled classes generate real CSS (build integration)', () => {
290+
it('emits a rule for the hashed class with the group declarations', async () => {
291+
const { build } = await import('../src/build')
292+
const { defaultConfig } = await import('../src/config')
293+
const dir = `${import.meta.dir}/.compile-css-test`
294+
await Bun.write(`${dir}/index.html`, '<div class=":cw: flex items-center gap-2 hover:underline">hi</div>')
295+
try {
296+
const result = await build({
297+
...defaultConfig,
298+
content: [`${dir}/*.html`],
299+
minify: false,
300+
compileClass: { enabled: true },
301+
} as any)
302+
const compiled = [...result.compiledClasses!.values()][0]
303+
expect(compiled.className).toMatch(/^cw-/)
304+
// The hashed selector carries the group's declarations
305+
expect(result.css).toContain(`.${compiled.className} {`)
306+
expect(result.css).toContain('display: flex;')
307+
expect(result.css).toContain('align-items: center;')
308+
// Variant utilities inside the group expand onto the compiled selector
309+
expect(result.css).toContain(`.${compiled.className}:hover`)
310+
// The raw utilities are NOT emitted under their own selectors — the
311+
// markup no longer contains them
312+
expect(result.css).not.toContain('.items-center {')
313+
// And the transformed markup references the hashed class
314+
const transformed = [...result.transformedFiles!.values()][0]
315+
expect(transformed).toContain(`class="${compiled.className}"`)
316+
}
317+
finally {
318+
const { rm } = await import('node:fs/promises')
319+
await rm(dir, { recursive: true, force: true })
320+
}
321+
})
322+
})

0 commit comments

Comments
 (0)