@koobiq/tokens-builder 3.17.1 → 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.
- package/actions/css-index.js +95 -0
- package/build.js +3 -0
- package/configs/css-new.js +21 -1
- package/configs/css.js +20 -0
- package/package.json +1 -1
- package/transformGroups/css.js +15 -0
- package/transforms/name/custom-kebab.js +30 -11
|
@@ -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('==============================================');
|
package/configs/css-new.js
CHANGED
|
@@ -99,7 +99,27 @@ const newSemanticPaletteConfig = newSemanticPaletteColors.map((color) => ({
|
|
|
99
99
|
|
|
100
100
|
module.exports = {
|
|
101
101
|
css: {
|
|
102
|
-
transformGroup: 'kbq/css',
|
|
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
package/transformGroups/css.js
CHANGED
|
@@ -13,4 +13,19 @@ module.exports = (StyleDictionary) => {
|
|
|
13
13
|
'kbq-attribute/comment'
|
|
14
14
|
]
|
|
15
15
|
});
|
|
16
|
+
|
|
17
|
+
StyleDictionary.registerTransformGroup({
|
|
18
|
+
name: 'kbq/css-new',
|
|
19
|
+
transforms: [
|
|
20
|
+
'attribute/cti',
|
|
21
|
+
'kbq-attribute/palette',
|
|
22
|
+
'kbq-attribute/font',
|
|
23
|
+
'kbq-attribute/light',
|
|
24
|
+
'kbq-attribute/dark',
|
|
25
|
+
'name/custom-kebab-strip-theme',
|
|
26
|
+
'color/css',
|
|
27
|
+
'kbq/prefix',
|
|
28
|
+
'kbq-attribute/comment'
|
|
29
|
+
]
|
|
30
|
+
});
|
|
16
31
|
};
|
|
@@ -1,20 +1,39 @@
|
|
|
1
|
+
const toKebab = (path) => {
|
|
2
|
+
const standardKebab = path
|
|
3
|
+
.map((part) =>
|
|
4
|
+
part
|
|
5
|
+
.replace(/([a-z])([A-Z])/g, '$1-$2')
|
|
6
|
+
.replace(/"/g, '')
|
|
7
|
+
.replace(/[\s.]+/g, '-')
|
|
8
|
+
.toLowerCase()
|
|
9
|
+
)
|
|
10
|
+
.join('-');
|
|
11
|
+
|
|
12
|
+
// for alpha colors, remove the dash between 'a' and the index
|
|
13
|
+
return standardKebab.replace(/-a-(\d+)$/, '-a$1');
|
|
14
|
+
};
|
|
15
|
+
|
|
1
16
|
module.exports = (StyleDictionary) => {
|
|
2
17
|
StyleDictionary.registerTransform({
|
|
3
18
|
name: 'name/custom-kebab',
|
|
4
19
|
type: 'name',
|
|
20
|
+
transformer: (token) => toKebab(token.path)
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// Same as custom-kebab but strips 'light'/'dark' when used as the type
|
|
24
|
+
// segment (e.g. shadow.light.card → shadow-card).
|
|
25
|
+
// Used for the new palette output so theme-scoped shadows don't carry
|
|
26
|
+
// the theme prefix in their variable name.
|
|
27
|
+
StyleDictionary.registerTransform({
|
|
28
|
+
name: 'name/custom-kebab-strip-theme',
|
|
29
|
+
type: 'name',
|
|
5
30
|
transformer: (token) => {
|
|
6
|
-
const
|
|
7
|
-
.
|
|
8
|
-
part
|
|
9
|
-
|
|
10
|
-
.replace(/"/g, '')
|
|
11
|
-
.replace(/[\s.]+/g, '-')
|
|
12
|
-
.toLowerCase()
|
|
13
|
-
)
|
|
14
|
-
.join('-');
|
|
31
|
+
const path =
|
|
32
|
+
token.attributes.category === 'light' || token.attributes.category === 'dark'
|
|
33
|
+
? token.path.filter((part) => part !== 'light' && part !== 'dark')
|
|
34
|
+
: token.path;
|
|
15
35
|
|
|
16
|
-
|
|
17
|
-
return standardKebab.replace(/-a-(\d+)$/, '-a$1');
|
|
36
|
+
return toKebab(path);
|
|
18
37
|
}
|
|
19
38
|
});
|
|
20
39
|
};
|