@koobiq/tokens-builder 3.17.2 → 3.18.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.
@@ -0,0 +1,95 @@
1
+ const fs = require('fs');
2
+ const path = require('node:path');
3
+
4
+ const HEADER = '/**\n * Do not edit directly\n */\n';
5
+
6
+ /**
7
+ * Generates a pair of consumer entry points for a CSS platform:
8
+ *
9
+ * index.css — thin file that `@import`s every curated token file
10
+ * index.bundled.css — the same files concatenated into a single stylesheet
11
+ *
12
+ * The list of files to include (and where the index is written) is read from the
13
+ * platform's `index` config so each track can curate its own aggregate set:
14
+ *
15
+ * css: {
16
+ * transformGroup: 'kbq/css',
17
+ * actions: ['kbq/css-index'],
18
+ * index: {
19
+ * dir: 'css', // where index files are written (relative to buildPath)
20
+ * files: ['css/font.css', …] // token files to include (relative to buildPath)
21
+ * },
22
+ * files: [ … ]
23
+ * }
24
+ */
25
+ module.exports = (StyleDictionary) => {
26
+ StyleDictionary.registerAction({
27
+ name: 'kbq/css-index',
28
+ do: (_dictionary, config) => {
29
+ if (!config.index) {
30
+ return;
31
+ }
32
+
33
+ const { dir = '', files } = config.index;
34
+ const buildPath = config.buildPath;
35
+ const indexDir = path.join(buildPath, dir);
36
+
37
+ const imports = files
38
+ .map((dest) => path.relative(indexDir, path.join(buildPath, dest)).split(path.sep).join('/'))
39
+ .map((rel) => `@import '${rel.startsWith('../') ? rel : `./${rel}`}';`)
40
+ .join('\n');
41
+
42
+ fs.writeFileSync(path.join(indexDir, 'index.css'), `${HEADER}\n${imports}\n`);
43
+
44
+ // Merge all declarations into one block per selector, so `:root` / `.kbq-light` /
45
+ // `.kbq-dark` aren't repeated. On duplicate properties the last one wins, matching
46
+ // the `@import` order in index.css.
47
+ const selectors = [];
48
+ const declsBySelector = new Map();
49
+
50
+ for (const dest of files) {
51
+ const content = fs.readFileSync(path.join(buildPath, dest), 'utf8');
52
+ // A fresh regex per file keeps its `lastIndex` from carrying over between files.
53
+ const blockRegex = /([^{}]+)\{([^}]*)\}/g;
54
+ let match;
55
+
56
+ while ((match = blockRegex.exec(content)) !== null) {
57
+ const selector = match[1].trim();
58
+
59
+ if (!declsBySelector.has(selector)) {
60
+ selectors.push(selector);
61
+ declsBySelector.set(selector, new Map());
62
+ }
63
+
64
+ const decls = declsBySelector.get(selector);
65
+
66
+ for (const rawLine of match[2].split('\n')) {
67
+ const line = rawLine.trim();
68
+
69
+ if (!line) {
70
+ continue;
71
+ }
72
+
73
+ const property = line.match(/^(--[\w-]+)\s*:/);
74
+ // Re-insert so the last declaration wins and keeps its position.
75
+ const key = property ? property[1] : `@line-${decls.size}`;
76
+
77
+ decls.delete(key);
78
+ decls.set(key, line);
79
+ }
80
+ }
81
+ }
82
+
83
+ const bundled = selectors
84
+ .map((selector) => {
85
+ const body = [...declsBySelector.get(selector).values()].map((line) => ` ${line}`).join('\n');
86
+
87
+ return `${selector} {\n${body}\n}`;
88
+ })
89
+ .join('\n\n');
90
+
91
+ fs.writeFileSync(path.join(indexDir, 'index.bundled.css'), `${HEADER}\n${bundled}\n`);
92
+ },
93
+ undo: () => {}
94
+ });
95
+ };
package/build.js CHANGED
@@ -29,6 +29,9 @@ require('./formats/typography')(StyleDictionary);
29
29
  require('./formats/palette')(StyleDictionary);
30
30
  require('./formats/variables')(StyleDictionary);
31
31
 
32
+ // ==== Include custom actions ====
33
+ require('./actions/css-index')(StyleDictionary);
34
+
32
35
  // ==== Run build ====
33
36
  console.log('Build started...');
34
37
  console.log('==============================================');
@@ -100,6 +100,26 @@ const newSemanticPaletteConfig = newSemanticPaletteColors.map((color) => ({
100
100
  module.exports = {
101
101
  css: {
102
102
  transformGroup: 'kbq/css-new',
103
+ actions: ['kbq/css-index'],
104
+ // OKLch entry point. Font/size/typography are color-agnostic and identical across tracks,
105
+ // so they are pulled from the sibling web/css track (../css) instead of being duplicated
106
+ // here. Per-color palette/semantic-palette splits are omitted since their variables are
107
+ // already covered by the aggregate palette.css / semantic-palette.css.
108
+ index: {
109
+ dir: '',
110
+ files: [
111
+ '../css/font.css',
112
+ '../css/size.css',
113
+ '../css/typography.css',
114
+ '../css/md-typography.css',
115
+ 'palette.css',
116
+ 'semantic-palette.css',
117
+ 'light/semantic-colors.css',
118
+ 'light/shadows.css',
119
+ 'dark/semantic-colors.css',
120
+ 'dark/shadows.css'
121
+ ]
122
+ },
103
123
  files: [
104
124
  ...newSemanticPaletteConfig,
105
125
  ...newPaletteByColorsConfig,
package/configs/css.js CHANGED
@@ -68,6 +68,26 @@ const semanticPaletteConfig = semanticPaletteColors.map((color) => ({
68
68
  module.exports = {
69
69
  css: {
70
70
  transformGroup: 'kbq/css',
71
+ actions: ['kbq/css-index'],
72
+ // Curated aggregate token files consumers can pull in via a single entry point.
73
+ // Per-color palette splits, root css-tokens*.css and deprecated component tokens are
74
+ // intentionally excluded to avoid duplicate variable declarations.
75
+ index: {
76
+ dir: 'css',
77
+ files: [
78
+ 'css/font.css',
79
+ 'css/size.css',
80
+ 'css/typography.css',
81
+ 'css/md-typography.css',
82
+ 'css/palette.css',
83
+ 'css/light/semantic-palette.css',
84
+ 'css/light/semantic-colors.css',
85
+ 'css/light/shadows.css',
86
+ 'css/dark/semantic-palette.css',
87
+ 'css/dark/semantic-colors.css',
88
+ 'css/dark/shadows.css'
89
+ ]
90
+ },
71
91
  files: [
72
92
  ...semanticPaletteConfig,
73
93
  {
package/package.json CHANGED
@@ -19,5 +19,5 @@
19
19
  "default": "./*"
20
20
  }
21
21
  },
22
- "version": "3.17.2"
22
+ "version": "3.18.0"
23
23
  }