@hiscovega/grisso 1.0.5 → 1.0.7

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/lib/tokens.js ADDED
@@ -0,0 +1,133 @@
1
+ import { resolveConfig } from "./resolve-config.js";
2
+ /** Orden fijo de secciones — matchea defaults.ts y tokens-example.css */
3
+ const TOKEN_MAP_KEYS = [
4
+ "spacing",
5
+ "brandColors",
6
+ "foregroundColors",
7
+ "iconColors",
8
+ "supportColors",
9
+ "backgroundColors",
10
+ "overlayColors",
11
+ "borderWidth",
12
+ "opacity",
13
+ "shadows",
14
+ "borderColors",
15
+ ];
16
+ /** Labels para los headers CSS de cada sección */
17
+ const SECTION_LABELS = {
18
+ spacing: "Spacing",
19
+ brandColors: "Colors: marca",
20
+ foregroundColors: "Colors: texto",
21
+ iconColors: "Colors: iconos",
22
+ supportColors: "Colors: soporte",
23
+ backgroundColors: "Colors: fondo",
24
+ overlayColors: "Colors: overlays",
25
+ borderWidth: "Border widths",
26
+ opacity: "Opacidad",
27
+ shadows: "Sombras",
28
+ borderColors: "Colors: bordes",
29
+ };
30
+ /** Keys que NO son token maps */
31
+ const NON_TOKEN_KEYS = new Set([
32
+ "columns",
33
+ "breakpoints",
34
+ "states",
35
+ "safelist",
36
+ "extend",
37
+ ]);
38
+ const VAR_RE = /var\(--([\w-]+)\)/g;
39
+ /** Extrae nombres de custom properties de un token map, deduplicados y en orden */
40
+ function extractVarNames(tokenMap) {
41
+ const seen = new Set();
42
+ const result = [];
43
+ for (const value of Object.values(tokenMap)) {
44
+ for (const match of value.matchAll(VAR_RE)) {
45
+ const name = match[1];
46
+ if (!seen.has(name)) {
47
+ seen.add(name);
48
+ result.push(name);
49
+ }
50
+ }
51
+ }
52
+ return result;
53
+ }
54
+ /** Genera scaffold CSS con todas las custom properties */
55
+ function formatCSS(config) {
56
+ const lines = [];
57
+ lines.push("/* @hiscovega/grisso — tokens scaffold", " Define estas variables en :root o en el scope que prefieras.", " Generado desde la config resuelta de Grisso.", "*/", "", ":root {");
58
+ let hasSections = false;
59
+ // Secciones conocidas en orden fijo
60
+ for (const key of TOKEN_MAP_KEYS) {
61
+ const map = config[key];
62
+ if (!map || typeof map !== "object")
63
+ continue;
64
+ const vars = extractVarNames(map);
65
+ if (vars.length === 0)
66
+ continue;
67
+ const label = SECTION_LABELS[key] || key;
68
+ if (hasSections)
69
+ lines.push("");
70
+ lines.push(`\t/* ─── ${label} ${"─".repeat(Math.max(1, 55 - label.length))} */`);
71
+ for (const name of vars) {
72
+ lines.push(`\t--${name}: ;`);
73
+ }
74
+ hasSections = true;
75
+ }
76
+ // Secciones custom del consumidor (keys desconocidas que sean objetos)
77
+ for (const [key, value] of Object.entries(config)) {
78
+ if (NON_TOKEN_KEYS.has(key))
79
+ continue;
80
+ if (TOKEN_MAP_KEYS.includes(key))
81
+ continue;
82
+ if (!value || typeof value !== "object" || Array.isArray(value))
83
+ continue;
84
+ const vars = extractVarNames(value);
85
+ if (vars.length === 0)
86
+ continue;
87
+ if (hasSections)
88
+ lines.push("");
89
+ lines.push(`\t/* ─── ${key} ${"─".repeat(Math.max(1, 55 - key.length))} */`);
90
+ for (const name of vars) {
91
+ lines.push(`\t--${name}: ;`);
92
+ }
93
+ hasSections = true;
94
+ }
95
+ lines.push("}", "");
96
+ return lines.join("\n");
97
+ }
98
+ /** Genera JSON con los token maps de la config resuelta */
99
+ function formatJSON(config) {
100
+ const result = {};
101
+ for (const key of TOKEN_MAP_KEYS) {
102
+ const map = config[key];
103
+ if (map && typeof map === "object") {
104
+ result[key] = map;
105
+ }
106
+ }
107
+ // Incluir token maps custom del consumidor
108
+ for (const [key, value] of Object.entries(config)) {
109
+ if (NON_TOKEN_KEYS.has(key))
110
+ continue;
111
+ if (TOKEN_MAP_KEYS.includes(key))
112
+ continue;
113
+ if (!value || typeof value !== "object" || Array.isArray(value))
114
+ continue;
115
+ result[key] = value;
116
+ }
117
+ return JSON.stringify(result, null, "\t");
118
+ }
119
+ /**
120
+ * Extrae tokens de la config resuelta y genera un scaffold CSS o JSON.
121
+ *
122
+ * @example
123
+ * const css = await extractTokens();
124
+ * const json = await extractTokens({ format: "json" });
125
+ */
126
+ export async function extractTokens(options = {}) {
127
+ const { config: configPath, format = "css" } = options;
128
+ const config = await resolveConfig(configPath);
129
+ if (format === "json") {
130
+ return formatJSON(config);
131
+ }
132
+ return formatCSS(config);
133
+ }
package/lib/types.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  /** Mapa de breakpoints: nombre → media query */
2
2
  export type Breakpoints = Record<string, string>;
3
+ /** Mapa de states: nombre → pseudo-clase CSS (e.g. hover → ":hover") */
4
+ export type States = Record<string, string>;
3
5
  /** Mapa de tokens: nombre → valor CSS */
4
6
  export type TokenMap = Record<string, string>;
5
7
  /** Mapa de declaraciones CSS: propiedad → valor */
@@ -8,6 +10,7 @@ export type Declarations = Record<string, string>;
8
10
  export interface GrissoConfig {
9
11
  columns: number;
10
12
  breakpoints: Breakpoints;
13
+ states: States;
11
14
  spacing: TokenMap;
12
15
  brandColors: TokenMap;
13
16
  foregroundColors: TokenMap;
@@ -19,6 +22,8 @@ export interface GrissoConfig {
19
22
  opacity: TokenMap;
20
23
  shadows: TokenMap;
21
24
  borderColors: TokenMap;
25
+ /** Patrones de clases protegidas del tree-shaking */
26
+ safelist: (string | RegExp)[];
22
27
  [key: string]: unknown;
23
28
  }
24
29
  /** Función parcial que genera CSS a partir de la config */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hiscovega/grisso",
3
3
  "description": "Griddo CSS utility class library",
4
- "version": "1.0.5",
4
+ "version": "1.0.7",
5
5
  "license": "UNLICENSED",
6
6
  "private": false,
7
7
  "type": "module",
@@ -10,6 +10,7 @@
10
10
  "style": "./dist/grisso.css"
11
11
  },
12
12
  "./build": "./lib/build.js",
13
+ "./tokens": "./lib/tokens.js",
13
14
  "./config": "./lib/defaults.js",
14
15
  "./tokens-example.css": "./tokens-example.css"
15
16
  },