2022-04-17 12:46:06 +08:00
|
|
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
|
|
const { colord } = require('colord')
|
|
|
|
const colors = require('tailwindcss/colors')
|
|
|
|
|
2022-05-01 19:53:25 +08:00
|
|
|
const replaceBrandColorWithCSSVar = () => {
|
2022-04-17 12:46:06 +08:00
|
|
|
const blues = Object.entries(colors.blue).map(([key, value]) => {
|
2022-05-29 17:53:27 +08:00
|
|
|
const rgb = colord(value).toHsl()
|
|
|
|
const hsl = colord(value).toHsl()
|
2022-04-17 12:46:06 +08:00
|
|
|
return {
|
|
|
|
key,
|
2022-05-29 17:53:27 +08:00
|
|
|
hsl: `${hsl.h} ${hsl.s} ${hsl.l}`,
|
|
|
|
rgb: `${rgb.r} ${rgb.g} ${rgb.b}`,
|
2022-04-17 12:46:06 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return {
|
2022-05-01 19:53:25 +08:00
|
|
|
postcssPlugin: 'replaceBrandColorWithCSSVar',
|
2022-04-17 12:46:06 +08:00
|
|
|
Declaration(decl) {
|
|
|
|
let value = decl.value
|
|
|
|
blues.forEach(blue => {
|
2022-04-29 19:16:34 +08:00
|
|
|
if (decl?.parent?.selector?.includes('-blue-')) {
|
|
|
|
return
|
|
|
|
}
|
2023-03-26 02:16:01 +08:00
|
|
|
value = value.replace(`rgb(${blue.rgb}`, `hsl(var(--brand-color-${blue.key})`)
|
2022-04-17 12:46:06 +08:00
|
|
|
})
|
|
|
|
// if (decl.value !== value) {
|
|
|
|
// console.log({
|
|
|
|
// before: decl.value,
|
|
|
|
// after: value,
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
decl.value = value
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-05-01 19:53:25 +08:00
|
|
|
replaceBrandColorWithCSSVar.postcss = true
|
2022-04-17 12:46:06 +08:00
|
|
|
|
2022-03-13 14:40:38 +08:00
|
|
|
module.exports = {
|
2022-04-17 12:46:06 +08:00
|
|
|
plugins: [
|
|
|
|
require('tailwindcss'),
|
|
|
|
require('autoprefixer'),
|
2022-05-13 23:33:11 +08:00
|
|
|
// replaceBrandColorWithCSSVar,
|
2022-04-17 12:46:06 +08:00
|
|
|
],
|
2022-03-13 14:40:38 +08:00
|
|
|
}
|