@tenphi/tasty 0.8.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -1
- package/dist/chunks/definitions.js +1 -2
- package/dist/chunks/definitions.js.map +1 -1
- package/dist/config.js +15 -1
- package/dist/config.js.map +1 -1
- package/dist/core/index.d.ts +2 -2
- package/dist/core/index.js +3 -3
- package/dist/hooks/useGlobalStyles.d.ts +3 -0
- package/dist/hooks/useGlobalStyles.js +28 -1
- package/dist/hooks/useGlobalStyles.js.map +1 -1
- package/dist/hooks/useKeyframes.js +18 -3
- package/dist/hooks/useKeyframes.js.map +1 -1
- package/dist/hooks/useProperty.js +36 -13
- package/dist/hooks/useProperty.js.map +1 -1
- package/dist/hooks/useRawCSS.js +13 -1
- package/dist/hooks/useRawCSS.js.map +1 -1
- package/dist/hooks/useStyles.d.ts +5 -0
- package/dist/hooks/useStyles.js +82 -3
- package/dist/hooks/useStyles.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +3 -3
- package/dist/injector/index.d.ts +9 -1
- package/dist/injector/index.js +9 -1
- package/dist/injector/index.js.map +1 -1
- package/dist/injector/injector.d.ts +9 -0
- package/dist/injector/injector.js +20 -1
- package/dist/injector/injector.js.map +1 -1
- package/dist/injector/sheet-manager.d.ts +1 -0
- package/dist/injector/sheet-manager.js +1 -0
- package/dist/injector/sheet-manager.js.map +1 -1
- package/dist/parser/classify.js.map +1 -1
- package/dist/properties/index.js +20 -5
- package/dist/properties/index.js.map +1 -1
- package/dist/properties/property-type-resolver.js +4 -0
- package/dist/properties/property-type-resolver.js.map +1 -1
- package/dist/ssr/astro.d.ts +29 -0
- package/dist/ssr/astro.js +65 -0
- package/dist/ssr/astro.js.map +1 -0
- package/dist/ssr/async-storage.d.ts +17 -0
- package/dist/ssr/async-storage.js +35 -0
- package/dist/ssr/async-storage.js.map +1 -0
- package/dist/ssr/collect-auto-properties.js +40 -0
- package/dist/ssr/collect-auto-properties.js.map +1 -0
- package/dist/ssr/collector.d.ts +85 -0
- package/dist/ssr/collector.js +173 -0
- package/dist/ssr/collector.js.map +1 -0
- package/dist/ssr/context.d.ts +8 -0
- package/dist/ssr/context.js +14 -0
- package/dist/ssr/context.js.map +1 -0
- package/dist/ssr/format-global-rules.js +22 -0
- package/dist/ssr/format-global-rules.js.map +1 -0
- package/dist/ssr/format-keyframes.js +70 -0
- package/dist/ssr/format-keyframes.js.map +1 -0
- package/dist/ssr/format-property.js +48 -0
- package/dist/ssr/format-property.js.map +1 -0
- package/dist/ssr/format-rules.js +70 -0
- package/dist/ssr/format-rules.js.map +1 -0
- package/dist/ssr/hydrate.d.ts +22 -0
- package/dist/ssr/hydrate.js +50 -0
- package/dist/ssr/hydrate.js.map +1 -0
- package/dist/ssr/index.d.ts +5 -0
- package/dist/ssr/index.js +12 -0
- package/dist/ssr/index.js.map +1 -0
- package/dist/ssr/next.d.ts +45 -0
- package/dist/ssr/next.js +71 -0
- package/dist/ssr/next.js.map +1 -0
- package/dist/ssr/ssr-collector-ref.js +12 -0
- package/dist/ssr/ssr-collector-ref.js.map +1 -0
- package/dist/styles/predefined.d.ts +0 -2
- package/dist/styles/predefined.js +0 -3
- package/dist/styles/predefined.js.map +1 -1
- package/dist/styles/preset.js +1 -1
- package/dist/styles/preset.js.map +1 -1
- package/dist/styles/scrollbar.d.ts +9 -5
- package/dist/styles/scrollbar.js +25 -89
- package/dist/styles/scrollbar.js.map +1 -1
- package/dist/styles/transition.js +1 -1
- package/dist/styles/transition.js.map +1 -1
- package/dist/styles/types.d.ts +10 -13
- package/dist/tasty.d.ts +67 -68
- package/dist/zero/babel.d.ts +16 -2
- package/dist/zero/babel.js +32 -1
- package/dist/zero/babel.js.map +1 -1
- package/dist/zero/next.d.ts +29 -30
- package/dist/zero/next.js +49 -39
- package/dist/zero/next.js.map +1 -1
- package/docs/ssr.md +372 -0
- package/docs/styles.md +19 -12
- package/package.json +44 -28
- package/dist/styles/styledScrollbar.d.ts +0 -47
- package/dist/styles/styledScrollbar.js +0 -38
- package/dist/styles/styledScrollbar.js.map +0 -1
package/dist/properties/index.js
CHANGED
|
@@ -137,6 +137,20 @@ function getEffectiveDefinition(token, userDefinition) {
|
|
|
137
137
|
isValid: true
|
|
138
138
|
};
|
|
139
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* Convert a color initialValue to an RGB string for the companion `-rgb` property.
|
|
142
|
+
* Used by SSR to emit `@property --name-color-rgb { syntax: "<number>+"; ... }`.
|
|
143
|
+
*/
|
|
144
|
+
function colorInitialValueToRgb(initialValue) {
|
|
145
|
+
if (initialValue == null) return "0 0 0";
|
|
146
|
+
const val = String(initialValue).trim().toLowerCase();
|
|
147
|
+
if (val === "transparent" || val === "rgba(0,0,0,0)" || val === "") return "0 0 0";
|
|
148
|
+
if (val === "white") return "255 255 255";
|
|
149
|
+
if (val === "black") return "0 0 0";
|
|
150
|
+
const rgbMatch = val.match(/^rgba?\(\s*(\d+)[,\s]+(\d+)[,\s]+(\d+)/);
|
|
151
|
+
if (rgbMatch) return `${rgbMatch[1]} ${rgbMatch[2]} ${rgbMatch[3]}`;
|
|
152
|
+
return "0 0 0";
|
|
153
|
+
}
|
|
140
154
|
const UNIT_TO_SYNTAX = {};
|
|
141
155
|
const LENGTH_UNITS = [
|
|
142
156
|
"px",
|
|
@@ -173,12 +187,12 @@ const ANGLE_UNITS = [
|
|
|
173
187
|
];
|
|
174
188
|
const TIME_UNITS = ["ms", "s"];
|
|
175
189
|
for (const u of LENGTH_UNITS) UNIT_TO_SYNTAX[u] = {
|
|
176
|
-
syntax: "<length>",
|
|
190
|
+
syntax: "<length-percentage>",
|
|
177
191
|
initialValue: "0px"
|
|
178
192
|
};
|
|
179
193
|
UNIT_TO_SYNTAX["%"] = {
|
|
180
|
-
syntax: "<percentage>",
|
|
181
|
-
initialValue: "
|
|
194
|
+
syntax: "<length-percentage>",
|
|
195
|
+
initialValue: "0px"
|
|
182
196
|
};
|
|
183
197
|
for (const u of ANGLE_UNITS) UNIT_TO_SYNTAX[u] = {
|
|
184
198
|
syntax: "<angle>",
|
|
@@ -190,7 +204,8 @@ for (const u of TIME_UNITS) UNIT_TO_SYNTAX[u] = {
|
|
|
190
204
|
};
|
|
191
205
|
/**
|
|
192
206
|
* Infer CSS @property syntax from a concrete value.
|
|
193
|
-
*
|
|
207
|
+
* Detects numeric types: \<number\>, \<length-percentage\>, \<angle\>, \<time\>.
|
|
208
|
+
* Length and percentage values both map to \<length-percentage\> for maximum flexibility.
|
|
194
209
|
* Color properties are handled separately via the #name token convention
|
|
195
210
|
* (--name-color gets \<color\> syntax automatically in getEffectiveDefinition).
|
|
196
211
|
*
|
|
@@ -217,5 +232,5 @@ function inferSyntaxFromValue(value) {
|
|
|
217
232
|
}
|
|
218
233
|
|
|
219
234
|
//#endregion
|
|
220
|
-
export { extractLocalProperties, getEffectiveDefinition, hasLocalProperties, inferSyntaxFromValue, normalizePropertyDefinition };
|
|
235
|
+
export { colorInitialValueToRgb, extractLocalProperties, getEffectiveDefinition, hasLocalProperties, inferSyntaxFromValue, normalizePropertyDefinition, parsePropertyToken };
|
|
221
236
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/properties/index.ts"],"sourcesContent":["/**\n * Properties Utilities\n *\n * Utilities for extracting and processing CSS @property definitions in styles.\n * Unlike keyframes, properties are permanent once registered and don't need cleanup.\n *\n * Property names use tasty token syntax:\n * - `$name` for regular properties → `--name`\n * - `#name` for color properties → `--name-color` (auto-sets syntax: '<color>')\n */\n\nimport type { PropertyDefinition } from '../injector/types';\nimport { RE_NUMBER, RE_RAW_UNIT } from '../parser/const';\nimport type { Styles } from '../styles/types';\n\n// ============================================================================\n// Constants\n// ============================================================================\n\nconst PROPERTIES_KEY = '@properties';\n\n/**\n * Valid CSS custom property name pattern (after the -- prefix).\n * Must start with a letter or underscore, followed by letters, digits, hyphens, or underscores.\n */\nconst VALID_PROPERTY_NAME_PATTERN = /^[a-z_][a-z0-9-_]*$/i;\n\n// ============================================================================\n// Validation Functions\n// ============================================================================\n\n/**\n * Validate a CSS custom property name (the part after --).\n * Returns true if the name is valid for use as a CSS custom property.\n */\nexport function isValidPropertyName(name: string): boolean {\n return VALID_PROPERTY_NAME_PATTERN.test(name);\n}\n\n/**\n * Result of parsing a property token.\n */\nexport interface ParsedPropertyToken {\n /** The CSS custom property name (e.g., '--my-prop') */\n cssName: string;\n /** Whether this is a color property */\n isColor: boolean;\n /** Whether the token was valid */\n isValid: boolean;\n /** Error message if invalid */\n error?: string;\n}\n\n// ============================================================================\n// Extraction Functions\n// ============================================================================\n\n/**\n * Check if styles object has local @properties definition.\n * Fast path: single property lookup.\n */\nexport function hasLocalProperties(styles: Styles): boolean {\n return PROPERTIES_KEY in styles;\n}\n\n/**\n * Extract local @properties from styles object.\n * Returns null if no local properties (fast path).\n */\nexport function extractLocalProperties(\n styles: Styles,\n): Record<string, PropertyDefinition> | null {\n const properties = styles[PROPERTIES_KEY];\n if (!properties || typeof properties !== 'object') {\n return null;\n }\n return properties as Record<string, PropertyDefinition>;\n}\n\n// ============================================================================\n// Token Parsing Functions\n// ============================================================================\n\n/**\n * Parse a property token name and return the CSS property name and whether it's a color.\n * Supports tasty token syntax and validates the property name.\n *\n * Token formats:\n * - `$name` → { cssName: '--name', isColor: false }\n * - `#name` → { cssName: '--name-color', isColor: true }\n * - `--name` → { cssName: '--name', isColor: false } (legacy, auto-detect color by suffix)\n * - `name` → { cssName: '--name', isColor: false } (legacy)\n *\n * @param token - The property token to parse\n * @returns Parsed result with cssName, isColor, isValid, and optional error\n */\nexport function parsePropertyToken(token: string): ParsedPropertyToken {\n if (!token || typeof token !== 'string') {\n return {\n cssName: '',\n isColor: false,\n isValid: false,\n error: 'Property token must be a non-empty string',\n };\n }\n\n let name: string;\n let isColor: boolean;\n\n if (token.startsWith('$')) {\n // Regular property token: $name → --name\n name = token.slice(1);\n isColor = false;\n } else if (token.startsWith('#')) {\n // Color property token: #name → --name-color\n name = token.slice(1);\n isColor = true;\n } else if (token.startsWith('--')) {\n // Legacy format with -- prefix\n name = token.slice(2);\n isColor = token.endsWith('-color');\n } else {\n // Legacy format without prefix\n name = token;\n isColor = token.endsWith('-color');\n }\n\n // Validate the name\n if (!name) {\n return {\n cssName: '',\n isColor,\n isValid: false,\n error: 'Property name cannot be empty',\n };\n }\n\n if (!isValidPropertyName(name)) {\n return {\n cssName: '',\n isColor,\n isValid: false,\n error: `Invalid property name \"${name}\". Must start with a letter or underscore, followed by letters, digits, hyphens, or underscores.`,\n };\n }\n\n // Build the CSS custom property name\n // For #name tokens, we add -color suffix\n // For legacy formats (--name-color or name-color), the name already includes -color\n let cssName: string;\n if (token.startsWith('#')) {\n // Color token: #name → --name-color\n cssName = `--${name}-color`;\n } else {\n // All other formats: just add -- prefix\n cssName = `--${name}`;\n }\n\n return {\n cssName,\n isColor,\n isValid: true,\n };\n}\n\n// ============================================================================\n// Normalization Functions\n// ============================================================================\n\n/**\n * Normalize a property name to the CSS custom property format.\n *\n * @deprecated Use parsePropertyToken instead for proper token handling\n */\nexport function normalizePropertyName(name: string): string {\n const result = parsePropertyToken(name);\n return result.isValid ? result.cssName : `--${name}`;\n}\n\n/**\n * Normalize a property definition to a consistent string representation.\n * Used for comparing definitions to detect type conflicts.\n *\n * Only `syntax` and `inherits` are compared — `initialValue` is intentionally\n * excluded because different components may set different defaults for the\n * same typed property (e.g. auto-inferred `0px` vs explicit `6px`).\n *\n * Keys are sorted alphabetically to ensure consistent comparison:\n * { inherits: true, syntax: '<color>' } === { syntax: '<color>', inherits: true }\n */\nexport function normalizePropertyDefinition(def: PropertyDefinition): string {\n const normalized: Record<string, unknown> = {};\n\n if (def.inherits !== undefined) {\n normalized.inherits = def.inherits;\n }\n if (def.syntax !== undefined) {\n normalized.syntax = def.syntax;\n }\n\n return JSON.stringify(normalized);\n}\n\n/**\n * Result of getEffectiveDefinition.\n */\nexport interface EffectiveDefinitionResult {\n /** The CSS custom property name */\n cssName: string;\n /** The effective property definition */\n definition: PropertyDefinition;\n /** Whether this is a color property */\n isColor: boolean;\n /** Whether the token was valid */\n isValid: boolean;\n /** Error message if invalid */\n error?: string;\n}\n\n/**\n * Get the effective property definition for a token.\n * For color tokens (#name), auto-sets syntax to '<color>' and defaults initialValue to 'transparent'.\n *\n * @param token - Property token ($name, #name, --name, or plain name)\n * @param userDefinition - User-provided definition options\n * @returns Effective definition with cssName, definition, isValid, and optional error\n */\nexport function getEffectiveDefinition(\n token: string,\n userDefinition: PropertyDefinition,\n): EffectiveDefinitionResult {\n const parsed = parsePropertyToken(token);\n\n if (!parsed.isValid) {\n return {\n cssName: '',\n definition: userDefinition,\n isColor: false,\n isValid: false,\n error: parsed.error,\n };\n }\n\n if (parsed.isColor) {\n // Color properties have fixed syntax and default initialValue\n return {\n cssName: parsed.cssName,\n definition: {\n syntax: '<color>', // Always '<color>' for color tokens, cannot be overridden\n inherits: userDefinition.inherits, // Allow inherits to be customized\n initialValue: userDefinition.initialValue ?? 'transparent', // Default to transparent\n },\n isColor: true,\n isValid: true,\n };\n }\n\n // Regular properties use the definition as-is\n return {\n cssName: parsed.cssName,\n definition: userDefinition,\n isColor: false,\n isValid: true,\n };\n}\n\n// ============================================================================\n// Value Type Inference\n// ============================================================================\n\n/**\n * Result of inferring a CSS @property syntax from a value.\n */\nexport interface InferredSyntax {\n syntax: string;\n initialValue: string;\n}\n\nconst UNIT_TO_SYNTAX: Record<string, InferredSyntax> = {};\n\nconst LENGTH_UNITS = [\n 'px',\n 'em',\n 'rem',\n 'vw',\n 'vh',\n 'vmin',\n 'vmax',\n 'ch',\n 'ex',\n 'cap',\n 'ic',\n 'lh',\n 'rlh',\n 'svw',\n 'svh',\n 'lvw',\n 'lvh',\n 'dvw',\n 'dvh',\n 'cqw',\n 'cqh',\n 'cqi',\n 'cqb',\n 'cqmin',\n 'cqmax',\n];\n\nconst ANGLE_UNITS = ['deg', 'rad', 'grad', 'turn'];\nconst TIME_UNITS = ['ms', 's'];\n\nfor (const u of LENGTH_UNITS) {\n UNIT_TO_SYNTAX[u] = { syntax: '<length>', initialValue: '0px' };\n}\nUNIT_TO_SYNTAX['%'] = { syntax: '<percentage>', initialValue: '0%' };\nfor (const u of ANGLE_UNITS) {\n UNIT_TO_SYNTAX[u] = { syntax: '<angle>', initialValue: '0deg' };\n}\nfor (const u of TIME_UNITS) {\n UNIT_TO_SYNTAX[u] = { syntax: '<time>', initialValue: '0s' };\n}\n\n/**\n * Infer CSS @property syntax from a concrete value.\n * Only detects numeric types: \\<number\\>, \\<length\\>, \\<percentage\\>, \\<angle\\>, \\<time\\>.\n * Color properties are handled separately via the #name token convention\n * (--name-color gets \\<color\\> syntax automatically in getEffectiveDefinition).\n *\n * @param value - The CSS value to infer from (e.g. '10px', '1', '45deg')\n * @returns Inferred syntax and initial value, or null if not inferable\n */\nexport function inferSyntaxFromValue(value: string): InferredSyntax | null {\n if (!value || typeof value !== 'string') return null;\n\n const trimmed = value.trim();\n if (!trimmed) return null;\n\n if (RE_NUMBER.test(trimmed)) {\n // Bare zero is ambiguous (could be <length>, <angle>, <percentage>, etc.)\n if (parseFloat(trimmed) === 0) return null;\n return { syntax: '<number>', initialValue: '0' };\n }\n\n const unitMatch = trimmed.match(RE_RAW_UNIT);\n if (unitMatch) {\n const unit = unitMatch[2];\n const mapping = UNIT_TO_SYNTAX[unit];\n if (mapping) return mapping;\n }\n\n return null;\n}\n"],"mappings":";;;AAmBA,MAAM,iBAAiB;;;;;AAMvB,MAAM,8BAA8B;;;;;AAUpC,SAAgB,oBAAoB,MAAuB;AACzD,QAAO,4BAA4B,KAAK,KAAK;;;;;;AAyB/C,SAAgB,mBAAmB,QAAyB;AAC1D,QAAO,kBAAkB;;;;;;AAO3B,SAAgB,uBACd,QAC2C;CAC3C,MAAM,aAAa,OAAO;AAC1B,KAAI,CAAC,cAAc,OAAO,eAAe,SACvC,QAAO;AAET,QAAO;;;;;;;;;;;;;;;AAo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gB;GAC9C;EACD,SAAS;EACT,SAAS;EACV;AAIH,QAAO;EACL,SAAS,OAAO;EAChB,YAAY;EACZ,SAAS;EACT,SAAS;EACV;;AAeH,MAAM,iBAAiD,EAAE;AAEzD,MAAM,eAAe;CACnB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD;AAED,MAAM,cAAc;CAAC;CAAO;CAAO;CAAQ;CAAO;AAClD,MAAM,aAAa,CAAC,MAAM,IAAI;AAE9B,KAAK,MAAM,KAAK,aACd,gBAAe,KAAK;CAAE,QAAQ;CAAY,cAAc;CAAO;AAEjE,eAAe,OAAO;CAAE,QAAQ;CAAgB,cAAc;CAAM;AACpE,KAAK,MAAM,KAAK,YACd,gBAAe,KAAK;CAAE,QAAQ;CAAW,cAAc;CAAQ;AAEjE,KAAK,MAAM,KAAK,WACd,gBAAe,KAAK;CAAE,QAAQ;CAAU,cAAc;CAAM;;;;;;;;;;AAY9D,SAAgB,qBAAqB,OAAsC;AACzE,KAAI,CAAC,SAAS,OAAO,UAAU,SAAU,QAAO;CAEhD,MAAM,UAAU,MAAM,MAAM;AAC5B,KAAI,CAAC,QAAS,QAAO;AAErB,KAAI,UAAU,KAAK,QAAQ,EAAE;AAE3B,MAAI,WAAW,QAAQ,KAAK,EAAG,QAAO;AACtC,SAAO;GAAE,QAAQ;GAAY,cAAc;GAAK;;CAGlD,MAAM,YAAY,QAAQ,MAAM,YAAY;AAC5C,KAAI,WAAW;EAEb,MAAM,UAAU,eADH,UAAU;AAEvB,MAAI,QAAS,QAAO;;AAGtB,QAAO"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/properties/index.ts"],"sourcesContent":["/**\n * Properties Utilities\n *\n * Utilities for extracting and processing CSS @property definitions in styles.\n * Unlike keyframes, properties are permanent once registered and don't need cleanup.\n *\n * Property names use tasty token syntax:\n * - `$name` for regular properties → `--name`\n * - `#name` for color properties → `--name-color` (auto-sets syntax: '<color>')\n */\n\nimport type { PropertyDefinition } from '../injector/types';\nimport { RE_NUMBER, RE_RAW_UNIT } from '../parser/const';\nimport type { Styles } from '../styles/types';\n\n// ============================================================================\n// Constants\n// ============================================================================\n\nconst PROPERTIES_KEY = '@properties';\n\n/**\n * Valid CSS custom property name pattern (after the -- prefix).\n * Must start with a letter or underscore, followed by letters, digits, hyphens, or underscores.\n */\nconst VALID_PROPERTY_NAME_PATTERN = /^[a-z_][a-z0-9-_]*$/i;\n\n// ============================================================================\n// Validation Functions\n// ============================================================================\n\n/**\n * Validate a CSS custom property name (the part after --).\n * Returns true if the name is valid for use as a CSS custom property.\n */\nexport function isValidPropertyName(name: string): boolean {\n return VALID_PROPERTY_NAME_PATTERN.test(name);\n}\n\n/**\n * Result of parsing a property token.\n */\nexport interface ParsedPropertyToken {\n /** The CSS custom property name (e.g., '--my-prop') */\n cssName: string;\n /** Whether this is a color property */\n isColor: boolean;\n /** Whether the token was valid */\n isValid: boolean;\n /** Error message if invalid */\n error?: string;\n}\n\n// ============================================================================\n// Extraction Functions\n// ============================================================================\n\n/**\n * Check if styles object has local @properties definition.\n * Fast path: single property lookup.\n */\nexport function hasLocalProperties(styles: Styles): boolean {\n return PROPERTIES_KEY in styles;\n}\n\n/**\n * Extract local @properties from styles object.\n * Returns null if no local properties (fast path).\n */\nexport function extractLocalProperties(\n styles: Styles,\n): Record<string, PropertyDefinition> | null {\n const properties = styles[PROPERTIES_KEY];\n if (!properties || typeof properties !== 'object') {\n return null;\n }\n return properties as Record<string, PropertyDefinition>;\n}\n\n// ============================================================================\n// Token Parsing Functions\n// ============================================================================\n\n/**\n * Parse a property token name and return the CSS property name and whether it's a color.\n * Supports tasty token syntax and validates the property name.\n *\n * Token formats:\n * - `$name` → { cssName: '--name', isColor: false }\n * - `#name` → { cssName: '--name-color', isColor: true }\n * - `--name` → { cssName: '--name', isColor: false } (legacy, auto-detect color by suffix)\n * - `name` → { cssName: '--name', isColor: false } (legacy)\n *\n * @param token - The property token to parse\n * @returns Parsed result with cssName, isColor, isValid, and optional error\n */\nexport function parsePropertyToken(token: string): ParsedPropertyToken {\n if (!token || typeof token !== 'string') {\n return {\n cssName: '',\n isColor: false,\n isValid: false,\n error: 'Property token must be a non-empty string',\n };\n }\n\n let name: string;\n let isColor: boolean;\n\n if (token.startsWith('$')) {\n // Regular property token: $name → --name\n name = token.slice(1);\n isColor = false;\n } else if (token.startsWith('#')) {\n // Color property token: #name → --name-color\n name = token.slice(1);\n isColor = true;\n } else if (token.startsWith('--')) {\n // Legacy format with -- prefix\n name = token.slice(2);\n isColor = token.endsWith('-color');\n } else {\n // Legacy format without prefix\n name = token;\n isColor = token.endsWith('-color');\n }\n\n // Validate the name\n if (!name) {\n return {\n cssName: '',\n isColor,\n isValid: false,\n error: 'Property name cannot be empty',\n };\n }\n\n if (!isValidPropertyName(name)) {\n return {\n cssName: '',\n isColor,\n isValid: false,\n error: `Invalid property name \"${name}\". Must start with a letter or underscore, followed by letters, digits, hyphens, or underscores.`,\n };\n }\n\n // Build the CSS custom property name\n // For #name tokens, we add -color suffix\n // For legacy formats (--name-color or name-color), the name already includes -color\n let cssName: string;\n if (token.startsWith('#')) {\n // Color token: #name → --name-color\n cssName = `--${name}-color`;\n } else {\n // All other formats: just add -- prefix\n cssName = `--${name}`;\n }\n\n return {\n cssName,\n isColor,\n isValid: true,\n };\n}\n\n// ============================================================================\n// Normalization Functions\n// ============================================================================\n\n/**\n * Normalize a property name to the CSS custom property format.\n *\n * @deprecated Use parsePropertyToken instead for proper token handling\n */\nexport function normalizePropertyName(name: string): string {\n const result = parsePropertyToken(name);\n return result.isValid ? result.cssName : `--${name}`;\n}\n\n/**\n * Normalize a property definition to a consistent string representation.\n * Used for comparing definitions to detect type conflicts.\n *\n * Only `syntax` and `inherits` are compared — `initialValue` is intentionally\n * excluded because different components may set different defaults for the\n * same typed property (e.g. auto-inferred `0px` vs explicit `6px`).\n *\n * Keys are sorted alphabetically to ensure consistent comparison:\n * { inherits: true, syntax: '<color>' } === { syntax: '<color>', inherits: true }\n */\nexport function normalizePropertyDefinition(def: PropertyDefinition): string {\n const normalized: Record<string, unknown> = {};\n\n if (def.inherits !== undefined) {\n normalized.inherits = def.inherits;\n }\n if (def.syntax !== undefined) {\n normalized.syntax = def.syntax;\n }\n\n return JSON.stringify(normalized);\n}\n\n/**\n * Result of getEffectiveDefinition.\n */\nexport interface EffectiveDefinitionResult {\n /** The CSS custom property name */\n cssName: string;\n /** The effective property definition */\n definition: PropertyDefinition;\n /** Whether this is a color property */\n isColor: boolean;\n /** Whether the token was valid */\n isValid: boolean;\n /** Error message if invalid */\n error?: string;\n}\n\n/**\n * Get the effective property definition for a token.\n * For color tokens (#name), auto-sets syntax to '<color>' and defaults initialValue to 'transparent'.\n *\n * @param token - Property token ($name, #name, --name, or plain name)\n * @param userDefinition - User-provided definition options\n * @returns Effective definition with cssName, definition, isValid, and optional error\n */\nexport function getEffectiveDefinition(\n token: string,\n userDefinition: PropertyDefinition,\n): EffectiveDefinitionResult {\n const parsed = parsePropertyToken(token);\n\n if (!parsed.isValid) {\n return {\n cssName: '',\n definition: userDefinition,\n isColor: false,\n isValid: false,\n error: parsed.error,\n };\n }\n\n if (parsed.isColor) {\n // Color properties have fixed syntax and default initialValue\n return {\n cssName: parsed.cssName,\n definition: {\n syntax: '<color>', // Always '<color>' for color tokens, cannot be overridden\n inherits: userDefinition.inherits, // Allow inherits to be customized\n initialValue: userDefinition.initialValue ?? 'transparent', // Default to transparent\n },\n isColor: true,\n isValid: true,\n };\n }\n\n // Regular properties use the definition as-is\n return {\n cssName: parsed.cssName,\n definition: userDefinition,\n isColor: false,\n isValid: true,\n };\n}\n\n// ============================================================================\n// Color Utilities\n// ============================================================================\n\n/**\n * Convert a color initialValue to an RGB string for the companion `-rgb` property.\n * Used by SSR to emit `@property --name-color-rgb { syntax: \"<number>+\"; ... }`.\n */\nexport function colorInitialValueToRgb(\n initialValue: string | number | undefined,\n): string {\n if (initialValue == null) return '0 0 0';\n\n const val = String(initialValue).trim().toLowerCase();\n\n if (val === 'transparent' || val === 'rgba(0,0,0,0)' || val === '') {\n return '0 0 0';\n }\n\n // Named color: white\n if (val === 'white') return '255 255 255';\n if (val === 'black') return '0 0 0';\n\n // rgb(R G B) or rgb(R, G, B) — extract components\n const rgbMatch = val.match(/^rgba?\\(\\s*(\\d+)[,\\s]+(\\d+)[,\\s]+(\\d+)/);\n if (rgbMatch) {\n return `${rgbMatch[1]} ${rgbMatch[2]} ${rgbMatch[3]}`;\n }\n\n // Fallback for any other value\n return '0 0 0';\n}\n\n// ============================================================================\n// Value Type Inference\n// ============================================================================\n\n/**\n * Result of inferring a CSS @property syntax from a value.\n */\nexport interface InferredSyntax {\n syntax: string;\n initialValue: string;\n}\n\nconst UNIT_TO_SYNTAX: Record<string, InferredSyntax> = {};\n\nconst LENGTH_UNITS = [\n 'px',\n 'em',\n 'rem',\n 'vw',\n 'vh',\n 'vmin',\n 'vmax',\n 'ch',\n 'ex',\n 'cap',\n 'ic',\n 'lh',\n 'rlh',\n 'svw',\n 'svh',\n 'lvw',\n 'lvh',\n 'dvw',\n 'dvh',\n 'cqw',\n 'cqh',\n 'cqi',\n 'cqb',\n 'cqmin',\n 'cqmax',\n];\n\nconst ANGLE_UNITS = ['deg', 'rad', 'grad', 'turn'];\nconst TIME_UNITS = ['ms', 's'];\n\nfor (const u of LENGTH_UNITS) {\n UNIT_TO_SYNTAX[u] = { syntax: '<length-percentage>', initialValue: '0px' };\n}\nUNIT_TO_SYNTAX['%'] = { syntax: '<length-percentage>', initialValue: '0px' };\nfor (const u of ANGLE_UNITS) {\n UNIT_TO_SYNTAX[u] = { syntax: '<angle>', initialValue: '0deg' };\n}\nfor (const u of TIME_UNITS) {\n UNIT_TO_SYNTAX[u] = { syntax: '<time>', initialValue: '0s' };\n}\n\n/**\n * Infer CSS @property syntax from a concrete value.\n * Detects numeric types: \\<number\\>, \\<length-percentage\\>, \\<angle\\>, \\<time\\>.\n * Length and percentage values both map to \\<length-percentage\\> for maximum flexibility.\n * Color properties are handled separately via the #name token convention\n * (--name-color gets \\<color\\> syntax automatically in getEffectiveDefinition).\n *\n * @param value - The CSS value to infer from (e.g. '10px', '1', '45deg')\n * @returns Inferred syntax and initial value, or null if not inferable\n */\nexport function inferSyntaxFromValue(value: string): InferredSyntax | null {\n if (!value || typeof value !== 'string') return null;\n\n const trimmed = value.trim();\n if (!trimmed) return null;\n\n if (RE_NUMBER.test(trimmed)) {\n // Bare zero is ambiguous (could be <length>, <angle>, <percentage>, etc.)\n if (parseFloat(trimmed) === 0) return null;\n return { syntax: '<number>', initialValue: '0' };\n }\n\n const unitMatch = trimmed.match(RE_RAW_UNIT);\n if (unitMatch) {\n const unit = unitMatch[2];\n const mapping = UNIT_TO_SYNTAX[unit];\n if (mapping) return mapping;\n }\n\n return null;\n}\n"],"mappings":";;;AAmBA,MAAM,iBAAiB;;;;;AAMvB,MAAM,8BAA8B;;;;;AAUpC,SAAgB,oBAAoB,MAAuB;AACzD,QAAO,4BAA4B,KAAK,KAAK;;;;;;AAyB/C,SAAgB,mBAAmB,QAAyB;AAC1D,QAAO,kBAAkB;;;;;;AAO3B,SAAgB,uBACd,QAC2C;CAC3C,MAAM,aAAa,OAAO;AAC1B,KAAI,CAAC,cAAc,OAAO,eAAe,SACvC,QAAO;AAET,QAAO;;;;;;;;;;;;;;;AAoBT,SAAgB,mBAAmB,OAAoC;AACrE,KAAI,CAAC,SAAS,OAAO,UAAU,SAC7B,QAAO;EACL,SAAS;EACT,SAAS;EACT,SAAS;EACT,OAAO;EACR;CAGH,IAAI;CACJ,IAAI;AAEJ,KAAI,MAAM,WAAW,IAAI,EAAE;AAEzB,SAAO,MAAM,MAAM,EAAE;AACrB,YAAU;YACD,MAAM,WAAW,IAAI,EAAE;AAEhC,SAAO,MAAM,MAAM,EAAE;AACrB,YAAU;YACD,MAAM,WAAW,KAAK,EAAE;AAEjC,SAAO,MAAM,MAA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AG5B,MAAM,WAAW,IAAI,MAAM,yCAAyC;AACpE,KAAI,SACF,QAAO,GAAG,SAAS,GAAG,GAAG,SAAS,GAAG,GAAG,SAAS;AAInD,QAAO;;AAeT,MAAM,iBAAiD,EAAE;AAEzD,MAAM,eAAe;CACnB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD;AAED,MAAM,cAAc;CAAC;CAAO;CAAO;CAAQ;CAAO;AAClD,MAAM,aAAa,CAAC,MAAM,IAAI;AAE9B,KAAK,MAAM,KAAK,aACd,gBAAe,KAAK;CAAE,QAAQ;CAAuB,cAAc;CAAO;AAE5E,eAAe,OAAO;CAAE,QAAQ;CAAuB,cAAc;CAAO;AAC5E,KAAK,MAAM,KAAK,YACd,gBAAe,KAAK;CAAE,QAAQ;CAAW,cAAc;CAAQ;AAEjE,KAAK,MAAM,KAAK,WACd,gBAAe,KAAK;CAAE,QAAQ;CAAU,cAAc;CAAM;;;;;;;;;;;AAa9D,SAAgB,qBAAqB,OAAsC;AACzE,KAAI,CAAC,SAAS,OAAO,UAAU,SAAU,QAAO;CAEhD,MAAM,UAAU,MAAM,MAAM;AAC5B,KAAI,CAAC,QAAS,QAAO;AAErB,KAAI,UAAU,KAAK,QAAQ,EAAE;AAE3B,MAAI,WAAW,QAAQ,KAAK,EAAG,QAAO;AACtC,SAAO;GAAE,QAAQ;GAAY,cAAc;GAAK;;CAGlD,MAAM,YAAY,QAAQ,MAAM,YAAY;AAC5C,KAAI,WAAW;EAEb,MAAM,UAAU,eADH,UAAU;AAEvB,MAAI,QAAS,QAAO;;AAGtB,QAAO"}
|
|
@@ -32,6 +32,10 @@ var PropertyTypeResolver = class {
|
|
|
32
32
|
registerProperty(propName, "<color>", "transparent");
|
|
33
33
|
continue;
|
|
34
34
|
}
|
|
35
|
+
if (propName.endsWith("-line-height")) {
|
|
36
|
+
registerProperty(propName, "<number> | <length-percentage>", "0");
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
35
39
|
const varMatch = SINGLE_VAR_REF.exec(value);
|
|
36
40
|
if (varMatch) {
|
|
37
41
|
const depName = varMatch[1];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"property-type-resolver.js","names":[],"sources":["../../src/properties/property-type-resolver.ts"],"sourcesContent":["/**\n * PropertyTypeResolver\n *\n * Automatically infers CSS @property types from custom property values.\n * Supports deferred resolution for var() reference chains of arbitrary depth.\n */\n\nimport { inferSyntaxFromValue } from './index';\n\nconst CUSTOM_PROP_DECL = /^\\s*(--[a-z0-9_-]+)\\s*:\\s*(.+?)\\s*$/i;\nconst SINGLE_VAR_REF = /^var\\((--[a-z0-9_-]+)\\)$/i;\n\nexport class PropertyTypeResolver {\n /** propName → the prop it depends on */\n private pendingDeps = new Map<string, string>();\n /** propName → list of props waiting on it */\n private reverseDeps = new Map<string, string[]>();\n\n /**\n * Scan CSS declarations and auto-register @property for custom properties\n * whose types can be inferred from their values.\n */\n scanDeclarations(\n declarations: string,\n isPropertyDefined: (name: string) => boolean,\n registerProperty: (\n name: string,\n syntax: string,\n initialValue: string,\n ) => void,\n ): void {\n if (!declarations.includes('--')) return;\n\n const parts = declarations.split(/;+/);\n\n for (const part of parts) {\n if (!part.trim()) continue;\n\n const match = CUSTOM_PROP_DECL.exec(part);\n if (!match) continue;\n\n const propName = match[1];\n const value = match[2].trim();\n\n if (isPropertyDefined(propName)) continue;\n\n // Name-based: --*-color properties are always <color> (from #name tokens)\n if (propName.endsWith('-color')) {\n registerProperty(propName, '<color>', 'transparent');\n continue;\n }\n\n // Single var() reference → record dependency for deferred resolution\n const varMatch = SINGLE_VAR_REF.exec(value);\n if (varMatch) {\n const depName = varMatch[1];\n this.addDependency(propName, depName);\n continue;\n }\n\n // Skip complex expressions (calc, multiple var, etc.)\n if (this.isComplexValue(value)) continue;\n\n const inferred = inferSyntaxFromValue(value);\n if (!inferred) continue;\n\n this.resolve(\n propName,\n inferred.syntax,\n inferred.initialValue,\n isPropertyDefined,\n registerProperty,\n );\n }\n }\n\n private addDependency(propName: string, depName: string): void {\n if (propName === depName) return;\n\n this.pendingDeps.set(propName, depName);\n\n let dependents = this.reverseDeps.get(depName);\n if (!dependents) {\n dependents = [];\n this.reverseDeps.set(depName, dependents);\n }\n if (!dependents.includes(propName)) {\n dependents.push(propName);\n }\n }\n\n private resolve(\n propName: string,\n syntax: string,\n initialValue: string,\n isPropertyDefined: (name: string) => boolean,\n registerProperty: (\n name: string,\n syntax: string,\n initialValue: string,\n ) => void,\n resolving?: Set<string>,\n ): void {\n if (!resolving) resolving = new Set();\n if (resolving.has(propName)) return;\n resolving.add(propName);\n\n if (!isPropertyDefined(propName)) {\n registerProperty(propName, syntax, initialValue);\n }\n\n const dependents = this.reverseDeps.get(propName);\n if (dependents) {\n this.reverseDeps.delete(propName);\n\n for (const dependent of dependents) {\n this.pendingDeps.delete(dependent);\n\n if (isPropertyDefined(dependent)) continue;\n\n this.resolve(\n dependent,\n syntax,\n initialValue,\n isPropertyDefined,\n registerProperty,\n resolving,\n );\n }\n }\n }\n\n private isComplexValue(value: string): boolean {\n if (value.includes('calc(')) return true;\n const firstVar = value.indexOf('var(');\n if (firstVar === -1) return false;\n if (value.indexOf('var(', firstVar + 4) !== -1) return true;\n return !SINGLE_VAR_REF.test(value);\n }\n}\n"],"mappings":";;;;;;;;;AASA,MAAM,mBAAmB;AACzB,MAAM,iBAAiB;AAEvB,IAAa,uBAAb,MAAkC;;CAEhC,AAAQ,8BAAc,IAAI,KAAqB;;CAE/C,AAAQ,8BAAc,IAAI,KAAuB;;;;;CAMjD,iBACE,cACA,mBACA,kBAKM;AACN,MAAI,CAAC,aAAa,SAAS,KAAK,CAAE;EAElC,MAAM,QAAQ,aAAa,MAAM,KAAK;AAEtC,OAAK,MAAM,QAAQ,OAAO;AACxB,OAAI,CAAC,KAAK,MAAM,CAAE;GAElB,MAAM,QAAQ,iBAAiB,K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AQ,eAAe,OAAwB;AAC7C,MAAI,MAAM,SAAS,QAAQ,CAAE,QAAO;EACpC,MAAM,WAAW,MAAM,QAAQ,OAAO;AACtC,MAAI,aAAa,GAAI,QAAO;AAC5B,MAAI,MAAM,QAAQ,QAAQ,WAAW,EAAE,KAAK,GAAI,QAAO;AACvD,SAAO,CAAC,eAAe,KAAK,MAAM"}
|
|
1
|
+
{"version":3,"file":"property-type-resolver.js","names":[],"sources":["../../src/properties/property-type-resolver.ts"],"sourcesContent":["/**\n * PropertyTypeResolver\n *\n * Automatically infers CSS @property types from custom property values.\n * Supports deferred resolution for var() reference chains of arbitrary depth.\n */\n\nimport { inferSyntaxFromValue } from './index';\n\nconst CUSTOM_PROP_DECL = /^\\s*(--[a-z0-9_-]+)\\s*:\\s*(.+?)\\s*$/i;\nconst SINGLE_VAR_REF = /^var\\((--[a-z0-9_-]+)\\)$/i;\n\nexport class PropertyTypeResolver {\n /** propName → the prop it depends on */\n private pendingDeps = new Map<string, string>();\n /** propName → list of props waiting on it */\n private reverseDeps = new Map<string, string[]>();\n\n /**\n * Scan CSS declarations and auto-register @property for custom properties\n * whose types can be inferred from their values.\n */\n scanDeclarations(\n declarations: string,\n isPropertyDefined: (name: string) => boolean,\n registerProperty: (\n name: string,\n syntax: string,\n initialValue: string,\n ) => void,\n ): void {\n if (!declarations.includes('--')) return;\n\n const parts = declarations.split(/;+/);\n\n for (const part of parts) {\n if (!part.trim()) continue;\n\n const match = CUSTOM_PROP_DECL.exec(part);\n if (!match) continue;\n\n const propName = match[1];\n const value = match[2].trim();\n\n if (isPropertyDefined(propName)) continue;\n\n // Name-based: --*-color properties are always <color> (from #name tokens)\n if (propName.endsWith('-color')) {\n registerProperty(propName, '<color>', 'transparent');\n continue;\n }\n\n // Name-based: --*-line-height accepts numbers, lengths, and percentages\n if (propName.endsWith('-line-height')) {\n registerProperty(propName, '<number> | <length-percentage>', '0');\n continue;\n }\n\n // Single var() reference → record dependency for deferred resolution\n const varMatch = SINGLE_VAR_REF.exec(value);\n if (varMatch) {\n const depName = varMatch[1];\n this.addDependency(propName, depName);\n continue;\n }\n\n // Skip complex expressions (calc, multiple var, etc.)\n if (this.isComplexValue(value)) continue;\n\n const inferred = inferSyntaxFromValue(value);\n if (!inferred) continue;\n\n this.resolve(\n propName,\n inferred.syntax,\n inferred.initialValue,\n isPropertyDefined,\n registerProperty,\n );\n }\n }\n\n private addDependency(propName: string, depName: string): void {\n if (propName === depName) return;\n\n this.pendingDeps.set(propName, depName);\n\n let dependents = this.reverseDeps.get(depName);\n if (!dependents) {\n dependents = [];\n this.reverseDeps.set(depName, dependents);\n }\n if (!dependents.includes(propName)) {\n dependents.push(propName);\n }\n }\n\n private resolve(\n propName: string,\n syntax: string,\n initialValue: string,\n isPropertyDefined: (name: string) => boolean,\n registerProperty: (\n name: string,\n syntax: string,\n initialValue: string,\n ) => void,\n resolving?: Set<string>,\n ): void {\n if (!resolving) resolving = new Set();\n if (resolving.has(propName)) return;\n resolving.add(propName);\n\n if (!isPropertyDefined(propName)) {\n registerProperty(propName, syntax, initialValue);\n }\n\n const dependents = this.reverseDeps.get(propName);\n if (dependents) {\n this.reverseDeps.delete(propName);\n\n for (const dependent of dependents) {\n this.pendingDeps.delete(dependent);\n\n if (isPropertyDefined(dependent)) continue;\n\n this.resolve(\n dependent,\n syntax,\n initialValue,\n isPropertyDefined,\n registerProperty,\n resolving,\n );\n }\n }\n }\n\n private isComplexValue(value: string): boolean {\n if (value.includes('calc(')) return true;\n const firstVar = value.indexOf('var(');\n if (firstVar === -1) return false;\n if (value.indexOf('var(', firstVar + 4) !== -1) return true;\n return !SINGLE_VAR_REF.test(value);\n }\n}\n"],"mappings":";;;;;;;;;AASA,MAAM,mBAAmB;AACzB,MAAM,iBAAiB;AAEvB,IAAa,uBAAb,MAAkC;;CAEhC,AAAQ,8BAAc,IAAI,KAAqB;;CAE/C,AAAQ,8BAAc,IAAI,KAAuB;;;;;CAMjD,iBACE,cACA,mBACA,kBAKM;AACN,MA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a;EAGlD,MAAM,aAAa,KAAK,YAAY,IAAI,SAAS;AACjD,MAAI,YAAY;AACd,QAAK,YAAY,OAAO,SAAS;AAEjC,QAAK,MAAM,aAAa,YAAY;AAClC,SAAK,YAAY,OAAO,UAAU;AAElC,QAAI,kBAAkB,UAAU,CAAE;AAElC,SAAK,QACH,WACA,QACA,cACA,mBACA,kBACA,UACD;;;;CAKP,AAAQ,eAAe,OAAwB;AAC7C,MAAI,MAAM,SAAS,QAAQ,CAAE,QAAO;EACpC,MAAM,WAAW,MAAM,QAAQ,OAAO;AACtC,MAAI,aAAa,GAAI,QAAO;AAC5B,MAAI,MAAM,QAAQ,QAAQ,WAAW,EAAE,KAAK,GAAI,QAAO;AACvD,SAAO,CAAC,eAAe,KAAK,MAAM"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { hydrateTastyCache } from "./hydrate.js";
|
|
2
|
+
|
|
3
|
+
//#region src/ssr/astro.d.ts
|
|
4
|
+
interface TastyMiddlewareOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Whether to embed the cache state script for client hydration.
|
|
7
|
+
* Set to false to skip cache transfer. Default: true.
|
|
8
|
+
*/
|
|
9
|
+
transferCache?: boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Create an Astro middleware that collects Tasty styles during SSR.
|
|
13
|
+
*
|
|
14
|
+
* All React components rendered during the request (both static
|
|
15
|
+
* and islands) will have their useStyles() calls captured by the
|
|
16
|
+
* collector via AsyncLocalStorage. After rendering, the middleware
|
|
17
|
+
* injects the collected CSS into </head>.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* // src/middleware.ts
|
|
22
|
+
* import { tastyMiddleware } from '@tenphi/tasty/ssr/astro';
|
|
23
|
+
* export const onRequest = tastyMiddleware();
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
declare function tastyMiddleware(options?: TastyMiddlewareOptions): (_context: unknown, next: () => Promise<Response>) => Promise<Response>;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { TastyMiddlewareOptions, hydrateTastyCache, tastyMiddleware };
|
|
29
|
+
//# sourceMappingURL=astro.d.ts.map
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { getConfig } from "../config.js";
|
|
2
|
+
import { registerSSRCollectorGetter } from "./ssr-collector-ref.js";
|
|
3
|
+
import { ServerStyleCollector } from "./collector.js";
|
|
4
|
+
import { getSSRCollector, runWithCollector } from "./async-storage.js";
|
|
5
|
+
import { hydrateTastyCache } from "./hydrate.js";
|
|
6
|
+
|
|
7
|
+
//#region src/ssr/astro.ts
|
|
8
|
+
/**
|
|
9
|
+
* Astro integration for Tasty SSR.
|
|
10
|
+
*
|
|
11
|
+
* Provides tastyMiddleware() for Astro's middleware system.
|
|
12
|
+
* The middleware wraps request handling in a ServerStyleCollector
|
|
13
|
+
* via AsyncLocalStorage, then injects collected CSS into </head>.
|
|
14
|
+
*
|
|
15
|
+
* Import from '@tenphi/tasty/ssr/astro'.
|
|
16
|
+
*/
|
|
17
|
+
registerSSRCollectorGetter(getSSRCollector);
|
|
18
|
+
/**
|
|
19
|
+
* Create an Astro middleware that collects Tasty styles during SSR.
|
|
20
|
+
*
|
|
21
|
+
* All React components rendered during the request (both static
|
|
22
|
+
* and islands) will have their useStyles() calls captured by the
|
|
23
|
+
* collector via AsyncLocalStorage. After rendering, the middleware
|
|
24
|
+
* injects the collected CSS into </head>.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* // src/middleware.ts
|
|
29
|
+
* import { tastyMiddleware } from '@tenphi/tasty/ssr/astro';
|
|
30
|
+
* export const onRequest = tastyMiddleware();
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
function tastyMiddleware(options) {
|
|
34
|
+
const { transferCache = true } = options ?? {};
|
|
35
|
+
return async (_context, next) => {
|
|
36
|
+
const collector = new ServerStyleCollector();
|
|
37
|
+
const response = await runWithCollector(collector, () => next());
|
|
38
|
+
const css = collector.getCSS();
|
|
39
|
+
if (!css) return response;
|
|
40
|
+
const nonce = getConfig().nonce;
|
|
41
|
+
const nonceAttr = nonce ? ` nonce="${nonce}"` : "";
|
|
42
|
+
const html = await response.text();
|
|
43
|
+
const styleTag = `<style data-tasty-ssr${nonceAttr}>${css}</style>`;
|
|
44
|
+
let cacheTag = "";
|
|
45
|
+
if (transferCache) {
|
|
46
|
+
const cacheState = collector.getCacheState();
|
|
47
|
+
if (Object.keys(cacheState.entries).length > 0) cacheTag = `<script data-tasty-cache type="application/json"${nonceAttr}>${JSON.stringify(cacheState)}<\/script>`;
|
|
48
|
+
}
|
|
49
|
+
const modifiedHtml = html.replace("</head>", `${styleTag}${cacheTag}</head>`);
|
|
50
|
+
return new Response(modifiedHtml, {
|
|
51
|
+
status: response.status,
|
|
52
|
+
headers: response.headers
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (typeof window !== "undefined") {
|
|
57
|
+
const script = document.querySelector("script[data-tasty-cache]");
|
|
58
|
+
if (script) try {
|
|
59
|
+
hydrateTastyCache(JSON.parse(script.textContent));
|
|
60
|
+
} catch {}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
export { hydrateTastyCache, tastyMiddleware };
|
|
65
|
+
//# sourceMappingURL=astro.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"astro.js","names":[],"sources":["../../src/ssr/astro.ts"],"sourcesContent":["/**\n * Astro integration for Tasty SSR.\n *\n * Provides tastyMiddleware() for Astro's middleware system.\n * The middleware wraps request handling in a ServerStyleCollector\n * via AsyncLocalStorage, then injects collected CSS into </head>.\n *\n * Import from '@tenphi/tasty/ssr/astro'.\n */\n\nimport { getConfig } from '../config';\nimport { getSSRCollector, runWithCollector } from './async-storage';\nimport { ServerStyleCollector } from './collector';\nimport { hydrateTastyCache } from './hydrate';\nimport { registerSSRCollectorGetter } from './ssr-collector-ref';\n\n// Wire up ALS-based collector discovery so useStyles can find\n// the collector set by tastyMiddleware's runWithCollector().\nregisterSSRCollectorGetter(getSSRCollector);\n\n// Re-export for convenience\nexport { hydrateTastyCache };\n\nexport interface TastyMiddlewareOptions {\n /**\n * Whether to embed the cache state script for client hydration.\n * Set to false to skip cache transfer. Default: true.\n */\n transferCache?: boolean;\n}\n\n/**\n * Create an Astro middleware that collects Tasty styles during SSR.\n *\n * All React components rendered during the request (both static\n * and islands) will have their useStyles() calls captured by the\n * collector via AsyncLocalStorage. After rendering, the middleware\n * injects the collected CSS into </head>.\n *\n * @example\n * ```ts\n * // src/middleware.ts\n * import { tastyMiddleware } from '@tenphi/tasty/ssr/astro';\n * export const onRequest = tastyMiddleware();\n * ```\n */\nexport function tastyMiddleware(options?: TastyMiddlewareOptions) {\n const { transferCache = true } = options ?? {};\n\n return async (\n _context: unknown,\n next: () => Promise<Response>,\n ): Promise<Response> => {\n const collector = new ServerStyleCollector();\n\n const response = await runWithCollector(collector, () => next());\n\n const css = collector.getCSS();\n if (!css) return response;\n\n const nonce = getConfig().nonce;\n const nonceAttr = nonce ? ` nonce=\"${nonce}\"` : '';\n const html = await response.text();\n const styleTag = `<style data-tasty-ssr${nonceAttr}>${css}</style>`;\n\n let cacheTag = '';\n if (transferCache) {\n const cacheState = collector.getCacheState();\n const hasHydratableStyles = Object.keys(cacheState.entries).length > 0;\n if (hasHydratableStyles) {\n cacheTag = `<script data-tasty-cache type=\"application/json\"${nonceAttr}>${JSON.stringify(cacheState)}</script>`;\n }\n }\n\n const modifiedHtml = html.replace(\n '</head>',\n `${styleTag}${cacheTag}</head>`,\n );\n\n return new Response(modifiedHtml, {\n status: response.status,\n headers: response.headers,\n });\n };\n}\n\n// Client-side auto-hydration.\n// When imported in the browser, reads the cache state from the DOM\n// and pre-populates the injector before any island hydrates.\nif (typeof window !== 'undefined') {\n const script = document.querySelector('script[data-tasty-cache]');\n if (script) {\n try {\n const state = JSON.parse(script.textContent!);\n hydrateTastyCache(state);\n } catch {\n // Ignore malformed cache state\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAkBA,2BAA2B,gBAAgB;;;;;;;;;;;;;;;;AA4B3C,SAAgB,gBAAgB,SAAkC;CAChE,MAAM,EAAE,gBAAgB,SAAS,WAAW,EAAE;AAE9C,QAAO,OACL,UACA,SACsB;EACtB,MAAM,YAAY,IAAI,sBAAsB;EAE5C,MAAM,WAAW,MAAM,iBAAiB,iBAAiB,MAAM,CAAC;EAEhE,MAAM,MAAM,UAAU,QAAQ;AAC9B,MAAI,CAAC,IAAK,QAAO;EAEjB,MAAM,QAAQ,WAAW,CAAC;EAC1B,MAAM,YAAY,QAAQ,WAAW,MAAM,KAAK;EAChD,MAAM,OAAO,MAAM,SAAS,MAAM;EAClC,MAAM,WAAW,wBAAwB,UAAU,GAAG,IAAI;EAE1D,IAAI,WAAW;AACf,MAAI,eAAe;GACjB,MAAM,aAAa,UAAU,eAAe;AAE5C,OAD4B,OAAO,KAAK,WAAW,QAAQ,CAAC,SAAS,EAEnE,YAAW,mDAAmD,UAAU,GAAG,KAAK,UAAU,WAAW,CAAC;;EAI1G,MAAM,eAAe,KAAK,QACxB,WACA,GAAG,WAAW,SAAS,SACxB;AAED,SAAO,IAAI,SAAS,cAAc;GAChC,QAAQ,SAAS;GACjB,SAAS,SAAS;GACnB,CAAC;;;AAON,IAAI,OAAO,WAAW,aAAa;CACjC,MAAM,SAAS,SAAS,cAAc,2BAA2B;AACjE,KAAI,OACF,KAAI;AAEF,oBADc,KAAK,MAAM,OAAO,YAAa,CACrB;SAClB"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ServerStyleCollector } from "./collector.js";
|
|
2
|
+
|
|
3
|
+
//#region src/ssr/async-storage.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Run a function with a ServerStyleCollector bound to the current
|
|
6
|
+
* async context. All useStyles() calls within `fn` (and any async
|
|
7
|
+
* continuations) will find this collector via getSSRCollector().
|
|
8
|
+
*/
|
|
9
|
+
declare function runWithCollector<T>(collector: ServerStyleCollector, fn: () => T): T;
|
|
10
|
+
/**
|
|
11
|
+
* Retrieve the ServerStyleCollector bound to the current async context.
|
|
12
|
+
* Returns null when called outside of runWithCollector() or on the client.
|
|
13
|
+
*/
|
|
14
|
+
declare function getSSRCollector(): ServerStyleCollector | null;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { getSSRCollector, runWithCollector };
|
|
17
|
+
//# sourceMappingURL=async-storage.d.ts.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
|
|
3
|
+
//#region src/ssr/async-storage.ts
|
|
4
|
+
/**
|
|
5
|
+
* AsyncLocalStorage integration for SSR collector discovery.
|
|
6
|
+
*
|
|
7
|
+
* Used by Astro middleware and generic framework integrations where
|
|
8
|
+
* the library cannot wrap the React tree with a context provider.
|
|
9
|
+
* The middleware calls runWithCollector() around the render, and
|
|
10
|
+
* useStyles() calls getSSRCollector() to find it.
|
|
11
|
+
*
|
|
12
|
+
* This module imports from 'node:async_hooks' — it must be excluded
|
|
13
|
+
* from client bundles via the build configuration.
|
|
14
|
+
*/
|
|
15
|
+
const tastySSRStorage = new AsyncLocalStorage();
|
|
16
|
+
/**
|
|
17
|
+
* Run a function with a ServerStyleCollector bound to the current
|
|
18
|
+
* async context. All useStyles() calls within `fn` (and any async
|
|
19
|
+
* continuations) will find this collector via getSSRCollector().
|
|
20
|
+
*/
|
|
21
|
+
function runWithCollector(collector, fn) {
|
|
22
|
+
return tastySSRStorage.run(collector, fn);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Retrieve the ServerStyleCollector bound to the current async context.
|
|
26
|
+
* Returns null when called outside of runWithCollector() or on the client.
|
|
27
|
+
*/
|
|
28
|
+
function getSSRCollector() {
|
|
29
|
+
if (typeof tastySSRStorage?.getStore !== "function") return null;
|
|
30
|
+
return tastySSRStorage.getStore() ?? null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
export { getSSRCollector, runWithCollector };
|
|
35
|
+
//# sourceMappingURL=async-storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-storage.js","names":[],"sources":["../../src/ssr/async-storage.ts"],"sourcesContent":["/**\n * AsyncLocalStorage integration for SSR collector discovery.\n *\n * Used by Astro middleware and generic framework integrations where\n * the library cannot wrap the React tree with a context provider.\n * The middleware calls runWithCollector() around the render, and\n * useStyles() calls getSSRCollector() to find it.\n *\n * This module imports from 'node:async_hooks' — it must be excluded\n * from client bundles via the build configuration.\n */\n\nimport { AsyncLocalStorage } from 'node:async_hooks';\n\nimport type { ServerStyleCollector } from './collector';\n\nconst tastySSRStorage = new AsyncLocalStorage<ServerStyleCollector>();\n\n/**\n * Run a function with a ServerStyleCollector bound to the current\n * async context. All useStyles() calls within `fn` (and any async\n * continuations) will find this collector via getSSRCollector().\n */\nexport function runWithCollector<T>(\n collector: ServerStyleCollector,\n fn: () => T,\n): T {\n return tastySSRStorage.run(collector, fn);\n}\n\n/**\n * Retrieve the ServerStyleCollector bound to the current async context.\n * Returns null when called outside of runWithCollector() or on the client.\n */\nexport function getSSRCollector(): ServerStyleCollector | null {\n if (typeof tastySSRStorage?.getStore !== 'function') return null;\n return tastySSRStorage.getStore() ?? null;\n}\n"],"mappings":";;;;;;;;;;;;;;AAgBA,MAAM,kBAAkB,IAAI,mBAAyC;;;;;;AAOrE,SAAgB,iBACd,WACA,IACG;AACH,QAAO,gBAAgB,IAAI,WAAW,GAAG;;;;;;AAO3C,SAAgB,kBAA+C;AAC7D,KAAI,OAAO,iBAAiB,aAAa,WAAY,QAAO;AAC5D,QAAO,gBAAgB,UAAU,IAAI"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { parsePropertyToken } from "../properties/index.js";
|
|
2
|
+
import { PropertyTypeResolver } from "../properties/property-type-resolver.js";
|
|
3
|
+
import { formatPropertyCSS } from "./format-property.js";
|
|
4
|
+
|
|
5
|
+
//#region src/ssr/collect-auto-properties.ts
|
|
6
|
+
/**
|
|
7
|
+
* Scan rendered rules for custom property declarations and collect
|
|
8
|
+
* auto-inferred @property rules via the SSR collector.
|
|
9
|
+
*
|
|
10
|
+
* @param rules - Rendered style rules containing CSS declarations
|
|
11
|
+
* @param collector - SSR collector to emit @property CSS into
|
|
12
|
+
* @param styles - Original styles object (used to skip explicit @properties)
|
|
13
|
+
*/
|
|
14
|
+
function collectAutoInferredProperties(rules, collector, styles) {
|
|
15
|
+
const registered = /* @__PURE__ */ new Set();
|
|
16
|
+
if (styles) {
|
|
17
|
+
const localProps = styles["@properties"];
|
|
18
|
+
if (localProps && typeof localProps === "object") for (const token of Object.keys(localProps)) {
|
|
19
|
+
const parsed = parsePropertyToken(token);
|
|
20
|
+
if (parsed.isValid) registered.add(parsed.cssName);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const resolver = new PropertyTypeResolver();
|
|
24
|
+
for (const rule of rules) {
|
|
25
|
+
if (!rule.declarations) continue;
|
|
26
|
+
resolver.scanDeclarations(rule.declarations, (name) => registered.has(name), (name, syntax, initialValue) => {
|
|
27
|
+
registered.add(name);
|
|
28
|
+
const css = formatPropertyCSS(name, {
|
|
29
|
+
syntax,
|
|
30
|
+
inherits: true,
|
|
31
|
+
initialValue
|
|
32
|
+
});
|
|
33
|
+
if (css) collector.collectProperty(`__auto:${name}`, css);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
//#endregion
|
|
39
|
+
export { collectAutoInferredProperties };
|
|
40
|
+
//# sourceMappingURL=collect-auto-properties.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collect-auto-properties.js","names":[],"sources":["../../src/ssr/collect-auto-properties.ts"],"sourcesContent":["/**\n * SSR auto-property inference.\n *\n * Scans rendered CSS declarations for custom properties whose types\n * can be inferred from their values (e.g. `--angle: 30deg` → `<angle>`).\n * Mirrors the client-side auto-inference in StyleInjector.inject().\n */\n\nimport type { StyleResult } from '../pipeline';\nimport { parsePropertyToken } from '../properties';\nimport { PropertyTypeResolver } from '../properties/property-type-resolver';\nimport type { Styles } from '../styles/types';\n\nimport type { ServerStyleCollector } from './collector';\nimport { formatPropertyCSS } from './format-property';\n\n/**\n * Scan rendered rules for custom property declarations and collect\n * auto-inferred @property rules via the SSR collector.\n *\n * @param rules - Rendered style rules containing CSS declarations\n * @param collector - SSR collector to emit @property CSS into\n * @param styles - Original styles object (used to skip explicit @properties)\n */\nexport function collectAutoInferredProperties(\n rules: StyleResult[],\n collector: ServerStyleCollector,\n styles?: Styles,\n): void {\n const registered = new Set<string>();\n\n if (styles) {\n const localProps = styles['@properties'];\n if (localProps && typeof localProps === 'object') {\n for (const token of Object.keys(localProps as Record<string, unknown>)) {\n const parsed = parsePropertyToken(token);\n if (parsed.isValid) {\n registered.add(parsed.cssName);\n }\n }\n }\n }\n\n const resolver = new PropertyTypeResolver();\n\n for (const rule of rules) {\n if (!rule.declarations) continue;\n resolver.scanDeclarations(\n rule.declarations,\n (name) => registered.has(name),\n (name, syntax, initialValue) => {\n registered.add(name);\n const css = formatPropertyCSS(name, {\n syntax,\n inherits: true,\n initialValue,\n });\n if (css) {\n collector.collectProperty(`__auto:${name}`, css);\n }\n },\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAwBA,SAAgB,8BACd,OACA,WACA,QACM;CACN,MAAM,6BAAa,IAAI,KAAa;AAEpC,KAAI,QAAQ;EACV,MAAM,aAAa,OAAO;AAC1B,MAAI,cAAc,OAAO,eAAe,SACtC,MAAK,MAAM,SAAS,OAAO,KAAK,WAAsC,EAAE;GACtE,MAAM,SAAS,mBAAmB,MAAM;AACxC,OAAI,OAAO,QACT,YAAW,IAAI,OAAO,QAAQ;;;CAMtC,MAAM,WAAW,IAAI,sBAAsB;AAE3C,MAAK,MAAM,QAAQ,OAAO;AACxB,MAAI,CAAC,KAAK,aAAc;AACxB,WAAS,iBACP,KAAK,eACJ,SAAS,WAAW,IAAI,KAAK,GAC7B,MAAM,QAAQ,iBAAiB;AAC9B,cAAW,IAAI,KAAK;GACpB,MAAM,MAAM,kBAAkB,MAAM;IAClC;IACA,UAAU;IACV;IACD,CAAC;AACF,OAAI,IACF,WAAU,gBAAgB,UAAU,QAAQ,IAAI;IAGrD"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { StyleResult } from "../pipeline/index.js";
|
|
2
|
+
|
|
3
|
+
//#region src/ssr/collector.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Cache state serialized to the client for hydration.
|
|
6
|
+
*/
|
|
7
|
+
interface SSRCacheState {
|
|
8
|
+
/** cacheKey → className map, to pre-populate the client registry */
|
|
9
|
+
entries: Record<string, string>;
|
|
10
|
+
/** Counter value so client allocations don't collide with server ones */
|
|
11
|
+
classCounter: number;
|
|
12
|
+
}
|
|
13
|
+
declare class ServerStyleCollector {
|
|
14
|
+
private chunks;
|
|
15
|
+
private cacheKeyToClassName;
|
|
16
|
+
private classCounter;
|
|
17
|
+
private flushedKeys;
|
|
18
|
+
private propertyRules;
|
|
19
|
+
private flushedPropertyKeys;
|
|
20
|
+
private keyframeRules;
|
|
21
|
+
private flushedKeyframeKeys;
|
|
22
|
+
private globalStyles;
|
|
23
|
+
private flushedGlobalKeys;
|
|
24
|
+
private rawCSS;
|
|
25
|
+
private flushedRawKeys;
|
|
26
|
+
private keyframesCounter;
|
|
27
|
+
private internalsCollected;
|
|
28
|
+
/**
|
|
29
|
+
* Collect internal @property rules and :root token defaults.
|
|
30
|
+
* Mirrors markStylesGenerated() from the client-side injector.
|
|
31
|
+
* Called automatically on first chunk collection; idempotent.
|
|
32
|
+
*/
|
|
33
|
+
collectInternals(): void;
|
|
34
|
+
/**
|
|
35
|
+
* Allocate a className for a cache key, server-side.
|
|
36
|
+
* Mirrors StyleInjector.allocateClassName but without DOM access.
|
|
37
|
+
*/
|
|
38
|
+
allocateClassName(cacheKey: string): {
|
|
39
|
+
className: string;
|
|
40
|
+
isNewAllocation: boolean;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Record CSS rules for a chunk.
|
|
44
|
+
* Called by useStyles during server render.
|
|
45
|
+
*/
|
|
46
|
+
collectChunk(cacheKey: string, className: string, rules: StyleResult[]): void;
|
|
47
|
+
/**
|
|
48
|
+
* Record a @property rule. Deduplicated by name.
|
|
49
|
+
*/
|
|
50
|
+
collectProperty(name: string, css: string): void;
|
|
51
|
+
/**
|
|
52
|
+
* Record a @keyframes rule. Deduplicated by name.
|
|
53
|
+
*/
|
|
54
|
+
collectKeyframes(name: string, css: string): void;
|
|
55
|
+
/**
|
|
56
|
+
* Allocate a keyframe name for SSR. Uses provided name or generates one.
|
|
57
|
+
*/
|
|
58
|
+
allocateKeyframeName(providedName?: string): string;
|
|
59
|
+
/**
|
|
60
|
+
* Record global styles (from useGlobalStyles). Deduplicated by key.
|
|
61
|
+
*/
|
|
62
|
+
collectGlobalStyles(key: string, css: string): void;
|
|
63
|
+
/**
|
|
64
|
+
* Record raw CSS text (from useRawCSS). Deduplicated by key.
|
|
65
|
+
*/
|
|
66
|
+
collectRawCSS(key: string, css: string): void;
|
|
67
|
+
/**
|
|
68
|
+
* Extract all CSS collected so far as a single string.
|
|
69
|
+
* Includes @property and @keyframes rules.
|
|
70
|
+
* Used for non-streaming SSR (renderToString).
|
|
71
|
+
*/
|
|
72
|
+
getCSS(): string;
|
|
73
|
+
/**
|
|
74
|
+
* Flush only newly collected CSS since the last flush.
|
|
75
|
+
* Used for streaming SSR (renderToPipeableStream + useServerInsertedHTML).
|
|
76
|
+
*/
|
|
77
|
+
flushCSS(): string;
|
|
78
|
+
/**
|
|
79
|
+
* Serialize the cache state for client hydration.
|
|
80
|
+
*/
|
|
81
|
+
getCacheState(): SSRCacheState;
|
|
82
|
+
}
|
|
83
|
+
//#endregion
|
|
84
|
+
export { SSRCacheState, ServerStyleCollector };
|
|
85
|
+
//# sourceMappingURL=collector.d.ts.map
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { INTERNAL_PROPERTIES, INTERNAL_TOKENS, getGlobalProperties, hasGlobalProperties } from "../config.js";
|
|
2
|
+
import { formatPropertyCSS } from "./format-property.js";
|
|
3
|
+
import { formatRules } from "./format-rules.js";
|
|
4
|
+
|
|
5
|
+
//#region src/ssr/collector.ts
|
|
6
|
+
/**
|
|
7
|
+
* ServerStyleCollector — server-safe style collector for SSR.
|
|
8
|
+
*
|
|
9
|
+
* Accumulates CSS rules and cache metadata during server rendering.
|
|
10
|
+
* This is the server-side counterpart to StyleInjector: it allocates
|
|
11
|
+
* sequential class names (t0, t1, …), formats CSS rules into text,
|
|
12
|
+
* and serializes the cache state for client hydration.
|
|
13
|
+
*
|
|
14
|
+
* One instance is created per HTTP request. Concurrent requests
|
|
15
|
+
* each get their own collector (via AsyncLocalStorage or React context).
|
|
16
|
+
*/
|
|
17
|
+
function generateClassName(counter) {
|
|
18
|
+
return `t${counter}`;
|
|
19
|
+
}
|
|
20
|
+
var ServerStyleCollector = class {
|
|
21
|
+
chunks = /* @__PURE__ */ new Map();
|
|
22
|
+
cacheKeyToClassName = /* @__PURE__ */ new Map();
|
|
23
|
+
classCounter = 0;
|
|
24
|
+
flushedKeys = /* @__PURE__ */ new Set();
|
|
25
|
+
propertyRules = /* @__PURE__ */ new Map();
|
|
26
|
+
flushedPropertyKeys = /* @__PURE__ */ new Set();
|
|
27
|
+
keyframeRules = /* @__PURE__ */ new Map();
|
|
28
|
+
flushedKeyframeKeys = /* @__PURE__ */ new Set();
|
|
29
|
+
globalStyles = /* @__PURE__ */ new Map();
|
|
30
|
+
flushedGlobalKeys = /* @__PURE__ */ new Set();
|
|
31
|
+
rawCSS = /* @__PURE__ */ new Map();
|
|
32
|
+
flushedRawKeys = /* @__PURE__ */ new Set();
|
|
33
|
+
keyframesCounter = 0;
|
|
34
|
+
internalsCollected = false;
|
|
35
|
+
/**
|
|
36
|
+
* Collect internal @property rules and :root token defaults.
|
|
37
|
+
* Mirrors markStylesGenerated() from the client-side injector.
|
|
38
|
+
* Called automatically on first chunk collection; idempotent.
|
|
39
|
+
*/
|
|
40
|
+
collectInternals() {
|
|
41
|
+
if (this.internalsCollected) return;
|
|
42
|
+
this.internalsCollected = true;
|
|
43
|
+
for (const [token, definition] of Object.entries(INTERNAL_PROPERTIES)) {
|
|
44
|
+
const css = formatPropertyCSS(token, definition);
|
|
45
|
+
if (css) this.collectProperty(`__internal:${token}`, css);
|
|
46
|
+
}
|
|
47
|
+
if (hasGlobalProperties()) {
|
|
48
|
+
const globalProps = getGlobalProperties();
|
|
49
|
+
if (globalProps) for (const [token, definition] of Object.entries(globalProps)) {
|
|
50
|
+
const css = formatPropertyCSS(token, definition);
|
|
51
|
+
if (css) this.collectProperty(`__global:${token}`, css);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const tokenEntries = Object.entries(INTERNAL_TOKENS);
|
|
55
|
+
if (tokenEntries.length > 0) {
|
|
56
|
+
const declarations = tokenEntries.map(([name, value]) => `${name}: ${value}`).join("; ");
|
|
57
|
+
this.collectProperty("__internal:root-tokens", `:root { ${declarations} }`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Allocate a className for a cache key, server-side.
|
|
62
|
+
* Mirrors StyleInjector.allocateClassName but without DOM access.
|
|
63
|
+
*/
|
|
64
|
+
allocateClassName(cacheKey) {
|
|
65
|
+
const existing = this.cacheKeyToClassName.get(cacheKey);
|
|
66
|
+
if (existing) return {
|
|
67
|
+
className: existing,
|
|
68
|
+
isNewAllocation: false
|
|
69
|
+
};
|
|
70
|
+
const className = generateClassName(this.classCounter++);
|
|
71
|
+
this.cacheKeyToClassName.set(cacheKey, className);
|
|
72
|
+
return {
|
|
73
|
+
className,
|
|
74
|
+
isNewAllocation: true
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Record CSS rules for a chunk.
|
|
79
|
+
* Called by useStyles during server render.
|
|
80
|
+
*/
|
|
81
|
+
collectChunk(cacheKey, className, rules) {
|
|
82
|
+
if (this.chunks.has(cacheKey)) return;
|
|
83
|
+
const css = formatRules(rules, className);
|
|
84
|
+
if (css) this.chunks.set(cacheKey, css);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Record a @property rule. Deduplicated by name.
|
|
88
|
+
*/
|
|
89
|
+
collectProperty(name, css) {
|
|
90
|
+
if (!this.propertyRules.has(name)) this.propertyRules.set(name, css);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Record a @keyframes rule. Deduplicated by name.
|
|
94
|
+
*/
|
|
95
|
+
collectKeyframes(name, css) {
|
|
96
|
+
if (!this.keyframeRules.has(name)) this.keyframeRules.set(name, css);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Allocate a keyframe name for SSR. Uses provided name or generates one.
|
|
100
|
+
*/
|
|
101
|
+
allocateKeyframeName(providedName) {
|
|
102
|
+
return providedName ?? `k${this.keyframesCounter++}`;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Record global styles (from useGlobalStyles). Deduplicated by key.
|
|
106
|
+
*/
|
|
107
|
+
collectGlobalStyles(key, css) {
|
|
108
|
+
if (!this.globalStyles.has(key)) this.globalStyles.set(key, css);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Record raw CSS text (from useRawCSS). Deduplicated by key.
|
|
112
|
+
*/
|
|
113
|
+
collectRawCSS(key, css) {
|
|
114
|
+
if (!this.rawCSS.has(key)) this.rawCSS.set(key, css);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Extract all CSS collected so far as a single string.
|
|
118
|
+
* Includes @property and @keyframes rules.
|
|
119
|
+
* Used for non-streaming SSR (renderToString).
|
|
120
|
+
*/
|
|
121
|
+
getCSS() {
|
|
122
|
+
const parts = [];
|
|
123
|
+
for (const css of this.propertyRules.values()) parts.push(css);
|
|
124
|
+
for (const css of this.rawCSS.values()) parts.push(css);
|
|
125
|
+
for (const css of this.globalStyles.values()) parts.push(css);
|
|
126
|
+
for (const css of this.chunks.values()) parts.push(css);
|
|
127
|
+
for (const css of this.keyframeRules.values()) parts.push(css);
|
|
128
|
+
return parts.join("\n");
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Flush only newly collected CSS since the last flush.
|
|
132
|
+
* Used for streaming SSR (renderToPipeableStream + useServerInsertedHTML).
|
|
133
|
+
*/
|
|
134
|
+
flushCSS() {
|
|
135
|
+
const parts = [];
|
|
136
|
+
for (const [name, css] of this.propertyRules) if (!this.flushedPropertyKeys.has(name)) {
|
|
137
|
+
parts.push(css);
|
|
138
|
+
this.flushedPropertyKeys.add(name);
|
|
139
|
+
}
|
|
140
|
+
for (const [key, css] of this.rawCSS) if (!this.flushedRawKeys.has(key)) {
|
|
141
|
+
parts.push(css);
|
|
142
|
+
this.flushedRawKeys.add(key);
|
|
143
|
+
}
|
|
144
|
+
for (const [key, css] of this.globalStyles) if (!this.flushedGlobalKeys.has(key)) {
|
|
145
|
+
parts.push(css);
|
|
146
|
+
this.flushedGlobalKeys.add(key);
|
|
147
|
+
}
|
|
148
|
+
for (const [key, css] of this.chunks) if (!this.flushedKeys.has(key)) {
|
|
149
|
+
parts.push(css);
|
|
150
|
+
this.flushedKeys.add(key);
|
|
151
|
+
}
|
|
152
|
+
for (const [name, css] of this.keyframeRules) if (!this.flushedKeyframeKeys.has(name)) {
|
|
153
|
+
parts.push(css);
|
|
154
|
+
this.flushedKeyframeKeys.add(name);
|
|
155
|
+
}
|
|
156
|
+
return parts.join("\n");
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Serialize the cache state for client hydration.
|
|
160
|
+
*/
|
|
161
|
+
getCacheState() {
|
|
162
|
+
const entries = {};
|
|
163
|
+
for (const [cacheKey, className] of this.cacheKeyToClassName) entries[cacheKey] = className;
|
|
164
|
+
return {
|
|
165
|
+
entries,
|
|
166
|
+
classCounter: this.classCounter
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
//#endregion
|
|
172
|
+
export { ServerStyleCollector };
|
|
173
|
+
//# sourceMappingURL=collector.js.map
|