This is a vite plugin for make and adding css (and preprocessors) variable from json file.
This plugin depends on vite's virtual module system and
css.preprocessorOptions[extension].additionalData.
via npm
npm i vite-plugin-css-json-var
plugin object is exported as default.
import jsonConfigPlugin from "vite-plugin-css-json-var"
exsample:
{
"color": {
"primary": "#ff0000",
"secondary": "#ffffff"
}
}
this code will be transformed below
:root{
--color-primary: #ff0000;
--color-secondary: #ffffff;
}
add plugin option in your vite.config.js
.
import jsonConfigPlugin from "vite-plugin-css-json-var"
export default {
plugin: [
jsonConfigPlugin({
file: "./site-setting.json",
lang: "css",
style: "css",
})
]
}
If you choosed "css" option in style field, you should import virtual css file in entry file.
import "virtual:css-vars.css"
import "main.css"
all options are required.
pass or laod setting json file.
type: JSON | string
exsample:
jsonConfigPlugin({
//path to your setting json file.
file: "./site.config.json",
})
// Or, you can pass setting file directly.
import setting_json from "./site-config.json"
jsonConfigPlugin({
file: setting_json,
})
choose your using style language.
type: "css" | "sass" | "scss" | "less" | "stylus"
choose output style which you need.
- CSS custom properties
- Preprocessor's variable style
type: "css" | "preprocessor"
Note
If you choose "css" in lang option, you can select only "css" option.
"preprocessor" option, the plugin will automatically convert to the variable style of the lang
option.
MIT