|
| 1 | +import { GriffelStyle, resolveStyleRulesForSlots } from '@griffel/core'; |
| 2 | +import type { CSSClassesMapBySlot, CSSRulesByBucket } from '@griffel/core'; |
| 3 | +import type { ValueCache } from '@wyw-in-js/processor-utils'; |
| 4 | + |
| 5 | +import { createRuleLiteral } from './assets/createRuleLiteral'; |
| 6 | +import { normalizeStyleRules } from './assets/normalizeStyleRules'; |
| 7 | +import BaseGriffelProcessor from './BaseGriffelProcessor'; |
| 8 | +import type { FileContext } from './types'; |
| 9 | +import { dedupeCSSRules } from './utils/dedupeCSSRules'; |
| 10 | + |
| 11 | +export default class MakeStylesProcessor extends BaseGriffelProcessor { |
| 12 | + #cssClassMap: CSSClassesMapBySlot<string> | undefined; |
| 13 | + #cssRulesByBucket: CSSRulesByBucket | undefined; |
| 14 | + |
| 15 | + public override build(valueCache: ValueCache) { |
| 16 | + const stylesBySlots = valueCache.get(this.expressionName) as Record<string /* slot */, GriffelStyle>; |
| 17 | + |
| 18 | + [this.#cssClassMap, this.#cssRulesByBucket] = resolveStyleRulesForSlots( |
| 19 | + // Heads up! |
| 20 | + // Style rules should be normalized *before* they will be resolved to CSS rules to have deterministic |
| 21 | + // results across different build targets. |
| 22 | + normalizeStyleRules(this.path, this.context as FileContext, stylesBySlots), |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + public override doRuntimeReplacement(): void { |
| 27 | + if (!this.#cssClassMap || !this.#cssRulesByBucket) { |
| 28 | + throw new Error('Styles are not extracted yet. Please call `build` first.'); |
| 29 | + } |
| 30 | + |
| 31 | + const t = this.astService; |
| 32 | + const addAssetImport = (path: string) => t.addDefaultImport(path, 'asset'); |
| 33 | + |
| 34 | + const uniqueRules = dedupeCSSRules(this.#cssRulesByBucket); |
| 35 | + const rulesObjectExpression = t.objectExpression( |
| 36 | + Object.entries(uniqueRules).map(([bucketName, cssRules]) => |
| 37 | + t.objectProperty( |
| 38 | + t.identifier(bucketName), |
| 39 | + t.arrayExpression( |
| 40 | + cssRules.map(rule => { |
| 41 | + if (typeof rule === 'string') { |
| 42 | + return createRuleLiteral(this.path, t, this.context as FileContext, rule, addAssetImport); |
| 43 | + } |
| 44 | + |
| 45 | + const [cssRule, metadata] = rule; |
| 46 | + |
| 47 | + return t.arrayExpression([ |
| 48 | + createRuleLiteral(this.path, t, this.context as FileContext, cssRule, addAssetImport), |
| 49 | + t.objectExpression( |
| 50 | + Object.entries(metadata).map(([key, value]) => |
| 51 | + t.objectProperty(t.identifier(key), t.stringLiteral(value as string)), |
| 52 | + ), |
| 53 | + ), |
| 54 | + ]); |
| 55 | + }), |
| 56 | + ), |
| 57 | + ), |
| 58 | + ), |
| 59 | + ); |
| 60 | + |
| 61 | + const stylesImportIdentifier = t.addNamedImport('__styles', this.tagSource.source); |
| 62 | + const stylesCallExpression = t.callExpression(stylesImportIdentifier, [ |
| 63 | + t.valueToNode(this.#cssClassMap), |
| 64 | + rulesObjectExpression, |
| 65 | + ]); |
| 66 | + |
| 67 | + this.replacer(stylesCallExpression, true); |
| 68 | + } |
| 69 | +} |
0 commit comments