-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
225 lines (165 loc) · 5.62 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { theme } from 'tailwindcss/stubs/config.full'
import twColors from 'tailwindcss/lib/public/colors'
const varsMap = new Map()
const configProps = [
['blur', 'blur', 'blur'],
['borderRadius', 'radius', 'border-radius'],
['borderWidth', 'border', 'border-width'],
['boxShadow', 'shadow', 'box-shadow'],
['dropShadow', 'drop-shadow', 'drop-shadow'],
['fontWeight', 'font', 'font-weight'],
['fontFamily', 'family', 'font-family'],
['fontSize', 'text', 'font-size'],
['letterSpacing', 'tracking', 'letter-spacing'],
['lineHeight', 'leading', 'line-height'],
['opacity', 'opacity', 'opacity'],
['maxWidth', 'width', 'width'],
['screens', 'screen', 'screen'],
['spacing', 'size', 'size'],
['transitionTimingFunction', 'easing', 'easing'],
['zIndex', 'z', 'z-index'],
]
// Default Config
for (const prop of configProps) {
const [srcProp, cssPrefix, fileName] = prop
const currEntries = Object.entries(getThemeProp(theme[srcProp]))
const currVars = []
for (const [variant, _value] of currEntries) {
if (variant.toLowerCase() !== 'default') {
const value = joinOrGetValue(_value)
if (value) {
currVars.push({
[`--${cssPrefix}-${normalize(variant)}`]: value,
})
}
}
}
varsMap.set(fileName, currVars)
}
// Colors
const allColors = []
const noColors = ['inherit', 'transparent', 'current', 'black', 'white']
const deprecatedColors = ['lightBlue', 'trueGray', 'coolGray', 'blueGray', 'warmGray']
const colorEntries = Object.entries(twColors).filter(
([colorName]) => ![...deprecatedColors, ...noColors].includes(colorName)
)
for (const [_colorName, variantColors] of colorEntries) {
const currColors = []
const colorName = normalize(_colorName) // Used also as file name
const currVariants = Object.entries(variantColors)
if (isColorArr(currVariants)) {
for (const [numericVariant, value] of currVariants) {
const cssVar = {
[`--${colorName}-${numericVariant}`]: value,
}
currColors.push(cssVar)
allColors.push(cssVar)
}
varsMap.set(colorName, currColors)
}
}
// Write JS / JSON
const outEntries = Array.from(varsMap.entries()) // [['fileName', [{ '--name', value }, ... ]]]
const allVarsArr = flatDeep(Array.from(varsMap.values())) // [{ '--name': value }, ... ]
const outObj = {}
let outDts = ''
for (const cssVar of allVarsArr) {
const [varName, varValue] = flatDeep(Object.entries(cssVar))
outObj[varName] = varValue
outDts = outDts + `'${varName}': string,`
}
const outJson = JSON.stringify(outObj)
await Bun.write('./dist/index.mjs', `export const twVariables = ${outJson}`)
await Bun.write('./dist/index.js', `module.exports = ${outJson}`)
await Bun.write('./dist/variables.json', outJson)
// Write d.ts
const types = `export declare const twVariables: TwVariables; export type TwVariables = { ${outDts} }`
await Bun.write('./dist/index.d.ts', types)
// Write all vars in a single CSS file in different root blocks
let rootBlocks = ''
for (const [, cssVars] of outEntries) {
rootBlocks += getCSS(cssVars)
}
await Bun.write('./dist/variables.css', rootBlocks)
// Write all colors in a single CSS file
await Bun.write('./dist/colors.css', getCSS(allColors))
// Write separated CSS files
for await (const [fileName, cssVars] of outEntries) {
await Bun.write(`./dist/${fileName}.css`, getCSS(cssVars))
}
// Write preflight CSS file
const pfText = await Bun.file('preflight.css').text()
await Bun.write('./dist/preflight.css', pfText.replace(/[\s\n]+/g, '')) /* Minify */
// Package.json
const pkgText = await Bun.file('./package.json').text()
await Bun.write('./package-bk.json', pkgText)
const pkgObj = JSON.parse(pkgText)
pkgObj.exports = {}
// --- JS / JSON
pkgObj.exports['.'] = { import: './dist/index.mjs', require: './dist/index.js' }
pkgObj.exports['./variables.json'] = './dist/variables.json'
// --- Unified CSS files
pkgObj.exports['./preflight.css'] = './dist/preflight.css'
pkgObj.exports['./variables.css'] = './dist/variables.css'
pkgObj.exports['./colors.css'] = './dist/colors.css'
// --- Separated CSS files
for (const [fileName] of outEntries) {
pkgObj.exports[`./${fileName}.css`] = `./dist/${fileName}.css`
}
await Bun.write('./package.json', JSON.stringify(pkgObj, undefined, 3))
// Value getters
function getThemeProp(prop) {
if (typeof prop === 'function') {
return prop({
theme: () => {},
breakpoints: () => {},
})
}
if (isObj(prop)) {
return prop
}
return {}
}
function joinOrGetValue(values) {
if (Array.isArray(values)) {
let joined = ''
for (const value of values) {
if (isString(value)) {
joined += value + ','
}
}
return joined.slice(0, -1)
}
if (isString(values)) {
return values
}
return ''
}
// Write Utils
function getCSS(cssVars) {
let strVars = ''
for (const cssVar of cssVars) {
const [varName, varValue] = flatDeep(Object.entries(cssVar))
strVars += `${varName}: ${varValue};`
}
return `:root { ${strVars} }`
}
// Misc
function normalize(value) {
return camelToKebab(value).toLowerCase().replaceAll('.', '')
}
function camelToKebab(string) {
return string.replace(/[A-Z]/g, (match) => '-' + match)
}
function isColorArr(array) {
return Array.isArray(array) && array.every((item) => Array.isArray(item) && item.every(isString))
}
function isString(value) {
return typeof value === 'string'
}
function isObj(value) {
return typeof value === 'object' && value != null && !Array.isArray(value)
}
function flatDeep(array) {
return array.flat(1 / 0)
}