npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@yidun/vite-plugin-theme-preprocessor

v1.4.10

Published

css theme preprocessor plugin for vite

Downloads

1

Readme

@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

example view

Install

# use pnpm (or npm)
pnpm install color @zougt/vite-plugin-theme-preprocessor @zougt/some-loader-utils -D
# use yarn
yarn add color @zougt/vite-plugin-theme-preprocessor @zougt/some-loader-utils -D

vite.config.js

The config Only for Dynamic theme mode.

import { defineConfig } from "vite";
import {
  themePreprocessorPlugin,
  themePreprocessorHmrPlugin,
} from "@zougt/vite-plugin-theme-preprocessor";
import path from "path";
export default defineConfig({
  plugins: [
    themePreprocessorPlugin({
      less: {
        // Enable Dynamic theme mode.
        arbitraryMode: true,
        // Default theme color,It is usually the same as a theme color (@primary-color) in src/theme/theme-vars.less .
        defaultPrimaryColor: "#512da7",
        // Only one item of multipleScopeVars
        multipleScopeVars: [
          {
            // Any string, required
            scopeName: "theme-default",
            // path or varscontent must be selected
            path: path.resolve("src/theme/theme-vars.less"),
            // varsContent same as content in path
            // varsContent:`@primary-color:${defaultPrimaryColor};`
          },
        ],
        // 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
        includeStyleWithColors: [
          {
            // color can be string or string[], example: ["#ffffff","#000"] or ["transparent","none"].
            color: "#ffffff",
            // Exclude css props, example: not be #ffffff in background.
            // excludeCssProps:["background","background-color"]
            // Exclude css selectors
            // excludeSelectors: [
            //   ".ant-btn-link:hover, .ant-btn-link:focus, .ant-btn-link:active",
            // ],
          },
        ],
      },
      // scss:{

      // },
    }),
    // development need theme HMR
    themePreprocessorHmrPlugin(),
  ],
});

src/theme/theme-vars.less

/*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*/

/*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*/

/*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.*/

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

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

@border-radius-base: 6px;

Switch Theme Online

use @setCustomTheme module

import Color from "color";
// "@setCustomTheme" from themePreprocessorPlugin,Color is essential in setCustomTheme method.
import setCustomTheme from "@setCustomTheme";
setCustomTheme({
  Color,
  primaryColor: "#FF005A",
  //gradientReplacer:{},
  //targetValueReplacer:{}
});

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

# npm run dev
# npx z-theme inspect to see  gradientReplacer and targetValueReplacer
npx z-theme inspect

Preset theme mode

one inline demo

one demo repository

效果图

Install

# use pnpm or npm
pnpm install @zougt/vite-plugin-theme-preprocessor -D
# use yarn
yarn add @zougt/vite-plugin-theme-preprocessor -D

vite.config.js

The config Only for Preset theme mode。

import themePreprocessorPlugin from "@zougt/vite-plugin-theme-preprocessor";
export default {
  plugins: [
    themePreprocessorPlugin({
      scss: {
        // close arbitraryMode
        arbitraryMode: false,
        // Provide multiple  LESS/SCSS variable files
        multipleScopeVars: [
          {
            scopeName: "theme-default",
            // path or varscontent must be selected
            path: path.resolve("src/theme/default-vars.scss"),
            // varsContent same as content in path
            // varsContent:`@primary-color:${defaultPrimaryColor};`
          },
          {
            scopeName: "theme-mauve",
            path: path.resolve("src/theme/mauve-vars.scss"),
          },
        ],
        // 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
        includeStyleWithColors: [
          {
            // color can be string or string[], example: ["#ffffff","#000"] or ["transparent","none"].
            color: "#ffffff",
            // Exclude css props, example: not be #ffffff in background.
            // excludeCssProps:["background","background-color"]
            // Exclude css selectors
            // excludeSelectors: [
            //   ".ant-btn-link:hover, .ant-btn-link:focus, .ant-btn-link:active",
            // ],
          },
        ],
        // add scopeName to html tag className. default use multipleScopeVars[0].scopeName
        defaultScopeName: "",
        //  extract independent theme CSS files in production mode  extract为true以下属性有效
        extract: true,
        // theme CSS files output dir , default use viteConfig.build.assetsDir
        outputDir: "",
        // link tag id 
        themeLinkTagId: "theme-link-tag",
        // "head"||"head-prepend" || "body" ||"body-prepend"
        themeLinkTagInjectTo: "head",
        // Remove scopeName in the extracted CSS content.
        removeCssScopeName: false,
        // custom css file name.
        customThemeCssFileName: (scopeName) => scopeName,
      },
      // less: {
      //   multipleScopeVars: [
      //     {
      //       scopeName: "theme-default",
      //       path: path.resolve("src/theme/default-vars.less"),
      //     },
      //     {
      //       scopeName: "theme-mauve",
      //       path: path.resolve("src/theme/mauve-vars.less"),
      //     },
      //   ],
      // },
    }),
  ],
};

Switch Theme Online

What needs to be done:

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

for this, has toggleTheme method :

import { toggleTheme } from "@zougt/vite-plugin-theme-preprocessor/dist/browser-utils";

toggleTheme({
  scopeName: "theme-default",
});