Provide env of Vite variables in scss files
npm i vite-plugin-scss-env-vars -D
- use plugin
import ScssEnvVars from 'vite-plugin-scss-env-vars';
export default defineConfig(() => {
return {
// ...other configurations
plugins: [
ScssEnvVars()
],
// ...other configurations
}
});
- define vars in
.env
files
VITE_FILE='https://xxx.xxx.com/images'
VITE_COLOR_PRIMARY='#fb0000'
- use vite env in scss
.App {
background-image: url('#{$VITE_FILE}/home.jpg');
color: $VITE_COLOR_PRIMARY;
}
Filter the file to be processed
- type:
RegExp | ((id: string) => boolean)
- default:
/\.scss$/
If you don't want to use the original name of the VITE environment variable, you can convert the name by this callback function
- type:
(key: string) => string;
- default: use original name