Giter Site home page Giter Site logo

unplugin / unplugin-vue-cssvars Goto Github PK

View Code? Open in Web Editor NEW
131.0 7.0 21.0 1.3 MB

🌀 A vue plugin that allows you to use vue's CSSVars feature in css files

License: MIT License

TypeScript 92.09% JavaScript 0.76% HTML 0.52% Vue 3.20% CSS 1.01% SCSS 2.01% Less 0.16% Stylus 0.14% Sass 0.13%
css less rollup rspack sass unplugin vite vue vue3 webpack webpack4 webpack5 cssvars

unplugin-vue-cssvars's Introduction

unplugin-vue-cssvars

🌀 A vue plugin that allows you to use vue3's CSSVars feature in css files

English | 中文

Feature

  • 🧩 It is a function extension of vue
  • 🌈 Compatible with multiple bundled platforms(vite、webpack)
  • ⛰ Support css, sass, scss, less, stylus
  • ⚡ Support hmr

Core Strategy

1.When using the development server, unplugin-vue-cssvars will analyze the referenced css file from the component, and inject styles in the transformation code of @vitejs/plugin-vue 2.When building, unplugin-vue-cssvars will analyze the referenced css file from the component and inject it into sfc, don't worry about generating redundant code, packaging tools (such as vite) will automatically handle it.

Install

npm i unplugin-vue-cssvars -D

Or

yarn add unplugin-vue-cssvars -D

Or

pnpm add unplugin-vue-cssvars -D

Usage

  1. use plugin and set options
Vite
// vite.config.ts
import { defineConfig } from 'vite'
import { viteVueCSSVars } from 'unplugin-vue-cssvars'
import vue from '@vitejs/plugin-vue'
import type { PluginOption } from 'vite'
export default defineConfig({
  plugins: [
    vue(),
    viteVueCSSVars({
      include: [/.vue/],
      includeCompile: ['**/**.scss'],
      server: false,
    }) as PluginOption,
  ],
})

Rollup
// rollup.config.js
import { rollupVueCSSVars } from 'unplugin-vue-cssvars'
export default {
  plugins: [
    rollupVueCSSVars(/* options */),
  ],
}

Webpack
// webpack.config.js
module.exports = {
  /* ... */
  plugins: [
    require('unplugin-vue-cssvars').webpackVueCSSVars({ /* options */ }),
  ],
}

Vue CLI
// vue.config.js
module.exports = {
  configureWebpack: {
    plugins: [
      require('unplugin-vue-cssvars').webpackVueCSSVars({ /* options */ }),
    ],
  },
}

ESBuild
// esbuild.config.js
import { build } from 'esbuild'
import { esbuildVueCSSVars } from 'unplugin-vue-cssvars'

build({
  plugins: [esbuildVueCSSVars(/* options */)],
})
  1. use v-bind-m
// foo.css
.foo{
   color: v-bind-m(fontColor)
}
  1. use alias
    For example you have the following project structure:

img.png

// App.vue
<template>
 <div class="scss">
   app
 </div>
</template>

<style lang="scss" scoped>
@import '@/assets/scss/mixin';
</style>

Then you can configure like this

// vite.config.ts
import { resolve } from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { viteVueCSSVars } from '../dist'
export default defineConfig({
  resolve: {
    alias: {
      '@': resolve(__dirname, './src'),
    },
  },
  plugins: [
    vue(),
    viteVueCSSVars({
      include: [/.vue/],
      includeCompile: ['**/**.scss'],
      alias: {
        '@': resolve(__dirname, './src'),
      },
    }),
  ],
})

Option

export interface Options {
   /**
    * Provide path which will be transformed
    * @default process.cwd()
    */
   rootDir?: string
   
   /**
    * RegExp or glob to match files to be transformed
    */
   include?: FilterPattern

   /**
    * RegExp or glob to match files to NOT be transformed
    */
   exclude?: FilterPattern
   
   /**
    * Specify the file to be compiled, for example,
    * if you want to compile scss, then you can pass in ['** /**.sass']
    * @property { ['** /**.css', '** /**.less', '** /**.scss', '** /**.sass', '** /**.styl'] }
    * @default ['** /**.css']
    */
   includeCompile?: Array<string>
   
   /**
    * Flag whether to start with server at development time,
    * because unplugin-vue-cssvars uses different strategies for building and server development
    * If it is not passed in vite, unplugin-vue-cssvars will automatically 
    * recognize the command of config to determine the server value
    * @default true
    */
   server?: boolean

   /**
    * alias
    * @default undefined
    */
   alias?: Record<string, string>
}

Tips

● Rules When Transforming Analysis

  1. In sfc, if @import specifies a suffix, the conversion analysis will be performed according to the suffix file, otherwise the conversion analysis will be performed according to the lang attribute of the current style tag (default css)
  2. Rules in css: css files can only reference css files, and only files with css suffixes will be parsed.
  3. Rules in scss, less, stylus: scss, less, stylus files can refer to css files, and corresponding scss or less files or stylus files, Prioritize conversion analysis of files with preprocessor suffixes, if the file does not exist, analyze its css file

● Variable extraction rules in SFC

  1. For script setup, unplugin-vue-cssvars will extract all variables to match.
<script setup>
    const color = 'red'
</script>
  1. For composition api, unplugin-vue-cssvars will extract setup function return variables for matching.
<script>
 import { defineComponent } from 'vue'
 export default defineComponent( {
   setup(){
       const color = 'red'
       return {
          color
       }
   }
})
</script>
  1. For options api, unplugin-vue-cssvars will extract data function return variables for matching.
<script>
 export default {
   data(){
       const color = 'red'
       return {
          color
       }
   }
}
</script>
  1. For normal script, unplugin-vue-cssvars will extract all variables to match.
<script>
    const color = 'red'
</script>

● Variable conflict rules in SFC

  1. In sfc, there are options API and composition API, and all variables will be merged. If there are conflicts in variables, the syntax that appears later will take precedence (for example, if options API is written first and composition API is written later, composition API takes precedence).
  2. There are script setup, options api and composition api in sfc, all variables will be merged, if there is a variable conflict, script setup will take precedence
  3. Ordinary script in sfc will not exist at the same time as options api and composition api
  4. If the normal script exists in sfc, there must be script setup
  5. Common script and script setup variables in sfc will be merged, if there is a variable conflict, script setup will take precedence

● Priority after style injection

  1. Starting from sfc, analyze the css files referenced in the style tag, and in accordance with the order of references in the css files, they will be promoted in depth-first order and injected into sfc.
  2. After being injected into sfc, its priority is completely determined by the compiler of @vue/compiler-dom.

Thanks

unplugin-vue-cssvars's People

Contributors

baiwusanyu-c avatar dependabot[bot] avatar faga295 avatar renovate[bot] avatar simon-he95 avatar wangxiaoshow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

unplugin-vue-cssvars's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Warning

These dependencies are deprecated:

Datasource Name Replacement PR?
npm @vitest/coverage-c8 Unavailable
npm npm-run-all Available

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update actions/checkout digest to 692973e
  • chore(deps): update dependency @types/debug to v4.1.12
  • chore(deps): update dependency @types/estree to v1.0.5
  • chore(deps): update dependency @types/gulp to v4.0.17
  • chore(deps): update dependency @types/hash-sum to v1.0.2
  • chore(deps): update dependency @types/less to v3.0.6
  • chore(deps): update dependency @types/stylus to v0.48.42
  • chore(deps): update dependency rimraf to v5.0.10
  • chore(deps): update vitest monorepo to v0.34.7 (@vitest/ui, vitest)
  • fix(deps): update dependency baiwusanyu-utils to v1.0.18
  • fix(deps): update dependency fast-glob to v3.3.2
  • fix(deps): update dependency magic-string to v0.30.11
  • chore(deps): update babel monorepo (@babel/core, @babel/parser, @babel/types)
  • chore(deps): update dependency @rollup/pluginutils to v5.1.0
  • chore(deps): update dependency @types/node to v20.14.15
  • chore(deps): update dependency @vitejs/plugin-vue to v4.6.2
  • chore(deps): update dependency @vitejs/plugin-vue-jsx to v3.1.0
  • chore(deps): update dependency bumpp to v9.5.1
  • chore(deps): update dependency eslint to v8.57.0
  • chore(deps): update dependency esno to v4.7.0
  • chore(deps): update dependency lint-staged to v15.2.9
  • chore(deps): update dependency magic-string-ast to ^0.6.0
  • chore(deps): update dependency rollup to v4.20.0
  • chore(deps): update dependency sass to v1.77.8
  • chore(deps): update dependency simple-git-hooks to v2.11.1
  • chore(deps): update dependency sucrase to v3.35.0
  • chore(deps): update dependency tsup to v8.2.4
  • chore(deps): update dependency vite to v4.5.3
  • chore(deps): update dependency webpack to v5.93.0
  • chore(deps): update vue monorepo to v3.4.38 (@vue/compiler-dom, @vue/compiler-sfc, vue)
  • fix(deps): update dependency @vue/babel-plugin-jsx to v1.2.2
  • fix(deps): update dependency core-js to v3.38.0
  • fix(deps): update dependency fs-extra to v11.2.0 (fs-extra, @types/fs-extra)
  • fix(deps): update dependency unplugin to v1.12.1
  • chore(deps): update dependency @vitejs/plugin-vue-jsx to v4
  • chore(deps): update dependency eslint to v9
  • chore(deps): update dependency gulp to v5
  • chore(deps): update dependency rimraf to v6
  • chore(deps): update pnpm to v9
  • 🔐 Create all rate-limited PRs at once 🔐

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/releases.yml
  • actions/checkout v4@b4ffde65f46336ab88eb53be808477a3936bae11
  • pnpm/action-setup v2
  • actions/setup-node v4
.github/workflows/unit-test.yml
  • actions/checkout v4@b4ffde65f46336ab88eb53be808477a3936bae11
  • pnpm/action-setup v2
  • actions/setup-node v4
npm
build/package.json
package.json
  • @ast-grep/napi ^0.12.0
  • baiwusanyu-utils ^1.0.15
  • chalk ^5.3.0
  • estree-walker-ts ^1.0.1
  • fast-glob ^3.3.1
  • fs-extra ^11.1.1
  • hash-sum ^2.0.0
  • magic-string ^0.30.3
  • unplugin ^1.4.0
  • vue ^3.3.4
  • @babel/parser ^7.22.14
  • @babel/types ^7.22.11
  • @baiwusanyu/eslint-config ^1.0.15
  • @rollup/pluginutils ^5.0.4
  • @types/css-tree ^2.3.1
  • @types/debug ^4.1.8
  • @types/estree ^1.0.1
  • @types/fs-extra ^11.0.1
  • @types/gulp ^4.0.13
  • @types/hash-sum ^1.0.0
  • @types/less ^3.0.4
  • @types/node ^20.5.7
  • @types/stylus ^0.48.39
  • @vitejs/plugin-vue ^4.3.4
  • @vitejs/plugin-vue-jsx ^3.0.2
  • @vitest/coverage-c8 ^0.33.0
  • @vitest/ui ^0.34.3
  • @vue/compiler-dom ^3.3.4
  • @vue/compiler-sfc ^3.3.4
  • bumpp ^9.2.0
  • cross-env ^7.0.3
  • eslint ^8.48.0
  • esno ^4.0.0
  • git-ensure ^0.1.0
  • gulp ^4.0.2
  • jsdom ^22.1.0
  • less ^4.2.0
  • lint-staged ^15.0.0
  • magic-string-ast ^0.3.0
  • npm-run-all ^4.1.5
  • rimraf ^5.0.1
  • rollup ^4.0.0
  • sass ^1.66.1
  • simple-git-hooks ^2.9.0
  • stylus ^0.60.0
  • sucrase ^3.34.0
  • tsup ^8.0.0
  • typescript 5.3.2
  • vite ^4.4.9
  • vitest ^0.34.3
  • webpack ^5.88.2
  • @ast-grep/napi ^0.6.3 || ^0.11.0 || ^0.12.0
  • baiwusanyu-utils ^1.0.12
  • chalk ^4.1.2 || ^5.0.0
  • estree-walker-ts ^1.0.0
  • fast-glob ^3.2.12
  • fs-extra ^11.1.1
  • hash-sum ^2.0.0
  • magic-string ^0.30.0
  • unplugin ^1.3.1
  • vue ^3.2.47
  • pnpm 8.9.2
packages/core/package.json
packages/entry/package.json
play/vite/package.json
play/webpack/package.json
  • @vue/babel-plugin-jsx ^1.1.5
  • core-js ^3.32.1
  • @babel/core ^7.22.11
  • @vue/cli-plugin-babel ~5.0.8
  • @vue/cli-plugin-typescript ~5.0.8
  • @vue/cli-service ~5.0.8
utils/package.json

  • Check this box to trigger a request for Renovate to run again on this repository

[bug] error is reported during the build process

  1. The scss importer needs to be promoted to the top to ensure that variables can be accessed normally
  2. When there are multiple style tags, which style tag needs to be associated with the importer information mapped in injectCSSContent

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.