@tenphi/tasty 0.0.0-snapshot.fafcbfe → 0.0.0-snapshot.fde7ec8
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 +322 -160
- package/dist/_virtual/_rolldown/runtime.js +1 -2
- package/dist/chunks/cacheKey.d.ts +1 -0
- package/dist/chunks/cacheKey.js +17 -10
- package/dist/chunks/cacheKey.js.map +1 -1
- package/dist/chunks/definitions.d.ts +1 -1
- package/dist/chunks/definitions.js +3 -5
- package/dist/chunks/definitions.js.map +1 -1
- package/dist/chunks/index.d.ts +1 -0
- package/dist/chunks/renderChunk.d.ts +1 -0
- package/dist/chunks/renderChunk.js +32 -34
- package/dist/chunks/renderChunk.js.map +1 -1
- package/dist/compute-styles.d.ts +31 -0
- package/dist/compute-styles.js +322 -0
- package/dist/compute-styles.js.map +1 -0
- package/dist/config.d.ts +157 -30
- package/dist/config.js +233 -45
- package/dist/config.js.map +1 -1
- package/dist/core/index.d.ts +12 -11
- package/dist/core/index.js +12 -11
- package/dist/counter-style/index.js +51 -0
- package/dist/counter-style/index.js.map +1 -0
- package/dist/debug.d.ts +26 -141
- package/dist/debug.js +359 -639
- package/dist/debug.js.map +1 -1
- package/dist/font-face/index.js +63 -0
- package/dist/font-face/index.js.map +1 -0
- package/dist/hooks/index.d.ts +7 -0
- package/dist/hooks/useCounterStyle.d.ts +36 -0
- package/dist/hooks/useCounterStyle.js +65 -0
- package/dist/hooks/useCounterStyle.js.map +1 -0
- package/dist/hooks/useFontFace.d.ts +45 -0
- package/dist/hooks/useFontFace.js +66 -0
- package/dist/hooks/useFontFace.js.map +1 -0
- package/dist/hooks/useGlobalStyles.d.ts +21 -2
- package/dist/hooks/useGlobalStyles.js +60 -28
- package/dist/hooks/useGlobalStyles.js.map +1 -1
- package/dist/hooks/useKeyframes.d.ts +4 -2
- package/dist/hooks/useKeyframes.js +44 -43
- package/dist/hooks/useKeyframes.js.map +1 -1
- package/dist/hooks/useProperty.d.ts +4 -2
- package/dist/hooks/useProperty.js +36 -31
- package/dist/hooks/useProperty.js.map +1 -1
- package/dist/hooks/useRawCSS.d.ts +13 -44
- package/dist/hooks/useRawCSS.js +91 -16
- package/dist/hooks/useRawCSS.js.map +1 -1
- package/dist/hooks/useStyles.d.ts +3 -3
- package/dist/hooks/useStyles.js +8 -146
- package/dist/hooks/useStyles.js.map +1 -1
- package/dist/index.d.ts +23 -12
- package/dist/index.js +15 -12
- package/dist/injector/index.d.ts +36 -11
- package/dist/injector/index.js +43 -12
- package/dist/injector/index.js.map +1 -1
- package/dist/injector/injector.d.ts +62 -3
- package/dist/injector/injector.js +293 -46
- package/dist/injector/injector.js.map +1 -1
- package/dist/injector/sheet-manager.d.ts +22 -17
- package/dist/injector/sheet-manager.js +64 -79
- package/dist/injector/sheet-manager.js.map +1 -1
- package/dist/injector/types.d.ts +121 -21
- package/dist/keyframes/index.js +1 -1
- package/dist/parser/classify.js +6 -6
- package/dist/parser/classify.js.map +1 -1
- package/dist/parser/const.js +30 -3
- package/dist/parser/const.js.map +1 -1
- package/dist/parser/lru.js +1 -1
- package/dist/parser/lru.js.map +1 -1
- package/dist/parser/parser.js +2 -3
- package/dist/parser/parser.js.map +1 -1
- package/dist/parser/tokenizer.js +1 -1
- package/dist/parser/tokenizer.js.map +1 -1
- package/dist/parser/types.js +1 -1
- package/dist/parser/types.js.map +1 -1
- package/dist/pipeline/conditions.js +15 -9
- package/dist/pipeline/conditions.js.map +1 -1
- package/dist/pipeline/exclusive.js +58 -4
- package/dist/pipeline/exclusive.js.map +1 -1
- package/dist/pipeline/index.d.ts +3 -1
- package/dist/pipeline/index.js +141 -74
- package/dist/pipeline/index.js.map +1 -1
- package/dist/pipeline/materialize.js +460 -211
- package/dist/pipeline/materialize.js.map +1 -1
- package/dist/pipeline/parseStateKey.d.ts +1 -1
- package/dist/pipeline/parseStateKey.js +38 -22
- package/dist/pipeline/parseStateKey.js.map +1 -1
- package/dist/pipeline/simplify.js +289 -156
- package/dist/pipeline/simplify.js.map +1 -1
- package/dist/pipeline/warnings.js +18 -0
- package/dist/pipeline/warnings.js.map +1 -0
- package/dist/plugins/index.d.ts +2 -0
- package/dist/plugins/okhsl-plugin.js +3 -277
- package/dist/plugins/okhsl-plugin.js.map +1 -1
- package/dist/plugins/types.d.ts +20 -2
- package/dist/properties/index.js +83 -19
- package/dist/properties/index.js.map +1 -1
- package/dist/properties/property-type-resolver.d.ts +24 -0
- package/dist/properties/property-type-resolver.js +90 -0
- package/dist/properties/property-type-resolver.js.map +1 -0
- package/dist/rsc-cache.js +79 -0
- package/dist/rsc-cache.js.map +1 -0
- package/dist/ssr/astro-client.d.ts +1 -0
- package/dist/ssr/astro-client.js +19 -0
- package/dist/ssr/astro-client.js.map +1 -0
- package/dist/ssr/astro-middleware.d.ts +15 -0
- package/dist/ssr/astro-middleware.js +19 -0
- package/dist/ssr/astro-middleware.js.map +1 -0
- package/dist/ssr/astro.d.ts +108 -0
- package/dist/ssr/astro.js +149 -0
- package/dist/ssr/astro.js.map +1 -0
- package/dist/ssr/async-storage.d.ts +17 -0
- package/dist/ssr/async-storage.js +44 -0
- package/dist/ssr/async-storage.js.map +1 -0
- package/dist/ssr/collect-auto-properties.js +58 -0
- package/dist/ssr/collect-auto-properties.js.map +1 -0
- package/dist/ssr/collector.d.ts +98 -0
- package/dist/ssr/collector.js +235 -0
- package/dist/ssr/collector.js.map +1 -0
- package/dist/ssr/context.js +16 -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 +69 -0
- package/dist/ssr/format-keyframes.js.map +1 -0
- package/dist/ssr/format-property.js +49 -0
- package/dist/ssr/format-property.js.map +1 -0
- package/dist/ssr/format-rules.js +73 -0
- package/dist/ssr/format-rules.js.map +1 -0
- package/dist/ssr/hydrate.d.ts +29 -0
- package/dist/ssr/hydrate.js +45 -0
- package/dist/ssr/hydrate.js.map +1 -0
- package/dist/ssr/index.d.ts +4 -0
- package/dist/ssr/index.js +10 -0
- package/dist/ssr/index.js.map +1 -0
- package/dist/ssr/next.d.ts +46 -0
- package/dist/ssr/next.js +75 -0
- package/dist/ssr/next.js.map +1 -0
- package/dist/ssr/ssr-collector-ref.js +29 -0
- package/dist/ssr/ssr-collector-ref.js.map +1 -0
- package/dist/states/index.js +13 -259
- package/dist/states/index.js.map +1 -1
- package/dist/static/index.js +1 -2
- package/dist/static/inject.d.ts +5 -0
- package/dist/static/inject.js +17 -0
- package/dist/static/inject.js.map +1 -0
- package/dist/static/tastyStatic.js +1 -2
- package/dist/static/tastyStatic.js.map +1 -1
- package/dist/static/types.js +1 -1
- package/dist/styles/border.d.ts +2 -2
- package/dist/styles/border.js +30 -24
- package/dist/styles/border.js.map +1 -1
- package/dist/styles/color.d.ts +3 -3
- package/dist/styles/color.js +11 -8
- package/dist/styles/color.js.map +1 -1
- package/dist/styles/const.js +17 -0
- package/dist/styles/const.js.map +1 -0
- package/dist/styles/createStyle.js +29 -27
- package/dist/styles/createStyle.js.map +1 -1
- package/dist/styles/dimension.js +15 -3
- package/dist/styles/dimension.js.map +1 -1
- package/dist/styles/directional.js +133 -0
- package/dist/styles/directional.js.map +1 -0
- package/dist/styles/display.d.ts +3 -10
- package/dist/styles/display.js +45 -39
- package/dist/styles/display.js.map +1 -1
- package/dist/styles/fade.d.ts +2 -2
- package/dist/styles/fade.js +9 -5
- package/dist/styles/fade.js.map +1 -1
- package/dist/styles/fill.d.ts +15 -17
- package/dist/styles/fill.js +9 -9
- package/dist/styles/fill.js.map +1 -1
- package/dist/styles/flow.d.ts +3 -3
- package/dist/styles/flow.js +1 -1
- package/dist/styles/flow.js.map +1 -1
- package/dist/styles/gap.d.ts +1 -1
- package/dist/styles/gap.js +4 -3
- package/dist/styles/gap.js.map +1 -1
- package/dist/styles/height.d.ts +1 -1
- package/dist/styles/height.js +1 -2
- package/dist/styles/height.js.map +1 -1
- package/dist/styles/index.d.ts +0 -1
- package/dist/styles/index.js +3 -4
- package/dist/styles/index.js.map +1 -1
- package/dist/styles/inset.d.ts +1 -29
- package/dist/styles/inset.js +19 -135
- package/dist/styles/inset.js.map +1 -1
- package/dist/styles/list.d.ts +5 -5
- package/dist/styles/list.js +4 -2
- package/dist/styles/list.js.map +1 -1
- package/dist/styles/margin.d.ts +1 -1
- package/dist/styles/margin.js +17 -89
- package/dist/styles/margin.js.map +1 -1
- package/dist/styles/outline.d.ts +1 -1
- package/dist/styles/outline.js +6 -16
- package/dist/styles/outline.js.map +1 -1
- package/dist/styles/padding.d.ts +1 -1
- package/dist/styles/padding.js +17 -89
- package/dist/styles/padding.js.map +1 -1
- package/dist/styles/placement.d.ts +37 -0
- package/dist/styles/placement.js +74 -0
- package/dist/styles/placement.js.map +1 -0
- package/dist/styles/predefined.d.ts +4 -6
- package/dist/styles/predefined.js +9 -13
- package/dist/styles/predefined.js.map +1 -1
- package/dist/styles/preset.d.ts +6 -1
- package/dist/styles/preset.js +54 -53
- package/dist/styles/preset.js.map +1 -1
- package/dist/styles/radius.d.ts +2 -4
- package/dist/styles/radius.js +38 -6
- package/dist/styles/radius.js.map +1 -1
- package/dist/styles/scrollMargin.d.ts +24 -0
- package/dist/styles/scrollMargin.js +32 -0
- package/dist/styles/scrollMargin.js.map +1 -0
- package/dist/styles/scrollbar.d.ts +9 -5
- package/dist/styles/scrollbar.js +31 -85
- package/dist/styles/scrollbar.js.map +1 -1
- package/dist/styles/shadow.d.ts +3 -3
- package/dist/styles/shadow.js +4 -3
- package/dist/styles/shadow.js.map +1 -1
- package/dist/styles/shared.js +17 -0
- package/dist/styles/shared.js.map +1 -0
- package/dist/styles/transition.d.ts +2 -2
- package/dist/styles/transition.js +6 -5
- package/dist/styles/transition.js.map +1 -1
- package/dist/styles/types.d.ts +94 -28
- package/dist/styles/width.d.ts +1 -1
- package/dist/styles/width.js +1 -2
- package/dist/styles/width.js.map +1 -1
- package/dist/tasty.d.ts +46 -894
- package/dist/tasty.js +117 -60
- package/dist/tasty.js.map +1 -1
- package/dist/types.d.ts +3 -3
- package/dist/utils/cache-wrapper.js +4 -9
- package/dist/utils/cache-wrapper.js.map +1 -1
- package/dist/utils/case-converter.js +1 -1
- package/dist/utils/color-math.d.ts +46 -0
- package/dist/utils/color-math.js +749 -0
- package/dist/utils/color-math.js.map +1 -0
- package/dist/utils/color-space.d.ts +5 -0
- package/dist/utils/color-space.js +228 -0
- package/dist/utils/color-space.js.map +1 -0
- package/dist/utils/colors.d.ts +1 -1
- package/dist/utils/colors.js +3 -2
- package/dist/utils/colors.js.map +1 -1
- package/dist/utils/css-types.d.ts +1 -1
- package/dist/utils/deps-equal.js +15 -0
- package/dist/utils/deps-equal.js.map +1 -0
- package/dist/utils/dotize.d.ts +1 -1
- package/dist/utils/dotize.js +1 -1
- package/dist/utils/dotize.js.map +1 -1
- package/dist/utils/filter-base-props.d.ts +1 -1
- package/dist/utils/filter-base-props.js +1 -1
- package/dist/utils/filter-base-props.js.map +1 -1
- package/dist/utils/get-display-name.js +1 -1
- package/dist/utils/has-keys.js +13 -0
- package/dist/utils/has-keys.js.map +1 -0
- package/dist/utils/hash.js +14 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/is-dev-env.js +1 -1
- package/dist/utils/is-dev-env.js.map +1 -1
- package/dist/utils/is-valid-element-type.js +1 -1
- package/dist/utils/merge-styles.js +1 -2
- package/dist/utils/merge-styles.js.map +1 -1
- package/dist/utils/mod-attrs.d.ts +0 -2
- package/dist/utils/mod-attrs.js +3 -4
- package/dist/utils/mod-attrs.js.map +1 -1
- package/dist/utils/process-tokens.d.ts +3 -17
- package/dist/utils/process-tokens.js +19 -107
- package/dist/utils/process-tokens.js.map +1 -1
- package/dist/utils/resolve-recipes.js +1 -2
- package/dist/utils/resolve-recipes.js.map +1 -1
- package/dist/utils/selector-transform.js +32 -0
- package/dist/utils/selector-transform.js.map +1 -0
- package/dist/utils/string.js +1 -1
- package/dist/utils/string.js.map +1 -1
- package/dist/utils/styles.d.ts +5 -89
- package/dist/utils/styles.js +35 -400
- package/dist/utils/styles.js.map +1 -1
- package/dist/utils/typography.d.ts +35 -13
- package/dist/utils/typography.js +16 -18
- package/dist/utils/typography.js.map +1 -1
- package/dist/utils/warnings.js +1 -1
- package/dist/zero/babel.d.ts +57 -84
- package/dist/zero/babel.js +217 -37
- package/dist/zero/babel.js.map +1 -1
- package/dist/zero/css-writer.js +1 -2
- package/dist/zero/css-writer.js.map +1 -1
- package/dist/zero/extractor.js +119 -3
- package/dist/zero/extractor.js.map +1 -1
- package/dist/zero/index.js +1 -2
- package/dist/zero/next.d.ts +56 -30
- package/dist/zero/next.js +105 -40
- package/dist/zero/next.js.map +1 -1
- package/docs/PIPELINE.md +519 -0
- package/docs/README.md +31 -0
- package/docs/adoption.md +298 -0
- package/docs/comparison.md +419 -0
- package/docs/configuration.md +394 -0
- package/docs/debug.md +318 -0
- package/docs/design-system.md +436 -0
- package/docs/dsl.md +688 -0
- package/docs/getting-started.md +217 -0
- package/docs/injector.md +544 -0
- package/docs/methodology.md +616 -0
- package/docs/react-api.md +557 -0
- package/docs/ssr.md +442 -0
- package/docs/styles.md +596 -0
- package/docs/tasty-static.md +532 -0
- package/package.json +112 -31
- package/dist/styles/align.d.ts +0 -15
- package/dist/styles/align.js +0 -14
- package/dist/styles/align.js.map +0 -1
- package/dist/styles/justify.d.ts +0 -15
- package/dist/styles/justify.js +0 -14
- package/dist/styles/justify.js.map +0 -1
- 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/tokens/typography.d.ts +0 -19
- package/dist/tokens/typography.js +0 -237
- package/dist/tokens/typography.js.map +0 -1
- package/dist/utils/hsl-to-rgb.js +0 -38
- package/dist/utils/hsl-to-rgb.js.map +0 -1
- package/dist/utils/okhsl-to-rgb.js +0 -296
- package/dist/utils/okhsl-to-rgb.js.map +0 -1
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { getConfig } from "../config.js";
|
|
2
|
+
import { registerSSRCollectorGetterGlobal } from "./ssr-collector-ref.js";
|
|
3
|
+
import { ServerStyleCollector } from "./collector.js";
|
|
4
|
+
import { getSSRCollector, runWithCollector } from "./async-storage.js";
|
|
5
|
+
//#region src/ssr/astro.ts
|
|
6
|
+
/**
|
|
7
|
+
* Astro integration for Tasty SSR.
|
|
8
|
+
*
|
|
9
|
+
* Provides:
|
|
10
|
+
* - tastyIntegration() — Astro Integration API (recommended)
|
|
11
|
+
* - tastyMiddleware() — manual middleware for advanced composition
|
|
12
|
+
*
|
|
13
|
+
* Import from '@tenphi/tasty/ssr/astro'.
|
|
14
|
+
*/
|
|
15
|
+
registerSSRCollectorGetterGlobal(getSSRCollector);
|
|
16
|
+
/**
|
|
17
|
+
* Create an Astro middleware that collects Tasty styles during SSR.
|
|
18
|
+
*
|
|
19
|
+
* All React components rendered during the request will have their
|
|
20
|
+
* computeStyles() calls captured by the collector via AsyncLocalStorage.
|
|
21
|
+
* After rendering, the middleware injects the collected CSS into </head>.
|
|
22
|
+
*
|
|
23
|
+
* @example Manual middleware setup
|
|
24
|
+
* ```ts
|
|
25
|
+
* // src/middleware.ts
|
|
26
|
+
* import { tastyMiddleware } from '@tenphi/tasty/ssr/astro';
|
|
27
|
+
* export const onRequest = tastyMiddleware();
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @example Composing with other middleware
|
|
31
|
+
* ```ts
|
|
32
|
+
* // src/middleware.ts
|
|
33
|
+
* import { sequence } from 'astro:middleware';
|
|
34
|
+
* import { tastyMiddleware } from '@tenphi/tasty/ssr/astro';
|
|
35
|
+
*
|
|
36
|
+
* export const onRequest = sequence(
|
|
37
|
+
* tastyMiddleware(),
|
|
38
|
+
* myOtherMiddleware,
|
|
39
|
+
* );
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
function tastyMiddleware(options) {
|
|
43
|
+
return async (_context, next) => {
|
|
44
|
+
const transferCache = options?.transferCache ?? true;
|
|
45
|
+
const collector = new ServerStyleCollector();
|
|
46
|
+
const rendered = await runWithCollector(collector, async () => {
|
|
47
|
+
const response = await next();
|
|
48
|
+
const body = response.body;
|
|
49
|
+
if (!body) return {
|
|
50
|
+
html: null,
|
|
51
|
+
status: response.status,
|
|
52
|
+
headers: response.headers
|
|
53
|
+
};
|
|
54
|
+
const reader = body.pipeThrough(new TextDecoderStream()).getReader();
|
|
55
|
+
const parts = [];
|
|
56
|
+
for (;;) {
|
|
57
|
+
const { done, value } = await reader.read();
|
|
58
|
+
if (done) break;
|
|
59
|
+
parts.push(value);
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
html: parts.join(""),
|
|
63
|
+
status: response.status,
|
|
64
|
+
headers: response.headers
|
|
65
|
+
};
|
|
66
|
+
});
|
|
67
|
+
if (!rendered.html) return new Response(null, {
|
|
68
|
+
status: rendered.status,
|
|
69
|
+
headers: rendered.headers
|
|
70
|
+
});
|
|
71
|
+
let { html } = rendered;
|
|
72
|
+
const css = collector.getCSS();
|
|
73
|
+
if (!css) return new Response(html, {
|
|
74
|
+
status: rendered.status,
|
|
75
|
+
headers: rendered.headers
|
|
76
|
+
});
|
|
77
|
+
const nonce = getConfig().nonce;
|
|
78
|
+
const nonceAttr = nonce ? ` nonce="${nonce}"` : "";
|
|
79
|
+
const styleTag = `<style data-tasty-ssr${nonceAttr}>${css}</style>`;
|
|
80
|
+
let cacheTag = "";
|
|
81
|
+
if (transferCache) {
|
|
82
|
+
const classNames = collector.getRenderedClassNames();
|
|
83
|
+
if (classNames.length > 0) cacheTag = `<script${nonceAttr}>(window.__TASTY__=window.__TASTY__||[]).push(${classNames.map((n) => `"${n}"`).join(",")})<\/script>`;
|
|
84
|
+
}
|
|
85
|
+
const injection = styleTag + cacheTag;
|
|
86
|
+
const idx = html.indexOf("</head>");
|
|
87
|
+
if (idx !== -1) html = html.slice(0, idx) + injection + html.slice(idx);
|
|
88
|
+
else html = injection + html;
|
|
89
|
+
const headers = new Headers(rendered.headers);
|
|
90
|
+
headers.delete("content-length");
|
|
91
|
+
return new Response(html, {
|
|
92
|
+
status: rendered.status,
|
|
93
|
+
headers
|
|
94
|
+
});
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
let _middlewareTransferCache = true;
|
|
98
|
+
/** @internal */
|
|
99
|
+
function setMiddlewareTransferCache(value) {
|
|
100
|
+
_middlewareTransferCache = value;
|
|
101
|
+
}
|
|
102
|
+
/** @internal */
|
|
103
|
+
function getMiddlewareTransferCache() {
|
|
104
|
+
return _middlewareTransferCache;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Astro integration that automatically sets up Tasty SSR.
|
|
108
|
+
*
|
|
109
|
+
* Registers middleware for cross-component CSS deduplication and
|
|
110
|
+
* optionally injects a client hydration script for island support.
|
|
111
|
+
*
|
|
112
|
+
* @example Basic setup (with islands)
|
|
113
|
+
* ```ts
|
|
114
|
+
* // astro.config.mjs
|
|
115
|
+
* import { tastyIntegration } from '@tenphi/tasty/ssr/astro';
|
|
116
|
+
*
|
|
117
|
+
* export default defineConfig({
|
|
118
|
+
* integrations: [tastyIntegration()],
|
|
119
|
+
* });
|
|
120
|
+
* ```
|
|
121
|
+
*
|
|
122
|
+
* @example Static-only (no client JS)
|
|
123
|
+
* ```ts
|
|
124
|
+
* // astro.config.mjs
|
|
125
|
+
* import { tastyIntegration } from '@tenphi/tasty/ssr/astro';
|
|
126
|
+
*
|
|
127
|
+
* export default defineConfig({
|
|
128
|
+
* integrations: [tastyIntegration({ islands: false })],
|
|
129
|
+
* });
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
function tastyIntegration(options) {
|
|
133
|
+
const { islands = true } = options ?? {};
|
|
134
|
+
setMiddlewareTransferCache(islands);
|
|
135
|
+
return {
|
|
136
|
+
name: "@tenphi/tasty",
|
|
137
|
+
hooks: { "astro:config:setup": ({ addMiddleware, injectScript }) => {
|
|
138
|
+
addMiddleware({
|
|
139
|
+
entrypoint: new URL("./astro-middleware.js", import.meta.url),
|
|
140
|
+
order: "pre"
|
|
141
|
+
});
|
|
142
|
+
if (islands) injectScript("before-hydration", `import "@tenphi/tasty/ssr/astro-client";`);
|
|
143
|
+
} }
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
//#endregion
|
|
147
|
+
export { getMiddlewareTransferCache, setMiddlewareTransferCache, tastyIntegration, tastyMiddleware };
|
|
148
|
+
|
|
149
|
+
//# 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:\n * - tastyIntegration() — Astro Integration API (recommended)\n * - tastyMiddleware() — manual middleware for advanced composition\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 { registerSSRCollectorGetterGlobal } from './ssr-collector-ref';\n\n// Wire up ALS-based collector discovery so computeStyles() can find\n// the collector set by tastyMiddleware's runWithCollector().\n// Uses globalThis so the getter is visible across Astro's separate\n// module graphs (middleware vs page components).\nregisterSSRCollectorGetterGlobal(getSSRCollector);\n\nexport interface TastyMiddlewareOptions {\n /**\n * Whether to embed the class-list script for client hydration.\n * Set to false to skip class transfer (e.g. for CSP restrictions).\n * Without it, client components may re-inject CSS that already exists\n * in server-rendered `<style>` tags. 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 will have their\n * computeStyles() calls captured by the collector via AsyncLocalStorage.\n * After rendering, the middleware injects the collected CSS into </head>.\n *\n * @example Manual middleware setup\n * ```ts\n * // src/middleware.ts\n * import { tastyMiddleware } from '@tenphi/tasty/ssr/astro';\n * export const onRequest = tastyMiddleware();\n * ```\n *\n * @example Composing with other middleware\n * ```ts\n * // src/middleware.ts\n * import { sequence } from 'astro:middleware';\n * import { tastyMiddleware } from '@tenphi/tasty/ssr/astro';\n *\n * export const onRequest = sequence(\n * tastyMiddleware(),\n * myOtherMiddleware,\n * );\n * ```\n */\nexport function tastyMiddleware(options?: TastyMiddlewareOptions) {\n return async (\n _context: unknown,\n next: () => Promise<Response>,\n ): Promise<Response> => {\n const transferCache = options?.transferCache ?? true;\n const collector = new ServerStyleCollector();\n\n // Run the entire request — including body stream consumption — inside\n // the ALS context so that components rendering lazily during stream\n // reads can still find the collector via getSSRCollector().\n const rendered = await runWithCollector(collector, async () => {\n const response = await next();\n const body = response.body;\n if (!body) {\n return {\n html: null as string | null,\n status: response.status,\n headers: response.headers,\n };\n }\n\n const reader = body.pipeThrough(new TextDecoderStream()).getReader();\n const parts: string[] = [];\n for (;;) {\n const { done, value } = await reader.read();\n if (done) break;\n parts.push(value);\n }\n return {\n html: parts.join(''),\n status: response.status,\n headers: response.headers,\n };\n });\n\n if (!rendered.html) {\n return new Response(null, {\n status: rendered.status,\n headers: rendered.headers,\n });\n }\n\n let { html } = rendered;\n\n const css = collector.getCSS();\n if (!css) {\n return new Response(html, {\n status: rendered.status,\n headers: rendered.headers,\n });\n }\n\n const nonce = getConfig().nonce;\n const nonceAttr = nonce ? ` nonce=\"${nonce}\"` : '';\n const styleTag = `<style data-tasty-ssr${nonceAttr}>${css}</style>`;\n\n let cacheTag = '';\n if (transferCache) {\n const classNames = collector.getRenderedClassNames();\n if (classNames.length > 0) {\n const classListJSON = classNames.map((n) => `\"${n}\"`).join(',');\n cacheTag = `<script${nonceAttr}>(window.__TASTY__=window.__TASTY__||[]).push(${classListJSON})</script>`;\n }\n }\n\n const injection = styleTag + cacheTag;\n const idx = html.indexOf('</head>');\n if (idx !== -1) {\n html = html.slice(0, idx) + injection + html.slice(idx);\n } else {\n html = injection + html;\n }\n\n const headers = new Headers(rendered.headers);\n headers.delete('content-length');\n\n return new Response(html, {\n status: rendered.status,\n headers,\n });\n };\n}\n\n// ============================================================================\n// Module-level middleware config (set by tastyIntegration, read by\n// astro-middleware.ts via getter property)\n// ============================================================================\n\nlet _middlewareTransferCache = true;\n\n/** @internal */\nexport function setMiddlewareTransferCache(value: boolean): void {\n _middlewareTransferCache = value;\n}\n\n/** @internal */\nexport function getMiddlewareTransferCache(): boolean {\n return _middlewareTransferCache;\n}\n\n// ============================================================================\n// Astro Integration API\n// ============================================================================\n\nexport interface TastyIntegrationOptions {\n /**\n * Enable island hydration support.\n *\n * When `true` (default): injects a client hydration script via\n * `injectScript('before-hydration')` and sets `transferCache: true`\n * on the middleware. Islands skip the style pipeline during hydration.\n *\n * When `false`: no client JS is shipped and `transferCache` is set\n * to `false`. Use this for fully static sites without `client:*`\n * directives.\n */\n islands?: boolean;\n}\n\n/**\n * Astro integration that automatically sets up Tasty SSR.\n *\n * Registers middleware for cross-component CSS deduplication and\n * optionally injects a client hydration script for island support.\n *\n * @example Basic setup (with islands)\n * ```ts\n * // astro.config.mjs\n * import { tastyIntegration } from '@tenphi/tasty/ssr/astro';\n *\n * export default defineConfig({\n * integrations: [tastyIntegration()],\n * });\n * ```\n *\n * @example Static-only (no client JS)\n * ```ts\n * // astro.config.mjs\n * import { tastyIntegration } from '@tenphi/tasty/ssr/astro';\n *\n * export default defineConfig({\n * integrations: [tastyIntegration({ islands: false })],\n * });\n * ```\n */\nexport function tastyIntegration(options?: TastyIntegrationOptions) {\n const { islands = true } = options ?? {};\n\n setMiddlewareTransferCache(islands);\n\n return {\n name: '@tenphi/tasty',\n hooks: {\n 'astro:config:setup': ({\n addMiddleware,\n injectScript,\n }: {\n addMiddleware: (middleware: {\n entrypoint: string | URL;\n order: 'pre' | 'post';\n }) => void;\n injectScript: (stage: string, content: string) => void;\n }) => {\n addMiddleware({\n entrypoint: new URL('./astro-middleware.js', import.meta.url),\n order: 'pre',\n });\n\n if (islands) {\n injectScript(\n 'before-hydration',\n `import \"@tenphi/tasty/ssr/astro-client\";`,\n );\n }\n },\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;AAmBA,iCAAiC,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCjD,SAAgB,gBAAgB,SAAkC;AAChE,QAAO,OACL,UACA,SACsB;EACtB,MAAM,gBAAgB,SAAS,iBAAiB;EAChD,MAAM,YAAY,IAAI,sBAAsB;EAK5C,MAAM,WAAW,MAAM,iBAAiB,WAAW,YAAY;GAC7D,MAAM,WAAW,MAAM,MAAM;GAC7B,MAAM,OAAO,SAAS;AACtB,OAAI,CAAC,KACH,QAAO;IACL,MAAM;IACN,QAAQ,SAAS;IACjB,SAAS,SAAS;IACnB;GAGH,MAAM,SAAS,KAAK,YAAY,IAAI,mBAAmB,CAAC,CAAC,WAAW;GACpE,MAAM,QAAkB,EAAE;AAC1B,YAAS;IACP,MAAM,EAAE,MAAM,UAAU,MAAM,OAAO,MAAM;AAC3C,QAAI,KAAM;AACV,UAAM,KAAK,MAAM;;AAEnB,UAAO;IACL,MAAM,MAAM,KAAK,GAAG;IACpB,QAAQ,SAAS;IACjB,SAAS,SAAS;IACnB;IACD;AAEF,MAAI,CAAC,SAAS,KACZ,QAAO,IAAI,SAAS,MAAM;GACxB,QAAQ,SAAS;GACjB,SAAS,SAAS;GACnB,CAAC;EAGJ,IAAI,EAAE,SAAS;EAEf,MAAM,MAAM,UAAU,QAAQ;AAC9B,MAAI,CAAC,IACH,QAAO,IAAI,SAAS,MAAM;GACxB,QAAQ,SAAS;GACjB,SAAS,SAAS;GACnB,CAAC;EAGJ,MAAM,QAAQ,WAAW,CAAC;EAC1B,MAAM,YAAY,QAAQ,WAAW,MAAM,KAAK;EAChD,MAAM,WAAW,wBAAwB,UAAU,GAAG,IAAI;EAE1D,IAAI,WAAW;AACf,MAAI,eAAe;GACjB,MAAM,aAAa,UAAU,uBAAuB;AACpD,OAAI,WAAW,SAAS,EAEtB,YAAW,UAAU,UAAU,gDADT,WAAW,KAAK,MAAM,IAAI,EAAE,GAAG,CAAC,KAAK,IAAI,CAC8B;;EAIjG,MAAM,YAAY,WAAW;EAC7B,MAAM,MAAM,KAAK,QAAQ,UAAU;AACnC,MAAI,QAAQ,GACV,QAAO,KAAK,MAAM,GAAG,IAAI,GAAG,YAAY,KAAK,MAAM,IAAI;MAEvD,QAAO,YAAY;EAGrB,MAAM,UAAU,IAAI,QAAQ,SAAS,QAAQ;AAC7C,UAAQ,OAAO,iBAAiB;AAEhC,SAAO,IAAI,SAAS,MAAM;GACxB,QAAQ,SAAS;GACjB;GACD,CAAC;;;AASN,IAAI,2BAA2B;;AAG/B,SAAgB,2BAA2B,OAAsB;AAC/D,4BAA2B;;;AAI7B,SAAgB,6BAAsC;AACpD,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDT,SAAgB,iBAAiB,SAAmC;CAClE,MAAM,EAAE,UAAU,SAAS,WAAW,EAAE;AAExC,4BAA2B,QAAQ;AAEnC,QAAO;EACL,MAAM;EACN,OAAO,EACL,uBAAuB,EACrB,eACA,mBAOI;AACJ,iBAAc;IACZ,YAAY,IAAI,IAAI,yBAAyB,OAAO,KAAK,IAAI;IAC7D,OAAO;IACR,CAAC;AAEF,OAAI,QACF,cACE,oBACA,2CACD;KAGN;EACF"}
|
|
@@ -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,44 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
//#region src/ssr/async-storage.ts
|
|
3
|
+
/**
|
|
4
|
+
* AsyncLocalStorage integration for SSR collector discovery.
|
|
5
|
+
*
|
|
6
|
+
* Used by Astro middleware and generic framework integrations where
|
|
7
|
+
* the library cannot wrap the React tree with a context provider.
|
|
8
|
+
* The middleware calls runWithCollector() around the render, and
|
|
9
|
+
* useStyles() calls getSSRCollector() to find it.
|
|
10
|
+
*
|
|
11
|
+
* Uses globalThis to ensure the AsyncLocalStorage instance is shared
|
|
12
|
+
* across module instances — frameworks like Astro may load middleware
|
|
13
|
+
* and page components from separate module graphs.
|
|
14
|
+
*
|
|
15
|
+
* This module imports from 'node:async_hooks' — it must be excluded
|
|
16
|
+
* from client bundles via the build configuration.
|
|
17
|
+
*/
|
|
18
|
+
const ALS_KEY = "__tasty_ssr_als__";
|
|
19
|
+
function getSharedStorage() {
|
|
20
|
+
const g = globalThis;
|
|
21
|
+
if (!g[ALS_KEY]) g[ALS_KEY] = new AsyncLocalStorage();
|
|
22
|
+
return g[ALS_KEY];
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Run a function with a ServerStyleCollector bound to the current
|
|
26
|
+
* async context. All useStyles() calls within `fn` (and any async
|
|
27
|
+
* continuations) will find this collector via getSSRCollector().
|
|
28
|
+
*/
|
|
29
|
+
function runWithCollector(collector, fn) {
|
|
30
|
+
return getSharedStorage().run(collector, fn);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Retrieve the ServerStyleCollector bound to the current async context.
|
|
34
|
+
* Returns null when called outside of runWithCollector() or on the client.
|
|
35
|
+
*/
|
|
36
|
+
function getSSRCollector() {
|
|
37
|
+
const storage = getSharedStorage();
|
|
38
|
+
if (typeof storage?.getStore !== "function") return null;
|
|
39
|
+
return storage.getStore() ?? null;
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
export { getSSRCollector, runWithCollector };
|
|
43
|
+
|
|
44
|
+
//# 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 * Uses globalThis to ensure the AsyncLocalStorage instance is shared\n * across module instances — frameworks like Astro may load middleware\n * and page components from separate module graphs.\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 ALS_KEY = '__tasty_ssr_als__';\n\nfunction getSharedStorage(): AsyncLocalStorage<ServerStyleCollector> {\n const g = globalThis as Record<string, unknown>;\n if (!g[ALS_KEY]) {\n g[ALS_KEY] = new AsyncLocalStorage<ServerStyleCollector>();\n }\n return g[ALS_KEY] as AsyncLocalStorage<ServerStyleCollector>;\n}\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 getSharedStorage().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 const storage = getSharedStorage();\n if (typeof storage?.getStore !== 'function') return null;\n return storage.getStore() ?? null;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAoBA,MAAM,UAAU;AAEhB,SAAS,mBAA4D;CACnE,MAAM,IAAI;AACV,KAAI,CAAC,EAAE,SACL,GAAE,WAAW,IAAI,mBAAyC;AAE5D,QAAO,EAAE;;;;;;;AAQX,SAAgB,iBACd,WACA,IACG;AACH,QAAO,kBAAkB,CAAC,IAAI,WAAW,GAAG;;;;;;AAO9C,SAAgB,kBAA+C;CAC7D,MAAM,UAAU,kBAAkB;AAClC,KAAI,OAAO,SAAS,aAAa,WAAY,QAAO;AACpD,QAAO,QAAQ,UAAU,IAAI"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { parsePropertyToken } from "../properties/index.js";
|
|
2
|
+
import { PropertyTypeResolver } from "../properties/property-type-resolver.js";
|
|
3
|
+
import { pushRSCCSS } from "../rsc-cache.js";
|
|
4
|
+
import { formatPropertyCSS } from "./format-property.js";
|
|
5
|
+
//#region src/ssr/collect-auto-properties.ts
|
|
6
|
+
/**
|
|
7
|
+
* Scan rendered rules for auto-inferable custom properties and emit
|
|
8
|
+
* @property CSS via the provided callback.
|
|
9
|
+
*/
|
|
10
|
+
function scanAndEmitAutoProperties(rules, styles, emit) {
|
|
11
|
+
const registered = /* @__PURE__ */ new Set();
|
|
12
|
+
if (styles) {
|
|
13
|
+
const localProps = styles["@properties"];
|
|
14
|
+
if (localProps && typeof localProps === "object") for (const token of Object.keys(localProps)) {
|
|
15
|
+
const parsed = parsePropertyToken(token);
|
|
16
|
+
if (parsed.isValid) registered.add(parsed.cssName);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const resolver = new PropertyTypeResolver();
|
|
20
|
+
for (const rule of rules) {
|
|
21
|
+
if (!rule.declarations) continue;
|
|
22
|
+
resolver.scanDeclarations(rule.declarations, (name) => registered.has(name), (name, syntax, initialValue) => {
|
|
23
|
+
registered.add(name);
|
|
24
|
+
const css = formatPropertyCSS(name, {
|
|
25
|
+
syntax,
|
|
26
|
+
inherits: true,
|
|
27
|
+
initialValue
|
|
28
|
+
});
|
|
29
|
+
if (css) emit(name, css);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Scan rendered rules for custom property declarations and collect
|
|
35
|
+
* auto-inferred @property rules via the SSR collector.
|
|
36
|
+
*
|
|
37
|
+
* @param rules - Rendered style rules containing CSS declarations
|
|
38
|
+
* @param collector - SSR collector to emit @property CSS into
|
|
39
|
+
* @param styles - Original styles object (used to skip explicit @properties)
|
|
40
|
+
*/
|
|
41
|
+
function collectAutoInferredProperties(rules, collector, styles) {
|
|
42
|
+
scanAndEmitAutoProperties(rules, styles, (name, css) => {
|
|
43
|
+
collector.collectProperty(`__auto:${name}`, css);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* RSC variant: scan rendered rules and push auto-inferred @property CSS
|
|
48
|
+
* into the RSC pending buffer.
|
|
49
|
+
*/
|
|
50
|
+
function collectAutoInferredPropertiesRSC(rules, rscCache, styles) {
|
|
51
|
+
scanAndEmitAutoProperties(rules, styles, (name, css) => {
|
|
52
|
+
pushRSCCSS(rscCache, `__auto:${name}`, css);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
export { collectAutoInferredProperties, collectAutoInferredPropertiesRSC };
|
|
57
|
+
|
|
58
|
+
//# 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 / RSC 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 { RSCStyleCache } from '../rsc-cache';\nimport { pushRSCCSS } from '../rsc-cache';\nimport type { Styles } from '../styles/types';\n\nimport type { ServerStyleCollector } from './collector';\nimport { formatPropertyCSS } from './format-property';\n\n/**\n * Scan rendered rules for auto-inferable custom properties and emit\n * @property CSS via the provided callback.\n */\nfunction scanAndEmitAutoProperties(\n rules: StyleResult[],\n styles: Styles | undefined,\n emit: (name: string, css: string) => void,\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 emit(name, css);\n }\n },\n );\n }\n}\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 scanAndEmitAutoProperties(rules, styles, (name, css) => {\n collector.collectProperty(`__auto:${name}`, css);\n });\n}\n\n/**\n * RSC variant: scan rendered rules and push auto-inferred @property CSS\n * into the RSC pending buffer.\n */\nexport function collectAutoInferredPropertiesRSC(\n rules: StyleResult[],\n rscCache: RSCStyleCache,\n styles?: Styles,\n): void {\n scanAndEmitAutoProperties(rules, styles, (name, css) => {\n pushRSCCSS(rscCache, `__auto:${name}`, css);\n });\n}\n"],"mappings":";;;;;;;;;AAsBA,SAAS,0BACP,OACA,QACA,MACM;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,MAAK,MAAM,IAAI;IAGpB;;;;;;;;;;;AAYL,SAAgB,8BACd,OACA,WACA,QACM;AACN,2BAA0B,OAAO,SAAS,MAAM,QAAQ;AACtD,YAAU,gBAAgB,UAAU,QAAQ,IAAI;GAChD;;;;;;AAOJ,SAAgB,iCACd,OACA,UACA,QACM;AACN,2BAA0B,OAAO,SAAS,MAAM,QAAQ;AACtD,aAAW,UAAU,UAAU,QAAQ,IAAI;GAC3C"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { StyleResult } from "../pipeline/index.js";
|
|
2
|
+
|
|
3
|
+
//#region src/ssr/collector.d.ts
|
|
4
|
+
declare class ServerStyleCollector {
|
|
5
|
+
private chunks;
|
|
6
|
+
private cacheKeyToClassName;
|
|
7
|
+
private flushedKeys;
|
|
8
|
+
private propertyRules;
|
|
9
|
+
private flushedPropertyKeys;
|
|
10
|
+
private keyframeRules;
|
|
11
|
+
private flushedKeyframeKeys;
|
|
12
|
+
private globalStyles;
|
|
13
|
+
private flushedGlobalKeys;
|
|
14
|
+
private rawCSS;
|
|
15
|
+
private flushedRawKeys;
|
|
16
|
+
private fontFaceRules;
|
|
17
|
+
private flushedFontFaceKeys;
|
|
18
|
+
private counterStyleRules;
|
|
19
|
+
private flushedCounterStyleKeys;
|
|
20
|
+
private keyframesCounter;
|
|
21
|
+
private counterStyleCounter;
|
|
22
|
+
private internalsCollected;
|
|
23
|
+
/**
|
|
24
|
+
* Collect internal @property rules and :root token defaults.
|
|
25
|
+
* Mirrors markStylesGenerated() from the client-side injector.
|
|
26
|
+
* Called automatically on first chunk collection; idempotent.
|
|
27
|
+
*
|
|
28
|
+
* Internals are always emitted here — the RSC path deliberately
|
|
29
|
+
* defers to SSR so that tokens appear exactly once per page in
|
|
30
|
+
* <style data-tasty-ssr> (avoiding duplication of large token sets).
|
|
31
|
+
*/
|
|
32
|
+
collectInternals(): void;
|
|
33
|
+
/**
|
|
34
|
+
* Allocate a className for a cache key, server-side.
|
|
35
|
+
* Mirrors StyleInjector.allocateClassName but without DOM access.
|
|
36
|
+
*/
|
|
37
|
+
allocateClassName(cacheKey: string): {
|
|
38
|
+
className: string;
|
|
39
|
+
isNewAllocation: boolean;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Record CSS rules for a chunk.
|
|
43
|
+
* Called by useStyles during server render.
|
|
44
|
+
*/
|
|
45
|
+
collectChunk(cacheKey: string, className: string, rules: StyleResult[]): void;
|
|
46
|
+
/**
|
|
47
|
+
* Record a @property rule. Deduplicated by name.
|
|
48
|
+
*/
|
|
49
|
+
collectProperty(name: string, css: string): void;
|
|
50
|
+
/**
|
|
51
|
+
* Record a @keyframes rule. Deduplicated by name.
|
|
52
|
+
*/
|
|
53
|
+
collectKeyframes(name: string, css: string): void;
|
|
54
|
+
/**
|
|
55
|
+
* Allocate a keyframe name for SSR. Uses provided name or generates one.
|
|
56
|
+
*/
|
|
57
|
+
allocateKeyframeName(providedName?: string): string;
|
|
58
|
+
/**
|
|
59
|
+
* Record a @font-face rule. Deduplicated by key (content hash).
|
|
60
|
+
*/
|
|
61
|
+
collectFontFace(key: string, css: string): void;
|
|
62
|
+
/**
|
|
63
|
+
* Record a @counter-style rule. Deduplicated by name.
|
|
64
|
+
*/
|
|
65
|
+
collectCounterStyle(name: string, css: string): void;
|
|
66
|
+
/**
|
|
67
|
+
* Allocate a counter-style name for SSR. Uses provided name or generates one.
|
|
68
|
+
*/
|
|
69
|
+
allocateCounterStyleName(providedName?: string): string;
|
|
70
|
+
/**
|
|
71
|
+
* Record global styles (from useGlobalStyles). Deduplicated by key.
|
|
72
|
+
*/
|
|
73
|
+
collectGlobalStyles(key: string, css: string): void;
|
|
74
|
+
/**
|
|
75
|
+
* Record raw CSS text (from useRawCSS). Deduplicated by key.
|
|
76
|
+
*/
|
|
77
|
+
collectRawCSS(key: string, css: string): void;
|
|
78
|
+
/**
|
|
79
|
+
* Extract all CSS collected so far as a single string.
|
|
80
|
+
* Includes @property and @keyframes rules.
|
|
81
|
+
* Used for non-streaming SSR (renderToString).
|
|
82
|
+
*/
|
|
83
|
+
getCSS(): string;
|
|
84
|
+
/**
|
|
85
|
+
* Flush only newly collected CSS since the last flush.
|
|
86
|
+
* Used for streaming SSR (renderToPipeableStream + useServerInsertedHTML).
|
|
87
|
+
*/
|
|
88
|
+
flushCSS(): string;
|
|
89
|
+
private flushedClassNames;
|
|
90
|
+
/**
|
|
91
|
+
* Return class names rendered since the last call (for streaming).
|
|
92
|
+
* Used to emit lightweight class-list scripts for client hydration.
|
|
93
|
+
*/
|
|
94
|
+
getRenderedClassNames(): string[];
|
|
95
|
+
}
|
|
96
|
+
//#endregion
|
|
97
|
+
export { ServerStyleCollector };
|
|
98
|
+
//# sourceMappingURL=collector.d.ts.map
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { hashString } from "../utils/hash.js";
|
|
2
|
+
import { fontFaceContentHash, formatFontFaceRule } from "../font-face/index.js";
|
|
3
|
+
import { formatCounterStyleRule } from "../counter-style/index.js";
|
|
4
|
+
import { renderStyles } from "../pipeline/index.js";
|
|
5
|
+
import { getEffectiveProperties, getGlobalConfigTokens, getGlobalCounterStyle, getGlobalFontFace, getGlobalStyles } from "../config.js";
|
|
6
|
+
import { formatPropertyCSS } from "./format-property.js";
|
|
7
|
+
import { formatRules } from "./format-rules.js";
|
|
8
|
+
import { formatGlobalRules } from "./format-global-rules.js";
|
|
9
|
+
//#region src/ssr/collector.ts
|
|
10
|
+
/**
|
|
11
|
+
* ServerStyleCollector — server-safe style collector for SSR.
|
|
12
|
+
*
|
|
13
|
+
* Accumulates CSS rules and cache metadata during server rendering.
|
|
14
|
+
* This is the server-side counterpart to StyleInjector: it allocates
|
|
15
|
+
* hash-based class names (`t${hash}`), formats CSS rules into text,
|
|
16
|
+
* and tracks rendered class names for lightweight client transfer.
|
|
17
|
+
*
|
|
18
|
+
* One instance is created per HTTP request. Concurrent requests
|
|
19
|
+
* each get their own collector (via AsyncLocalStorage or React context).
|
|
20
|
+
*/
|
|
21
|
+
function generateClassName(cacheKey) {
|
|
22
|
+
return `t${hashString(cacheKey)}`;
|
|
23
|
+
}
|
|
24
|
+
var ServerStyleCollector = class {
|
|
25
|
+
chunks = /* @__PURE__ */ new Map();
|
|
26
|
+
cacheKeyToClassName = /* @__PURE__ */ new Map();
|
|
27
|
+
flushedKeys = /* @__PURE__ */ new Set();
|
|
28
|
+
propertyRules = /* @__PURE__ */ new Map();
|
|
29
|
+
flushedPropertyKeys = /* @__PURE__ */ new Set();
|
|
30
|
+
keyframeRules = /* @__PURE__ */ new Map();
|
|
31
|
+
flushedKeyframeKeys = /* @__PURE__ */ new Set();
|
|
32
|
+
globalStyles = /* @__PURE__ */ new Map();
|
|
33
|
+
flushedGlobalKeys = /* @__PURE__ */ new Set();
|
|
34
|
+
rawCSS = /* @__PURE__ */ new Map();
|
|
35
|
+
flushedRawKeys = /* @__PURE__ */ new Set();
|
|
36
|
+
fontFaceRules = /* @__PURE__ */ new Map();
|
|
37
|
+
flushedFontFaceKeys = /* @__PURE__ */ new Set();
|
|
38
|
+
counterStyleRules = /* @__PURE__ */ new Map();
|
|
39
|
+
flushedCounterStyleKeys = /* @__PURE__ */ new Set();
|
|
40
|
+
keyframesCounter = 0;
|
|
41
|
+
counterStyleCounter = 0;
|
|
42
|
+
internalsCollected = false;
|
|
43
|
+
/**
|
|
44
|
+
* Collect internal @property rules and :root token defaults.
|
|
45
|
+
* Mirrors markStylesGenerated() from the client-side injector.
|
|
46
|
+
* Called automatically on first chunk collection; idempotent.
|
|
47
|
+
*
|
|
48
|
+
* Internals are always emitted here — the RSC path deliberately
|
|
49
|
+
* defers to SSR so that tokens appear exactly once per page in
|
|
50
|
+
* <style data-tasty-ssr> (avoiding duplication of large token sets).
|
|
51
|
+
*/
|
|
52
|
+
collectInternals() {
|
|
53
|
+
if (this.internalsCollected) return;
|
|
54
|
+
this.internalsCollected = true;
|
|
55
|
+
for (const [token, definition] of Object.entries(getEffectiveProperties())) {
|
|
56
|
+
const css = formatPropertyCSS(token, definition);
|
|
57
|
+
if (css) this.collectProperty(`__prop:${token}`, css);
|
|
58
|
+
}
|
|
59
|
+
const tokenStyles = getGlobalConfigTokens();
|
|
60
|
+
if (tokenStyles && Object.keys(tokenStyles).length > 0) {
|
|
61
|
+
const tokenRules = renderStyles(tokenStyles, ":root");
|
|
62
|
+
if (tokenRules.length > 0) {
|
|
63
|
+
const css = formatGlobalRules(tokenRules);
|
|
64
|
+
if (css) this.collectGlobalStyles("__global:tokens", css);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const globalFF = getGlobalFontFace();
|
|
68
|
+
if (globalFF) for (const [family, input] of Object.entries(globalFF)) {
|
|
69
|
+
const descriptors = Array.isArray(input) ? input : [input];
|
|
70
|
+
for (const desc of descriptors) {
|
|
71
|
+
const hash = fontFaceContentHash(family, desc);
|
|
72
|
+
const css = formatFontFaceRule(family, desc);
|
|
73
|
+
this.collectFontFace(hash, css);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const globalCS = getGlobalCounterStyle();
|
|
77
|
+
if (globalCS) for (const [name, descriptors] of Object.entries(globalCS)) {
|
|
78
|
+
const css = formatCounterStyleRule(name, descriptors);
|
|
79
|
+
this.collectCounterStyle(name, css);
|
|
80
|
+
}
|
|
81
|
+
const globalStyles = getGlobalStyles();
|
|
82
|
+
if (globalStyles) {
|
|
83
|
+
for (const [selector, styles] of Object.entries(globalStyles)) if (Object.keys(styles).length > 0) {
|
|
84
|
+
const rules = renderStyles(styles, selector);
|
|
85
|
+
if (rules.length > 0) {
|
|
86
|
+
const css = formatGlobalRules(rules);
|
|
87
|
+
if (css) this.collectGlobalStyles(`__global:styles:${selector}`, css);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Allocate a className for a cache key, server-side.
|
|
94
|
+
* Mirrors StyleInjector.allocateClassName but without DOM access.
|
|
95
|
+
*/
|
|
96
|
+
allocateClassName(cacheKey) {
|
|
97
|
+
const existing = this.cacheKeyToClassName.get(cacheKey);
|
|
98
|
+
if (existing) return {
|
|
99
|
+
className: existing,
|
|
100
|
+
isNewAllocation: false
|
|
101
|
+
};
|
|
102
|
+
const className = generateClassName(cacheKey);
|
|
103
|
+
this.cacheKeyToClassName.set(cacheKey, className);
|
|
104
|
+
return {
|
|
105
|
+
className,
|
|
106
|
+
isNewAllocation: true
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Record CSS rules for a chunk.
|
|
111
|
+
* Called by useStyles during server render.
|
|
112
|
+
*/
|
|
113
|
+
collectChunk(cacheKey, className, rules) {
|
|
114
|
+
if (this.chunks.has(cacheKey)) return;
|
|
115
|
+
const css = formatRules(rules, className);
|
|
116
|
+
if (css) this.chunks.set(cacheKey, css);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Record a @property rule. Deduplicated by name.
|
|
120
|
+
*/
|
|
121
|
+
collectProperty(name, css) {
|
|
122
|
+
if (!this.propertyRules.has(name)) this.propertyRules.set(name, css);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Record a @keyframes rule. Deduplicated by name.
|
|
126
|
+
*/
|
|
127
|
+
collectKeyframes(name, css) {
|
|
128
|
+
if (!this.keyframeRules.has(name)) this.keyframeRules.set(name, css);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Allocate a keyframe name for SSR. Uses provided name or generates one.
|
|
132
|
+
*/
|
|
133
|
+
allocateKeyframeName(providedName) {
|
|
134
|
+
return providedName ?? `k${this.keyframesCounter++}`;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Record a @font-face rule. Deduplicated by key (content hash).
|
|
138
|
+
*/
|
|
139
|
+
collectFontFace(key, css) {
|
|
140
|
+
if (!this.fontFaceRules.has(key)) this.fontFaceRules.set(key, css);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Record a @counter-style rule. Deduplicated by name.
|
|
144
|
+
*/
|
|
145
|
+
collectCounterStyle(name, css) {
|
|
146
|
+
if (!this.counterStyleRules.has(name)) this.counterStyleRules.set(name, css);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Allocate a counter-style name for SSR. Uses provided name or generates one.
|
|
150
|
+
*/
|
|
151
|
+
allocateCounterStyleName(providedName) {
|
|
152
|
+
return providedName ?? `cs${this.counterStyleCounter++}`;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Record global styles (from useGlobalStyles). Deduplicated by key.
|
|
156
|
+
*/
|
|
157
|
+
collectGlobalStyles(key, css) {
|
|
158
|
+
if (!this.globalStyles.has(key)) this.globalStyles.set(key, css);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Record raw CSS text (from useRawCSS). Deduplicated by key.
|
|
162
|
+
*/
|
|
163
|
+
collectRawCSS(key, css) {
|
|
164
|
+
if (!this.rawCSS.has(key)) this.rawCSS.set(key, css);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Extract all CSS collected so far as a single string.
|
|
168
|
+
* Includes @property and @keyframes rules.
|
|
169
|
+
* Used for non-streaming SSR (renderToString).
|
|
170
|
+
*/
|
|
171
|
+
getCSS() {
|
|
172
|
+
const parts = [];
|
|
173
|
+
for (const css of this.propertyRules.values()) parts.push(css);
|
|
174
|
+
for (const css of this.fontFaceRules.values()) parts.push(css);
|
|
175
|
+
for (const css of this.counterStyleRules.values()) parts.push(css);
|
|
176
|
+
for (const css of this.rawCSS.values()) parts.push(css);
|
|
177
|
+
for (const css of this.globalStyles.values()) parts.push(css);
|
|
178
|
+
for (const css of this.chunks.values()) parts.push(css);
|
|
179
|
+
for (const css of this.keyframeRules.values()) parts.push(css);
|
|
180
|
+
return parts.join("\n");
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Flush only newly collected CSS since the last flush.
|
|
184
|
+
* Used for streaming SSR (renderToPipeableStream + useServerInsertedHTML).
|
|
185
|
+
*/
|
|
186
|
+
flushCSS() {
|
|
187
|
+
const parts = [];
|
|
188
|
+
for (const [name, css] of this.propertyRules) if (!this.flushedPropertyKeys.has(name)) {
|
|
189
|
+
parts.push(css);
|
|
190
|
+
this.flushedPropertyKeys.add(name);
|
|
191
|
+
}
|
|
192
|
+
for (const [key, css] of this.fontFaceRules) if (!this.flushedFontFaceKeys.has(key)) {
|
|
193
|
+
parts.push(css);
|
|
194
|
+
this.flushedFontFaceKeys.add(key);
|
|
195
|
+
}
|
|
196
|
+
for (const [key, css] of this.counterStyleRules) if (!this.flushedCounterStyleKeys.has(key)) {
|
|
197
|
+
parts.push(css);
|
|
198
|
+
this.flushedCounterStyleKeys.add(key);
|
|
199
|
+
}
|
|
200
|
+
for (const [key, css] of this.rawCSS) if (!this.flushedRawKeys.has(key)) {
|
|
201
|
+
parts.push(css);
|
|
202
|
+
this.flushedRawKeys.add(key);
|
|
203
|
+
}
|
|
204
|
+
for (const [key, css] of this.globalStyles) if (!this.flushedGlobalKeys.has(key)) {
|
|
205
|
+
parts.push(css);
|
|
206
|
+
this.flushedGlobalKeys.add(key);
|
|
207
|
+
}
|
|
208
|
+
for (const [key, css] of this.chunks) if (!this.flushedKeys.has(key)) {
|
|
209
|
+
parts.push(css);
|
|
210
|
+
this.flushedKeys.add(key);
|
|
211
|
+
}
|
|
212
|
+
for (const [name, css] of this.keyframeRules) if (!this.flushedKeyframeKeys.has(name)) {
|
|
213
|
+
parts.push(css);
|
|
214
|
+
this.flushedKeyframeKeys.add(name);
|
|
215
|
+
}
|
|
216
|
+
return parts.join("\n");
|
|
217
|
+
}
|
|
218
|
+
flushedClassNames = /* @__PURE__ */ new Set();
|
|
219
|
+
/**
|
|
220
|
+
* Return class names rendered since the last call (for streaming).
|
|
221
|
+
* Used to emit lightweight class-list scripts for client hydration.
|
|
222
|
+
*/
|
|
223
|
+
getRenderedClassNames() {
|
|
224
|
+
const names = [];
|
|
225
|
+
for (const className of this.cacheKeyToClassName.values()) if (!this.flushedClassNames.has(className)) {
|
|
226
|
+
this.flushedClassNames.add(className);
|
|
227
|
+
names.push(className);
|
|
228
|
+
}
|
|
229
|
+
return names;
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
//#endregion
|
|
233
|
+
export { ServerStyleCollector };
|
|
234
|
+
|
|
235
|
+
//# sourceMappingURL=collector.js.map
|