Vite 中文文档 Vite 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • vite

    • 指引
    • 为什么选 Vite
    • 开始
    • 功能
    • 命令行界面
    • 使用插件
    • 依赖预构建
    • 静态资源处理
    • 构建生产版本
    • 部署静态站点
    • 环境变量和模式
    • 服务端渲染
    • 后端集成
    • 与其他工具比较
    • 故障排除
    • 从 v3 迁移
  • API

  • 配置参考

  • vite

  • API

  • 配置参考

@zougt/vite-plugin-theme-preprocessor


中文文档

A vite v2.0+ plugin, allows you to easily implementing dynamic themes based on Less or Sass.

Easy to use.
Unlimited UI framework, Element-ui、Iview、Ant-design and others (based Less/Sass).
Can not use css3 vars.
Good browser compatibility (IE9+ ?,but Vite lowest polyfill to IE11, You also can use the webpack plugin @zougt/some-loader-utils ).

Demo repositories

Dynamic theme mode


v1.4.0 + supported


Any theme color can be selected from the color palette. Here, take LESS as an example, which is also applicable to SCSS.

One inline demo

One demo repository

Install


  1. ``` shell
  2. # use pnpm (or npm)
  3. pnpm install color @zougt/vite-plugin-theme-preprocessor @zougt/some-loader-utils -D
  4. # use yarn
  5. yarn add color @zougt/vite-plugin-theme-preprocessor @zougt/some-loader-utils -D
  6. ```

vite.config.js

The config Only for Dynamic theme mode.


  1. ``` js
  2. import { defineConfig } from "vite";
  3. import {
  4.   themePreprocessorPlugin,
  5.   themePreprocessorHmrPlugin,
  6. } from "@zougt/vite-plugin-theme-preprocessor";
  7. import path from "path";
  8. export default defineConfig({
  9.   plugins: [
  10.     themePreprocessorPlugin({
  11.       less: {
  12.         // Enable Dynamic theme mode.
  13.         arbitraryMode: true,
  14.         // Default theme color,It is usually the same as a theme color (@primary-color) in src/theme/theme-vars.less .
  15.         defaultPrimaryColor: "#512da7",
  16.         // Only one item of multipleScopeVars
  17.         multipleScopeVars: [
  18.           {
  19.             // Any string, required
  20.             scopeName: "theme-default",
  21.             // path or varscontent must be selected
  22.             path: path.resolve("src/theme/theme-vars.less"),
  23.             // varsContent same as content in path
  24.             // varsContent:`@primary-color:${defaultPrimaryColor};`
  25.           },
  26.         ],
  27.         // The color in CSS is not generated by the theme color variable, and it can also be extracted into the theme CSS to improve the weight
  28.         includeStyleWithColors: [
  29.           {
  30.             // color can be string or string[], example: ["#ffffff","#000"] or ["transparent","none"].
  31.             color: "#ffffff",
  32.             // Exclude css props, example: not be #ffffff in background.
  33.             // excludeCssProps:["background","background-color"]
  34.             // Exclude css selectors
  35.             // excludeSelectors: [
  36.             //   ".ant-btn-link:hover, .ant-btn-link:focus, .ant-btn-link:active",
  37.             // ],
  38.           },
  39.         ],
  40.       },
  41.       // scss:{

  42.       // },
  43.     }),
  44.     // development need theme HMR
  45.     themePreprocessorHmrPlugin(),
  46.   ],
  47. });
  48. ```

src/theme/theme-vars.less

  1. ``` css
  2. /*Note: This file should not be @import in other .less, The variables in this file are not used to set the theme of the project (of course, you can use it as the default theme during loading). The main function is that as long as the variable value here is different from the original variable value of the project, CSS that changes with the theme color gradient will be extracted after compilation*/

  3. /*Important: Once the content of this file is fixed, it does not need to be changed. You can dynamically switch topics online use setCustomTheme method*/

  4. /*Emphasis: the change of variable value will affect the changes of gradientReplacer and targetValueReplacer available attributes of setCustomTheme method, so once the content is fixed, it does not need to be changed.*/

  5. /*A theme color, same as defaultPrimaryColor of themePreprocessorPlugin, inline switch use setCustomTheme({primaryColor})*/
  6. @primary-color: #512da7;

  7. /*The style corresponding to this color will also change with the main color by default, inline switch  can use setCustomTheme({gradientReplacer:"#F7D06B"}}) */
  8. @alert-success-bg-color: #F7D06B;

  9. @border-radius-base: 6px;
  10. ```

Switch Theme Online

use @setCustomTheme module

  1. ``` js
  2. import Color from "color";
  3. // "@setCustomTheme" from themePreprocessorPlugin,Color is essential in setCustomTheme method.
  4. import setCustomTheme from "@setCustomTheme";
  5. setCustomTheme({
  6.   Color,
  7.   primaryColor: "#FF005A",
  8.   //gradientReplacer:{},
  9.   //targetValueReplacer:{}
  10. });
  11. ```

The available attributes of gradientReplacer and targetValueReplacer follow Less/SCSS content changes.

  1. ``` shell
  2. # npm run dev
  3. # npx z-theme inspect to see  gradientReplacer and targetValueReplacer
  4. npx z-theme inspect
  5. ```

Preset theme mode


one inline demo

one demo repository

Install


  1. ``` shell
  2. # use pnpm or npm
  3. pnpm install @zougt/vite-plugin-theme-preprocessor -D
  4. # use yarn
  5. yarn add @zougt/vite-plugin-theme-preprocessor -D
  6. ```

vite.config.js

The config Only for Preset theme mode。


  1. ``` js
  2. import themePreprocessorPlugin from "@zougt/vite-plugin-theme-preprocessor";
  3. export default {
  4.   plugins: [
  5.     themePreprocessorPlugin({
  6.       scss: {
  7.         // close arbitraryMode
  8.         arbitraryMode: false,
  9.         // Provide multiple  LESS/SCSS variable files
  10.         multipleScopeVars: [
  11.           {
  12.             scopeName: "theme-default",
  13.             // path or varscontent must be selected
  14.             path: path.resolve("src/theme/default-vars.scss"),
  15.             // varsContent same as content in path
  16.             // varsContent:`@primary-color:${defaultPrimaryColor};`
  17.           },
  18.           {
  19.             scopeName: "theme-mauve",
  20.             path: path.resolve("src/theme/mauve-vars.scss"),
  21.           },
  22.         ],
  23.         // The color in CSS is not generated by the theme color variable, and it can also be extracted into the theme CSS to improve the weight
  24.         includeStyleWithColors: [
  25.           {
  26.             // color can be string or string[], example: ["#ffffff","#000"] or ["transparent","none"].
  27.             color: "#ffffff",
  28.             // Exclude css props, example: not be #ffffff in background.
  29.             // excludeCssProps:["background","background-color"]
  30.             // Exclude css selectors
  31.             // excludeSelectors: [
  32.             //   ".ant-btn-link:hover, .ant-btn-link:focus, .ant-btn-link:active",
  33.             // ],
  34.           },
  35.         ],
  36.         // add scopeName to html tag className. default use multipleScopeVars[0].scopeName
  37.         defaultScopeName: "",
  38.         //  extract independent theme CSS files in production mode  extract为true以下属性有效
  39.         extract: true,
  40.         // theme CSS files output dir , default use viteConfig.build.assetsDir
  41.         outputDir: "",
  42.         // link tag id
  43.         themeLinkTagId: "theme-link-tag",
  44.         // "head"||"head-prepend" || "body" ||"body-prepend"
  45.         themeLinkTagInjectTo: "head",
  46.         // Remove scopeName in the extracted CSS content.
  47.         removeCssScopeName: false,
  48.         // custom css file name.
  49.         customThemeCssFileName: (scopeName) => scopeName,
  50.       },
  51.       // less: {
  52.       //   multipleScopeVars: [
  53.       //     {
  54.       //       scopeName: "theme-default",
  55.       //       path: path.resolve("src/theme/default-vars.less"),
  56.       //     },
  57.       //     {
  58.       //       scopeName: "theme-mauve",
  59.       //       path: path.resolve("src/theme/mauve-vars.less"),
  60.       //     },
  61.       //   ],
  62.       // },
  63.     }),
  64.   ],
  65. };
  66. ```

Switch Theme Online

What needs to be done:

add scopeName as clasName to Html tag , remove prev scopeName from html tag.
in production , if extract , need toggle the link href.

for this, has toggleTheme method :

  1. ``` js
  2. import { toggleTheme } from "@zougt/vite-plugin-theme-preprocessor/dist/browser-utils";

  3. toggleTheme({
  4.   scopeName: "theme-default",
  5. });
  6. ```
Last Updated: 2023-05-23 11:11:51