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

Commit 1163947

Browse files
feat(build): accept an input stylesheet, and warn on an @apply that is dropped
`input` names an authored stylesheet the build processes and emits with the utilities, so component CSS goes through the same pipeline instead of living in a `preflights` string the engine never reads. It is emitted AFTER the utilities, so a rule written by hand beats a utility of equal specificity -- which is the reason to have written it. A utility `@apply` cannot generate, or a `theme()` path that resolves to nothing, is reported with the file it came from rather than passed through in silence. And until a sheet is moved over, `@apply` in a preflight now says it will not reach the browser. It was shipped verbatim as an unknown at-rule and dropped there, with nothing in the build to suggest why the styles were missing. Closes #24 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent f993048 commit 1163947

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎packages/toolkit/src/engine/build.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { CssConfig } from './types'
22
import { CSSGenerator } from './generator'
33
import { resolveSafelist } from './safelist'
44
import { Scanner } from './scanner'
5+
import { containsApply, processStylesheet } from './stylesheet'
56
import { collectStyles } from './style/collect'
67
import { resetStyles } from './style/registry'
78
import { CompileClassTransformer } from './transformer-compile-class'
@@ -64,9 +65,34 @@ export async function build(config: CssConfig): Promise<BuildResult> {
6465
}
6566
}
6667

68+
// A preflight is shipped verbatim, so an `@apply` in one reaches the
69+
// browser as an unknown at-rule and is dropped there without a word. Say so
70+
// rather than leaving the author to find it in devtools.
71+
if (config.includePreflight !== false) {
72+
for (const preflight of config.preflights) {
73+
if (containsApply(preflight.getCSS())) {
74+
console.warn('⚠️ @apply in a preflight is not expanded and will not reach the browser. Put the CSS in the `input` stylesheet instead.')
75+
break
76+
}
77+
}
78+
}
79+
6780
// Preflight CSS is now added by generator.toCSS()
6881
let css = generator.toCSS(config.includePreflight !== false, config.minify)
6982

83+
// The authored stylesheet goes after the utilities, so a rule written by
84+
// hand beats a utility of equal specificity -- which is the reason to have
85+
// written it there.
86+
if (config.input) {
87+
const source = await Bun.file(config.input).text()
88+
const processed = processStylesheet(source, generator, config.theme)
89+
for (const utility of processed.unknownUtilities)
90+
console.warn(`⚠️ @apply ${utility}: not a utility this config generates (${config.input})`)
91+
for (const path of processed.unknownThemePaths)
92+
console.warn(`⚠️ theme(${path}): no such value in the theme (${config.input})`)
93+
css += `\n${processed.css}`
94+
}
95+
7096
// Styles declared through the `css.create()` API live in modules, not in
7197
// class attributes, so the scanner never sees them. Evaluating those modules
7298
// registers their atomic rules; append the result after the utilities so a

‎packages/toolkit/src/engine/types.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ export interface CssConfig {
130130
* Enabled by default; see `CodeStringsConfig` in the parser.
131131
*/
132132
codeStrings?: CodeStringsConfig
133+
/**
134+
* An authored stylesheet to process and emit alongside the utilities.
135+
*
136+
* Its `@apply` expands to the declarations the named utilities produce and
137+
* its `theme()` reads the registered theme, so component CSS goes through
138+
* the same pipeline as everything else instead of living in a `preflights`
139+
* string the engine never reads. Nesting is passed through as written.
140+
*/
141+
input?: string
133142
/** Generate :root CSS variables from theme colors (e.g., --monokai-bg: #2d2a2e) */
134143
cssVariables?: boolean
135144
/**

0 commit comments

Comments
 (0)