@projectwallace/css-design-tokens 0.8.1 → 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.
@@ -1,18 +1,18 @@
1
- import { cssKeywords as T, colorKeywords as W, namedColors as K, systemColors as M, colorFunctions as Z, analyze as U } from "@projectwallace/css-analyzer";
1
+ import { cssKeywords as B, colorKeywords as G, namedColors as K, systemColors as M, colorFunctions as U, analyze as Z } from "@projectwallace/css-analyzer";
2
2
  import { convert as V } from "css-time-sort";
3
3
  import { convert as J } from "color-sorter";
4
4
  import { parse as $ } from "css-tree";
5
5
  import { ColorSpace as o, sRGB as Q, XYZ_D65 as ee, XYZ_D50 as te, XYZ_ABS_D65 as re, Lab_D65 as ne, Lab as ie, LCH as le, sRGB_Linear as se, HSL as oe, HWB as ue, HSV as ae, P3_Linear as fe, P3 as ce, A98RGB_Linear as pe, A98RGB as me, ProPhoto_Linear as ge, ProPhoto as he, REC_2020_Linear as de, REC_2020 as _e, OKLab as ve, OKLCH as $e, OKLrab as be, parse as xe } from "colorjs.io/fn";
6
- const A = 0, j = 1, z = 2, y = 3, I = 4, R = 5, H = 6, X = 7, D = 8, P = 9, Y = 10, v = 11, ye = /* @__PURE__ */ new Map([
6
+ const A = 0, j = 1, z = 2, y = 3, I = 4, R = 5, D = 6, H = 7, X = 8, P = 9, Y = 10, v = 11, ye = /* @__PURE__ */ new Map([
7
7
  [A, "white"],
8
8
  [j, "black"],
9
9
  [z, "grey"],
10
10
  [y, "red"],
11
11
  [I, "orange"],
12
12
  [R, "yellow"],
13
- [H, "green"],
14
- [X, "cyan"],
15
- [D, "blue"],
13
+ [D, "green"],
14
+ [H, "cyan"],
15
+ [X, "blue"],
16
16
  [P, "magenta"],
17
17
  [Y, "pink"],
18
18
  [v, "unknown"]
@@ -23,7 +23,7 @@ function we(n) {
23
23
  let e = v;
24
24
  if (!i.includes("var(") && !i.includes("calc(")) {
25
25
  let s = J(i), { hue: l, saturation: r, lightness: f } = s;
26
- r < 10 && f === 100 ? e = A : r < 10 && f === 0 ? e = j : r < 5 ? e = z : l < 22 ? e = y : l < 50 ? e = I : l < 72 ? e = R : l < 144 ? e = H : l < 180 ? e = X : l < 250 ? e = D : l < 300 ? e = P : l < 350 ? e = Y : e = y;
26
+ r < 10 && f === 100 ? e = A : r < 10 && f === 0 ? e = j : r < 5 ? e = z : l < 22 ? e = y : l < 50 ? e = I : l < 72 ? e = R : l < 144 ? e = D : l < 180 ? e = H : l < 250 ? e = X : l < 300 ? e = P : l < 350 ? e = Y : e = y;
27
27
  }
28
28
  t.has(e) ? t.get(e).push(i) : t.set(e, [i]);
29
29
  }
@@ -62,24 +62,20 @@ function g(n) {
62
62
  components: [0, 0, 0],
63
63
  alpha: 0
64
64
  };
65
- if (T.has(t) || W.has(t) || t.includes("var("))
65
+ if (B.has(t) || G.has(t) || t.includes("var("))
66
66
  return null;
67
67
  try {
68
68
  let i = xe(n), [e, s, l] = i.coords;
69
69
  return {
70
70
  colorSpace: i.spaceId,
71
- components: [
72
- e ?? "none",
73
- s ?? "none",
74
- l ?? "none"
75
- ],
76
- alpha: i.alpha ?? 0
71
+ components: [e ?? "none", s ?? "none", l ?? "none"],
72
+ alpha: i.alpha ?? 1
77
73
  };
78
74
  } catch {
79
75
  return null;
80
76
  }
81
77
  }
82
- function O() {
78
+ function N() {
83
79
  return {
84
80
  color: void 0,
85
81
  offsetX: void 0,
@@ -97,7 +93,7 @@ function Se(n) {
97
93
  function i(l) {
98
94
  return l.loc ? n.slice(l.loc.start.offset, l.loc.end.offset) : "";
99
95
  }
100
- let e = O(), s = [e];
96
+ let e = N(), s = [e];
101
97
  return t.children.size < 2 ? null : (t.children.forEach((l) => {
102
98
  if (l.type === "Identifier") {
103
99
  if (l.name.toLowerCase() === "inset")
@@ -118,7 +114,7 @@ function Se(n) {
118
114
  };
119
115
  e.offsetX ? e.offsetY ? e.blur ? e.spread || (e.spread = r) : e.blur = r : e.offsetY = r : e.offsetX = r;
120
116
  } else if (l.type === "Function") {
121
- if (Z.has(l.name)) {
117
+ if (U.has(l.name)) {
122
118
  let r = g(i(l));
123
119
  if (r === null)
124
120
  return;
@@ -134,14 +130,14 @@ function Se(n) {
134
130
  if (r === null)
135
131
  return;
136
132
  e.color = r;
137
- } else l.type === "Operator" && l.value === "," && (L(e), e = O(), s.push(e));
138
- }), L(e), s);
133
+ } else l.type === "Operator" && l.value === "," && (O(e), e = N(), s.push(e));
134
+ }), O(e), s);
139
135
  }
140
136
  const _ = {
141
137
  value: 0,
142
138
  unit: "px"
143
139
  };
144
- function L(n) {
140
+ function O(n) {
145
141
  return n.offsetX || (n.offsetX = _), n.offsetY || (n.offsetY = _), n.blur || (n.blur = _), n.spread || (n.spread = _), n.color || (n.color = g("#000")), n;
146
142
  }
147
143
  const E = /* @__PURE__ */ new Map([
@@ -151,7 +147,7 @@ const E = /* @__PURE__ */ new Map([
151
147
  ["ease-out", [0, 0, 0.58, 1]],
152
148
  ["ease-in-out", [0.42, 0, 0.58, 1]]
153
149
  ]);
154
- function Ne(n) {
150
+ function Le(n) {
155
151
  if (n = n.trim().toLowerCase(), E.has(n))
156
152
  return E.get(n);
157
153
  if (n.includes("var("))
@@ -166,7 +162,7 @@ function Ne(n) {
166
162
  function C(n) {
167
163
  return n.replaceAll(/^['"]|['"]$/g, "");
168
164
  }
169
- function Oe(n) {
165
+ function Ne(n) {
170
166
  if (n.toLowerCase().includes("var("))
171
167
  return;
172
168
  let t = $(n, {
@@ -198,17 +194,21 @@ function a(n) {
198
194
  t = (t << 5) - t + i.charCodeAt(0), t |= 0;
199
195
  return (t >>> 0).toString(16);
200
196
  }
201
- function Le(n) {
197
+ const Oe = /* @__PURE__ */ new Set(["px", "rem"]);
198
+ function Ee(n) {
202
199
  let t = $(n, { context: "value" });
203
200
  if (t.children === null || t.children.size !== 1) return null;
204
201
  let i = t.children.first;
205
202
  switch (i.type) {
206
203
  case "Dimension": {
207
204
  let e = Number(i.value);
208
- return e === 0 ? 0 : {
205
+ if (e === 0)
206
+ return 0;
207
+ let s = i.unit.toLowerCase();
208
+ return Oe.has(s) ? {
209
209
  value: e,
210
- unit: i.unit
211
- };
210
+ unit: s
211
+ } : null;
212
212
  }
213
213
  case "Number":
214
214
  return Number(i.value);
@@ -230,7 +230,7 @@ let q = /* @__PURE__ */ new Map([
230
230
  ["xx-large", 2],
231
231
  ["xxx-large", 3]
232
232
  ]);
233
- function Ee(n) {
233
+ function Ce(n) {
234
234
  let t = $(n, { context: "value" });
235
235
  if (t.children === null || t.children.size !== 1) return null;
236
236
  let i = t.children.first;
@@ -269,9 +269,9 @@ function Ee(n) {
269
269
  }
270
270
  return null;
271
271
  }
272
- function Re(n) {
273
- let t = U(n);
274
- return Ce(t);
272
+ function De(n) {
273
+ let t = Z(n);
274
+ return qe(t);
275
275
  }
276
276
  function p(n) {
277
277
  return "uniqueWithLocations" in n ? n.uniqueWithLocations : n.unique;
@@ -279,7 +279,7 @@ function p(n) {
279
279
  function m(n) {
280
280
  return Array.isArray(n) ? n.length : n;
281
281
  }
282
- function Ce(n) {
282
+ function qe(n) {
283
283
  return {
284
284
  color: (() => {
285
285
  let t = /* @__PURE__ */ Object.create(null), i = p(n.values.colors), e = we(i);
@@ -287,7 +287,7 @@ function Ce(n) {
287
287
  for (let r of l) {
288
288
  let f = g(r), w = m(i[r]);
289
289
  if (f !== null) {
290
- let { colorSpace: k, components: F, alpha: B } = f, h = `${ye.get(s)}-${a([k, ...F, B].join(""))}`, G = n.values.colors.itemsPerContext, S = Object.entries(G).reduce((d, [N, b]) => r in b.unique || b.uniqueWithLocations && r in b.uniqueWithLocations ? d.add(N) : d, /* @__PURE__ */ new Set());
290
+ let { colorSpace: k, components: F, alpha: T } = f, h = `${ye.get(s)}-${a([k, ...F, T].join(""))}`, W = n.values.colors.itemsPerContext, S = Object.entries(W).reduce((d, [L, b]) => r in b.unique || b.uniqueWithLocations && r in b.uniqueWithLocations ? d.add(L) : d, /* @__PURE__ */ new Set());
291
291
  if (t[h]) {
292
292
  let d = t[h].$extensions[x];
293
293
  t[h].$extensions[x] = Array.from(new Set(d).union(S)), t[h].$extensions[u] += w;
@@ -308,7 +308,7 @@ function Ce(n) {
308
308
  font_size: (() => {
309
309
  let t = /* @__PURE__ */ Object.create(null), i = p(n.values.fontSizes);
310
310
  for (let e in i) {
311
- let s = Ee(e), l = {
311
+ let s = Ce(e), l = {
312
312
  [c]: e,
313
313
  [u]: m(i[e])
314
314
  };
@@ -332,7 +332,7 @@ function Ce(n) {
332
332
  font_family: (() => {
333
333
  let t = /* @__PURE__ */ Object.create(null), i = p(n.values.fontFamilies);
334
334
  for (let e in i) {
335
- let s = Oe(e), l = `fontFamily-${a(e)}`, r = {
335
+ let s = Ne(e), l = `fontFamily-${a(e)}`, r = {
336
336
  [c]: e,
337
337
  [u]: m(i[e])
338
338
  };
@@ -350,7 +350,7 @@ function Ce(n) {
350
350
  line_height: (() => {
351
351
  let t = /* @__PURE__ */ Object.create(null), i = p(n.values.lineHeights);
352
352
  for (let e in i) {
353
- let s = Le(e), l = {
353
+ let s = Ee(e), l = {
354
354
  [c]: e,
355
355
  [u]: m(i[e])
356
356
  };
@@ -459,7 +459,7 @@ function Ce(n) {
459
459
  easing: (() => {
460
460
  let t = /* @__PURE__ */ Object.create(null), i = p(n.values.animations.timingFunctions);
461
461
  for (let e in i) {
462
- let s = `easing-${a(e)}`, l = Ne(e), r = {
462
+ let s = `easing-${a(e)}`, l = Le(e), r = {
463
463
  [c]: e,
464
464
  [u]: m(i[e])
465
465
  };
@@ -477,6 +477,10 @@ function Ce(n) {
477
477
  };
478
478
  }
479
479
  export {
480
- Ce as analysis_to_tokens,
481
- Re as css_to_tokens
480
+ c as EXTENSION_AUTHORED_AS,
481
+ x as EXTENSION_CSS_PROPERTIES,
482
+ u as EXTENSION_USAGE_COUNT,
483
+ qe as analysis_to_tokens,
484
+ g as color_to_token,
485
+ De as css_to_tokens
482
486
  };
@@ -1,9 +1,9 @@
1
1
  import { ColorValue } from './types.js';
2
- type CssLength = {
2
+ export type CssLength = {
3
3
  value: number;
4
4
  unit: string;
5
5
  };
6
- export type DestructuredShadow = {
6
+ export type ShadowValue = {
7
7
  color: ColorValue | undefined;
8
8
  offsetX: CssLength | undefined;
9
9
  offsetY: CssLength | undefined;
@@ -11,5 +11,4 @@ export type DestructuredShadow = {
11
11
  spread: CssLength | undefined;
12
12
  inset: boolean | undefined;
13
13
  };
14
- export declare function destructure_box_shadow(value: string): null | DestructuredShadow[];
15
- export {};
14
+ export declare function destructure_box_shadow(value: string): null | ShadowValue[];
@@ -1 +1,2 @@
1
- export declare function destructure_font_family(value: string): string[] | undefined;
1
+ import { FontFamilyValue } from './types.js';
2
+ export declare function destructure_font_family(value: string): FontFamilyValue | undefined;
@@ -1,7 +1,4 @@
1
- type Length = {
2
- value: number;
3
- unit: string;
4
- };
1
+ import { Length } from './types';
5
2
  /**
6
3
  * @description Destructure a line-height from a string
7
4
  * The line-height property is specified as any one of the following:
@@ -11,4 +8,3 @@ type Length = {
11
8
  * - the keyword `normal`.
12
9
  */
13
10
  export declare function destructure_line_height(value: string): Length | number | null;
14
- export {};
package/dist/index.d.ts CHANGED
@@ -1,4 +1,7 @@
1
1
  import { CubicBezierToken, DimensionToken, DurationToken, FontFamilyToken, NumberToken, UnparsedToken, ColorToken, CssAnalysis, ShadowToken } from './types.js';
2
+ export { color_to_token } from './colors.js';
3
+ export { EXTENSION_AUTHORED_AS, EXTENSION_USAGE_COUNT, EXTENSION_CSS_PROPERTIES, type Easing, type CubicBezierToken, type DimensionValue, type DimensionToken, type DurationToken, type DurationValue, type FontFamilyToken, type NumberToken, type UnparsedToken, type CssAnalysis, type ShadowToken, type ColorSpace, type ColorToken, type ColorValue, type ColorComponent, } from './types.js';
4
+ export type { CssLength, ShadowValue } from './destructure-box-shadow.js';
2
5
  export declare function css_to_tokens(css: string): Tokens;
3
6
  type TokenID = string;
4
7
  export type Tokens = {
@@ -13,4 +16,3 @@ export type Tokens = {
13
16
  easing: Record<TokenID, CubicBezierToken | UnparsedToken>;
14
17
  };
15
18
  export declare function analysis_to_tokens(analysis: CssAnalysis): Tokens;
16
- export {};
package/dist/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { analyze } from '@projectwallace/css-analyzer';
2
- import { DestructuredShadow } from './destructure-box-shadow';
2
+ import { ShadowValue } from './destructure-box-shadow';
3
3
  export type CssAnalysis = ReturnType<typeof analyze>;
4
4
  export declare const EXTENSION_AUTHORED_AS = "com.projectwallace.css-authored-as";
5
5
  export declare const EXTENSION_USAGE_COUNT = "com.projectwallace.usage-count";
@@ -11,7 +11,12 @@ export type BaseToken = {
11
11
  [EXTENSION_USAGE_COUNT]: number;
12
12
  };
13
13
  };
14
- type DurationValue = {
14
+ export type Unit = 'rem' | 'px';
15
+ export type Length = {
16
+ value: number;
17
+ unit: Unit;
18
+ };
19
+ export type DurationValue = {
15
20
  value: number;
16
21
  unit: 'ms';
17
22
  };
@@ -23,7 +28,7 @@ export type UnparsedToken = BaseToken & {
23
28
  $value: string;
24
29
  $type?: never;
25
30
  };
26
- type ColorSpace = string | 'srgb' | 'display-p3' | 'hsl' | 'hwb' | 'lab' | 'lch' | 'oklab' | 'oklch' | 'display-p3' | 'a98-rgb' | 'prophoto-rgb' | 'rec2020' | 'xyz-d65' | 'xyz-d50';
31
+ export type ColorSpace = 'srgb' | 'display-p3' | 'hsl' | 'hwb' | 'lab' | 'lch' | 'oklab' | 'oklch' | 'display-p3' | 'a98-rgb' | 'prophoto-rgb' | 'rec2020' | 'xyz-d65' | 'xyz-d50';
27
32
  export type ColorComponent = number | 'none';
28
33
  export type ColorValue = {
29
34
  colorSpace: ColorSpace;
@@ -40,7 +45,7 @@ export type ColorToken = BaseToken & {
40
45
  };
41
46
  export type DimensionValue = {
42
47
  value: number;
43
- unit: string;
48
+ unit: Unit;
44
49
  };
45
50
  export type DimensionToken = BaseToken & {
46
51
  $type: 'dimension';
@@ -54,12 +59,12 @@ export type CubicBezierToken = BaseToken & {
54
59
  $type: 'cubicBezier';
55
60
  $value: Easing;
56
61
  };
62
+ export type FontFamilyValue = string[];
57
63
  export type FontFamilyToken = BaseToken & {
58
64
  $type: 'fontFamily';
59
- $value: string[];
65
+ $value: FontFamilyValue;
60
66
  };
61
67
  export type ShadowToken = BaseToken & {
62
68
  $type: 'shadow';
63
- $value: DestructuredShadow | DestructuredShadow[];
69
+ $value: ShadowValue | ShadowValue[];
64
70
  };
65
- export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@projectwallace/css-design-tokens",
3
3
  "description": "Generate spec-compliant Design Tokens from CSS.",
4
- "version": "0.8.1",
4
+ "version": "0.10.0",
5
5
  "license": "EUPL-1.2",
6
6
  "author": "Bart Veneman <bart@projectwallace.com>",
7
7
  "repository": {