@umber-style/server 0.0.0-stage → 0.1.0-alpha.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 soroush.tech
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,13 @@
1
- # Temporary Holding Version
1
+ # @umber-style/server
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Server rendering over an UmberStyle cache: the CSS of one page, as chunks and as `<style data-umber>` tags.
4
+
5
+ Install: `pnpm add @umber-style/server`.
6
+
7
+ ```ts
8
+ import createInstance from '@umber-style/server/create-instance'
9
+
10
+ const { extractCriticalToChunks, constructStyleTagsFromChunks } = createInstance(cache)
11
+ ```
12
+
13
+ Documentation: [docs](../../docs/README.md), and [The cache and server packages](../../docs/cache-and-server.md) for this package.
@@ -0,0 +1,54 @@
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
4
+ });
5
+ let _umber_style_core = require("@umber-style/core");
6
+ // src/createInstance.ts
7
+ const HEAD_PATTERN = /<head(?:\s[^>]*)?>/i;
8
+ /** The style element of one chunk. */
9
+ function createTag({ key, ids, css }) {
10
+ return `<style data-umber="${[key, ...ids].join(" ")}">${css}</style>`;
11
+ }
12
+ /**
13
+ * The server functions of `cache` (an `@umber-style/cache` cache, or any engine cache), a thin layer over
14
+ * `extractPageStyleParts`: the CSS of a page is decided from the class names in its HTML, plus the global
15
+ * rules, the `@keyframes` rules those use and the theme classes trimmed to the variables in use. The cache
16
+ * is not changed, so one cache can serve pages rendered at once, and the same page gets the same CSS.
17
+ */
18
+ function createInstance(cache) {
19
+ const key = cache.prefix;
20
+ const extractCriticalToChunks = (html) => {
21
+ const { global, classes } = (0, _umber_style_core.extractPageStyleParts)(cache, html);
22
+ return {
23
+ html,
24
+ styles: [{
25
+ key: `${key}-global`,
26
+ ...global
27
+ }, {
28
+ key,
29
+ ...classes
30
+ }]
31
+ };
32
+ };
33
+ const constructStyleTagsFromChunks = ({ styles }) => styles.filter(({ css }) => css !== "").map(createTag).join("");
34
+ return {
35
+ extractCritical(html) {
36
+ const { styles } = extractCriticalToChunks(html);
37
+ return {
38
+ html,
39
+ css: styles.map(({ css }) => css).join(""),
40
+ ids: styles.flatMap(({ ids }) => ids)
41
+ };
42
+ },
43
+ extractCriticalToChunks,
44
+ constructStyleTagsFromChunks,
45
+ renderStylesToString(html) {
46
+ const tags = constructStyleTagsFromChunks(extractCriticalToChunks(html));
47
+ const head = HEAD_PATTERN.exec(html);
48
+ if (head === null) return tags + html;
49
+ const at = head.index + head[0].length;
50
+ return html.slice(0, at) + tags + html.slice(at);
51
+ }
52
+ };
53
+ }
54
+ exports.default = createInstance;
@@ -0,0 +1,41 @@
1
+ import { Cache } from "@umber-style/core";
2
+ /** The CSS of one key of a page, and the ids it covers. */
3
+ export interface StyleChunk {
4
+ /** `<key>-global` for the rules that no class stands for, `<key>` for the rules of the classes. */
5
+ key: string;
6
+ ids: string[];
7
+ css: string;
8
+ }
9
+ /** What `extractCritical` returns: the HTML it was given, the CSS of the page, and the ids it covers. */
10
+ export interface CriticalStyles {
11
+ html: string;
12
+ css: string;
13
+ ids: string[];
14
+ }
15
+ /** What `extractCriticalToChunks` returns: the HTML it was given, and the CSS of the page in chunks. */
16
+ export interface CriticalChunks {
17
+ html: string;
18
+ styles: StyleChunk[];
19
+ }
20
+ /** The server functions of one cache. */
21
+ export interface ServerInstance {
22
+ /** The CSS that `html` needs, in one piece. */
23
+ extractCritical(html: string): CriticalStyles;
24
+ /**
25
+ * The CSS that `html` needs in two chunks, in this order: `<key>-global` (the layer order, the
26
+ * `@property`, `@keyframes`, theme and global rules; its ids are the theme classes and the global ids)
27
+ * and `<key>` (the rules of the classes of the page; its ids are the classes).
28
+ */
29
+ extractCriticalToChunks(html: string): CriticalChunks;
30
+ /** One `<style data-umber="<key> <ids>">` element for each chunk that has CSS, in order, as text. */
31
+ constructStyleTagsFromChunks(chunks: Pick<CriticalChunks, 'styles'>): string;
32
+ /** `html` with the style elements of its chunks put first: right after its `<head>` when it has one, else at its start. */
33
+ renderStylesToString(html: string): string;
34
+ }
35
+ /**
36
+ * The server functions of `cache` (an `@umber-style/cache` cache, or any engine cache), a thin layer over
37
+ * `extractPageStyleParts`: the CSS of a page is decided from the class names in its HTML, plus the global
38
+ * rules, the `@keyframes` rules those use and the theme classes trimmed to the variables in use. The cache
39
+ * is not changed, so one cache can serve pages rendered at once, and the same page gets the same CSS.
40
+ */
41
+ export default function createInstance(cache: Cache): ServerInstance;
@@ -0,0 +1,41 @@
1
+ import { Cache } from "@umber-style/core";
2
+ /** The CSS of one key of a page, and the ids it covers. */
3
+ export interface StyleChunk {
4
+ /** `<key>-global` for the rules that no class stands for, `<key>` for the rules of the classes. */
5
+ key: string;
6
+ ids: string[];
7
+ css: string;
8
+ }
9
+ /** What `extractCritical` returns: the HTML it was given, the CSS of the page, and the ids it covers. */
10
+ export interface CriticalStyles {
11
+ html: string;
12
+ css: string;
13
+ ids: string[];
14
+ }
15
+ /** What `extractCriticalToChunks` returns: the HTML it was given, and the CSS of the page in chunks. */
16
+ export interface CriticalChunks {
17
+ html: string;
18
+ styles: StyleChunk[];
19
+ }
20
+ /** The server functions of one cache. */
21
+ export interface ServerInstance {
22
+ /** The CSS that `html` needs, in one piece. */
23
+ extractCritical(html: string): CriticalStyles;
24
+ /**
25
+ * The CSS that `html` needs in two chunks, in this order: `<key>-global` (the layer order, the
26
+ * `@property`, `@keyframes`, theme and global rules; its ids are the theme classes and the global ids)
27
+ * and `<key>` (the rules of the classes of the page; its ids are the classes).
28
+ */
29
+ extractCriticalToChunks(html: string): CriticalChunks;
30
+ /** One `<style data-umber="<key> <ids>">` element for each chunk that has CSS, in order, as text. */
31
+ constructStyleTagsFromChunks(chunks: Pick<CriticalChunks, 'styles'>): string;
32
+ /** `html` with the style elements of its chunks put first: right after its `<head>` when it has one, else at its start. */
33
+ renderStylesToString(html: string): string;
34
+ }
35
+ /**
36
+ * The server functions of `cache` (an `@umber-style/cache` cache, or any engine cache), a thin layer over
37
+ * `extractPageStyleParts`: the CSS of a page is decided from the class names in its HTML, plus the global
38
+ * rules, the `@keyframes` rules those use and the theme classes trimmed to the variables in use. The cache
39
+ * is not changed, so one cache can serve pages rendered at once, and the same page gets the same CSS.
40
+ */
41
+ export default function createInstance(cache: Cache): ServerInstance;
@@ -0,0 +1,50 @@
1
+ import { extractPageStyleParts } from "@umber-style/core";
2
+ // src/createInstance.ts
3
+ const HEAD_PATTERN = /<head(?:\s[^>]*)?>/i;
4
+ /** The style element of one chunk. */
5
+ function createTag({ key, ids, css }) {
6
+ return `<style data-umber="${[key, ...ids].join(" ")}">${css}</style>`;
7
+ }
8
+ /**
9
+ * The server functions of `cache` (an `@umber-style/cache` cache, or any engine cache), a thin layer over
10
+ * `extractPageStyleParts`: the CSS of a page is decided from the class names in its HTML, plus the global
11
+ * rules, the `@keyframes` rules those use and the theme classes trimmed to the variables in use. The cache
12
+ * is not changed, so one cache can serve pages rendered at once, and the same page gets the same CSS.
13
+ */
14
+ function createInstance(cache) {
15
+ const key = cache.prefix;
16
+ const extractCriticalToChunks = (html) => {
17
+ const { global, classes } = extractPageStyleParts(cache, html);
18
+ return {
19
+ html,
20
+ styles: [{
21
+ key: `${key}-global`,
22
+ ...global
23
+ }, {
24
+ key,
25
+ ...classes
26
+ }]
27
+ };
28
+ };
29
+ const constructStyleTagsFromChunks = ({ styles }) => styles.filter(({ css }) => css !== "").map(createTag).join("");
30
+ return {
31
+ extractCritical(html) {
32
+ const { styles } = extractCriticalToChunks(html);
33
+ return {
34
+ html,
35
+ css: styles.map(({ css }) => css).join(""),
36
+ ids: styles.flatMap(({ ids }) => ids)
37
+ };
38
+ },
39
+ extractCriticalToChunks,
40
+ constructStyleTagsFromChunks,
41
+ renderStylesToString(html) {
42
+ const tags = constructStyleTagsFromChunks(extractCriticalToChunks(html));
43
+ const head = HEAD_PATTERN.exec(html);
44
+ if (head === null) return tags + html;
45
+ const at = head.index + head[0].length;
46
+ return html.slice(0, at) + tags + html.slice(at);
47
+ }
48
+ };
49
+ }
50
+ export { createInstance as default };
package/package.json CHANGED
@@ -1,6 +1,73 @@
1
1
  {
2
2
  "name": "@umber-style/server",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0-alpha.0",
4
+ "description": "Server rendering over an UmberStyle cache: the CSS of one page as chunks and as style tags.",
5
+ "keywords": [
6
+ "styling",
7
+ "ssr",
8
+ "critical-css",
9
+ "css-variables",
10
+ "typescript"
11
+ ],
12
+ "license": "MIT",
13
+ "author": "Masoud Soroush <masoud@soroush.tech>",
14
+ "homepage": "https://umber-style.soroush.design",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/soroush-tech/umber-style.git",
18
+ "directory": "packages/server"
19
+ },
20
+ "bugs": {
21
+ "url": "https://github.com/soroush-tech/umber-style/issues"
22
+ },
23
+ "type": "module",
24
+ "sideEffects": false,
25
+ "files": [
26
+ "dist"
27
+ ],
28
+ "exports": {
29
+ "./create-instance": {
30
+ "import": {
31
+ "types": "./dist/createInstance.d.mts",
32
+ "default": "./dist/createInstance.mjs"
33
+ },
34
+ "require": {
35
+ "types": "./dist/createInstance.d.cts",
36
+ "default": "./dist/createInstance.cjs"
37
+ }
38
+ }
39
+ },
40
+ "publishConfig": {
41
+ "access": "public"
42
+ },
43
+ "dependencies": {
44
+ "@umber-style/core": "0.1.0-alpha.2"
45
+ },
46
+ "devDependencies": {
47
+ "@types/node": "^26.6.6",
48
+ "@types/react": "^19.3.0",
49
+ "@types/react-dom": "^19.3.0",
50
+ "@vitest/coverage-v8": "^5.1.0",
51
+ "jsdom": "^31.0.0",
52
+ "oxlint": "^1.93.0",
53
+ "react": "^19.3.1",
54
+ "react-dom": "^19.3.1",
55
+ "tsdown": "^0.23.7",
56
+ "typescript": "~7.0.2",
57
+ "vitest": "^5.1.0",
58
+ "@umber-style/cache": "0.1.0",
59
+ "@umber-style/css": "0.1.0",
60
+ "@umber-style/eslint-config": "0.0.0",
61
+ "@umber-style/react": "0.1.0"
62
+ },
63
+ "scripts": {
64
+ "build": "tsdown",
65
+ "lint": "oxlint --deny-warnings",
66
+ "typecheck": "tsc --noEmit",
67
+ "test": "vitest run",
68
+ "test:coverage": "vitest run --coverage"
69
+ },
70
+ "main": "./dist/createInstance.cjs",
71
+ "module": "./dist/createInstance.mjs",
72
+ "types": "./dist/createInstance.d.mts"
6
73
  }