-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
107 lines (97 loc) · 2.86 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const { default: getHex } = require('./utils/getHex');
const StyleDictionary = require('style-dictionary').extend({
source: ['tokens/*.json'],
// transform: {
// // Now we can use the transform 'myTransform' below
// myTransform: {
// type: 'name',
// transformer: (token) => token.path.join('_').toUpperCase(),
// },
// },
// format: {
// myFormat: ({ dictionary, platform }) => {
// console.log(dictionary);
// return dictionary.allTokens
// .map((token) => `${token.name}: ${token.value}`)
// .join('\n');
// },
// },
platforms: {
// test: {
// transform: ['myTransform'],
// transformGroup: 'js',
// buildPath: 'build/json/',
// files: [
// {
// destination: 'myform.json',
// format: 'myFormat',
// },
// ],
// },
'json-flat': {
transforms: ['color/hex'],
transformGroup: 'js',
buildPath: 'build/json/',
files: [
// {
// destination: 'styles.json',
// format: 'json/flat', // 디렉토리 구조 flat
// },
{
destination: 'clo-set.json',
format: 'json/clo-set',
},
// {
// destination: 'test.json',
// format: 'json/test', // 디렉토리 구조 유지
// },
],
},
// 'javascript-object': {
// transforms: ['color/hex'],
// transformGroup: 'js',
// buildPath: 'build/js/',
// files: [
// {
// destination: 'js-object.js',
// format: 'javascript/object',
// },
// ],
// },
},
});
const GLOBAL = 'global';
StyleDictionary.registerFormat({
name: 'json/clo-set',
formatter: function (dictionary, config) {
const allTokens = dictionary.allProperties;
const aliasTokens = allTokens.filter(
(item) => !item.path[0].includes(GLOBAL)
);
const globalTokens = allTokens
.filter((item) => item.path[0].includes(GLOBAL))
.map((item) => {
item.path.shift();
return item;
});
console.log(aliasTokens);
const preprocessedToken = {};
const globalTokenMap = {};
for (let i = 0; i < globalTokens.length; i++) {
const { path, value } = globalTokens[i];
const key = path.join('_').replace(/[- ]/g, '_').toUpperCase();
const hexValue = getHex(value);
preprocessedToken[key] = hexValue;
globalTokenMap[hexValue] = key;
}
for (let i = 0; i < aliasTokens.length; i++) {
const { path, value } = aliasTokens[i];
const key = path.join('_').replace(/[- ]/g, '_').toUpperCase();
const hexValue = getHex(value);
const globalMappedKey = globalTokenMap[hexValue];
preprocessedToken[key] = globalMappedKey ? `{${globalMappedKey}}` : value;
}
return JSON.stringify(preprocessedToken);
},
});
StyleDictionary.buildAllPlatforms();