P粉2121146612023-08-27 00:20:24
Tailwind configurations are CommonJS files (not modules), so you can't use ES6 features like import
You can install a package named dotenv
npm i dotenv
Needs to be placed above the tailwind configuration file, for example
require('dotenv').config()
module.exports = {/** */}
Create color variables in .env
. Note that since we require it to be outside the scope of Vite, it may not be prefixed with VITE_
ANY_COLOR='#ffc8dd'
Now you can access it in the tailwind configuration
theme: {
colors: {
primary: process.env.ANY_COLOR
}
}
This will work but if you change the colors in the .env
file you will need to rebuild the styles again. If it works for you (one deployment - one color anyway) - great. I personally would suggest another solution based on CSS variables - CanIUse link
Define variables in a CSS file or create style
tags inside
tags in
index.html
:root {
--theme-color: #ffc8dd;
}
and in configuration
theme: {
colors: {
primary: 'var(--theme-color)'
}
}
That's it - no extra packages, extra work, if you change a CSS variable, the changes will be applied instantly - even in production because we use CSS features