Socket
Socket
Sign inDemoInstall

vite-plugin-color-replace

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vite-plugin-color-replace

- 替换css文件中的颜色 或者 自定替换css 的属性;搭配 css变量可实现动态切换主题色


Version published
Maintainers
1
Created
Source

vite-plugin-replace-color

简介

  • 替换css文件中的颜色 或者 自定替换css 的属性;搭配 css变量可实现动态切换主题色

安装

node version: >=16.0.0

vite version: >=3.0.0

npm i vite-plugin-replace-color -D
# or
yarn add vite-plugin-replace-color -D
# or
pnpm install vite-plugin-replace-color -D

使用

  • vite.config.ts 中的配置插件
import replaceColorPlugin from 'vite-plugin-replace-color'
import path from 'path'
export default () => {
  return {
    plugins: [
      replaceColorPlugin({
        colorVariables: {
          //  定义 rgb 时 会替换 rgb() 里面的 内容 透明度不会修改
          // color: #fff  --> color: var(--c5-white)
          // backgroud: rgb(255,255,255,0.2) --->  backgroud:rgb(var(--c5-white-rgb)/0.2)
          "#fff": { hex: "var(--c5-white)", rgb: "var(--c5-white-rgb)" },
          "#2563f4": { hex: "var(--c5-color)", rgb: "var(--c5-rgb)" },
          "#409eff": "var(--c5-color)",
        },
        styleId: "_VITE_REPLACE_COLOR_", // 注入自定义css 内容 时  style标签名
        includes: ["src/**/App.vue*"], // vue文件编译处理后 样式文件路径可能并不是 .vue 结尾
        exclude: ["node_modules/**"], // 忽略 依赖包
      }),
    ],
  }
}

示例

.color {
  color: #2563f4;
  background-color: rgb(#2563f4, 0.4); // 有透明度 会从 rgb 属性中去匹配
  box-shadow: 0 0 1px rgb(37 99 244);
  border-color: #2563f466; // 8位带颜色的 也会从 rgb 属性中去匹配
}
  • 颜色替换结果
.color {
  color: var(--c5-color);
  background-color: rgb(var(--c5-rgb)/0.4);
  border-color: rgb(var(--c5-rgb)/0.4);
  box-shadow: 0 0 1px var(--c5-color);
}

参数说明

参数类型默认值/参数必填说明
colorVariablesRecord<string, string | { hex?: string; rgb?: string }>-声明替换的颜色
styleIdstring_VITE_REPLACE_COLOR_注入自定义css 内容 时 style标签名
customerReplaceVariable(decl) => stringdecl(https://postcss.org/api/#declaration)自定义替换css(返回''则删除该条属性)
includesArray<string|RegExp>|string|RegExp-匹配文件规则
excludeArray<string|RegExp>|string|RegExp-忽略文件规则

辅助功能

  • injectStyle 创建style标签 写入一些自定义css 到页面中 内容为空则删除 style标签
import { injectStyle } from "vite-plugin-replace-theme/es/client";
injectStyle(`:root { --c5-color: #2563F4; --c5-rgb: 37 99 244;}`)

Keywords

FAQs

Package last updated on 18 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc