-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.ts
135 lines (114 loc) · 3.87 KB
/
build.ts
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { FormatterArguments } from 'style-dictionary/types/Format';
import { config } from './config';
import StyleDictionaryBase, { TransformedToken } from 'style-dictionary';
import * as fs from 'fs';
const StyleDictionary = StyleDictionaryBase.extend(config);
const fileHeader = StyleDictionary.formatHelpers.fileHeader;
console.log('Build started...');
console.log('\n==============================================');
StyleDictionary.registerFormat({
formatter: ({ file, dictionary, options }: FormatterArguments) => {
const symbols = dictionary.allProperties.map(cssTemplate).join('') + '\n';
const composedVars = fs
.readFileSync('./composed-variables/composed-variables.css', 'utf-8')
.match(/\/\* EXTRACTING_CSS_VARS_START \*\/([\S\s]*)\/\* EXTRACTING_CSS_VARS_END \*\//m)![1];
return (
fileHeader({ file }) +
`${options.selector ?? ':root'} {\n${symbols}\n /* Composed variables */\n\n ${composedVars.trim()}\n}\n`
);
},
name: 'css/variables',
});
StyleDictionary.registerFormat({
formatter: (args: FormatterArguments) =>
args.dictionary.allProperties.map(scssTemplate).join('') + '\n',
name: 'custom/format/scss',
});
StyleDictionary.registerFormat({
formatter: (args: FormatterArguments) => {
const symbols = args.dictionary.allProperties.map(commonjsTemplate).join('');
return `module.exports = {\n${symbols}};\n`;
},
name: 'custom/format/javascript/module',
});
StyleDictionary.registerFormat({
formatter: ({ dictionary }) => {
const { allTokens } = dictionary;
allTokens.forEach((token) => {
// if a token uses a reference token, we add the original token object
const usesReference = dictionary.usesReference(token);
if (usesReference) {
const ref = dictionary.getReferences(token.original.value);
token.refOriginal = ref;
}
});
const fileContents = {
tokens: allTokens,
};
return JSON.stringify(fileContents, null, 2);
},
name: 'json/extended',
});
// FINALLY, BUILD ALL THE PLATFORMS
StyleDictionary.buildAllPlatforms();
console.log('\n==============================================');
console.log('\nBuild completed!');
function cssTemplate(token: TransformedToken) {
let output = '';
if (token.deprecated || token.comment || token.attributes?.category === 'size') {
output += ' /**\n';
if (token.deprecated) {
output += ` * @deprecated ${token.name} is deprecated`;
if (token.deprecated_comment) {
output += ` (${token.deprecated_comment})`;
}
output += '\n';
}
if (token.comment) {
output += ` * ${token.comment}\n`;
}
if (token.attributes?.category === 'size') {
output += ` * Original Value: ${token.original.value}px\n`;
}
output += ' */\n';
}
output += ` --${token.name}: ${token.value};\n`;
return output;
}
function scssTemplate(token: TransformedToken) {
let output = '';
if (token.deprecated) {
output += `/// @deprecated Notice: ${token.name} is deprecated`;
if (token.deprecated_comment) {
output += ` (${token.deprecated_comment})`;
}
output += '\n';
}
if (token.comment) {
output += `/// ${token.comment}\n`;
}
if (token.attributes?.category === 'size') {
output += `/// Original Value: ${token.original.value}px\n`;
}
output += `$${token.name}: ${token.value};\n`;
return output;
}
function commonjsTemplate(token: TransformedToken) {
let output = '';
if (token.deprecated || token.comment) {
output += ' /**\n';
if (token.deprecated) {
output += ` * @deprecated ${token.name} is deprecated`;
if (token.deprecated_comment) {
output += ` (${token.deprecated_comment})`;
}
output += '\n';
}
if (token.comment) {
output += ` * ${token.comment}\n`;
}
output += ' */\n';
}
output += ` ${token.name}: '${token.value}',\n`;
return output;
}