Skip to content

Commit 88a50fd

Browse files
authored
Merge pull request #20 from baiwusanyu-c/bwsy/next
feat(breaking-change): use the new implementation strategy
2 parents c11c4db + 4f1120a commit 88a50fd

32 files changed

+790
-796
lines changed

README.ZH-CN.md

Lines changed: 21 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,16 @@
66
## Feature
77

88
* 🧩 它是一个 vue 的功能扩展,让你能够在 css 文件中使用 v-bind
9-
* 🌈 支持全平台打包工具构建
9+
* 🌈 支持全平台打包工具构建(vite、webpack)
1010
* ⛰ 支持 css, sass, scss, less, stylus
1111
* ⚡ 轻量且快速
1212

13-
## Core Process
13+
## Core Strategy
14+
1.在使用开发服务器时,`unplugin-vue-cssvars`会从组件开始分析引用的css文件,
15+
并在`@vitejs/plugin-vue` 的转换代码中进行注入样式
16+
2.在打包时`unplugin-vue-cssvars`会从组件开始分析引用的css文件,并将其注入到
17+
sfc 中,别担心会产生多余的代码,打包工具(例如 vite)会自动处理它。
1418

15-
```mermaid
16-
graph LR
17-
A[vite] -- plugin --> B((unplugin-vue-cssvars))
18-
B -- 1.预处理项目中css文件 --> C(生成CSS Module Map获得包含 v-bind 的 css 代码等信息)
19-
C --> D
20-
B -- 2.基于步骤1与vue编译器 --> D(根据 SFC 组件信息获得其引用的 CSS Module)
21-
D --> E
22-
B -- 3.基于vue编译器 --> E(提取 SFC 组件变量)
23-
E --> F
24-
B -- 4.注入提升代码 --> F(匹配CSS Module 与 SFC 变量注入代码)
25-
F --> G((vitejs/plugin-vue))
2619
```
2720
2821
## Install
@@ -45,13 +38,18 @@ pnpm add unplugin-vue-cssvars -D
4538

4639
```ts
4740
// vite.config.ts
48-
import { resolve } from 'path'
4941
import { defineConfig } from 'vite'
5042
import { viteVueCSSVars } from 'unplugin-vue-cssvars'
5143
import type { PluginOption } from 'vite'
44+
import vue from '@vitejs/plugin-vue'
5245
export default defineConfig({
5346
plugins: [
54-
viteVueCSSVars(/* options */) as PluginOption,
47+
vue(),
48+
viteVueCSSVars({
49+
include: [/.vue/],
50+
includeCompile: ['**/**.scss'],
51+
server: false,
52+
}) as PluginOption,
5553
],
5654
})
5755
```
@@ -63,7 +61,6 @@ export default defineConfig({
6361

6462
```ts
6563
// rollup.config.js
66-
import { resolve } from 'path'
6764
import { rollupVueCSSVars } from 'unplugin-vue-cssvars'
6865
export default {
6966
plugins: [
@@ -127,6 +124,7 @@ export interface Options {
127124
* @default process.cwd()
128125
*/
129126
rootDir?: string
127+
130128
/**
131129
* 需要转换的文件名后缀列表(目前只支持.vue)RegExp or glob
132130
*/
@@ -144,100 +142,22 @@ export interface Options {
144142
*/
145143
revoke?: boolean
146144

147-
/**
148-
* 预处理器
149-
* unplugin-vue-cssvars包没有集成预处理器,
150-
* 当你想在预处理器文件中使用unplugin-vue-cssvars时,
151-
* 请将预处理器传递给unplugin-vue-cssvars
152-
* @property { sass | less | stylus }
153-
*/
154-
preprocessor?: PreProcessor
155-
156145
/**
157146
* 选择需要处理编译的文件,默认是css
158147
* 例如:如果你想要处理.scss文件,那么你可以传入 ['** /**.sass']
159148
* @property { ['** /**.css', '** /**.less', '** /**.scss', '** /**.sass', '** /**.styl'] }
160149
* @default ['** /**.css']
161150
*/
162151
includeCompile?: Array<string>
152+
153+
/**
154+
* 标记是否为开发服务器使用
155+
* 因为 unplugin-vue-cssvars uses 在开发服务器上和打包中使用了不同策略
156+
* @default true
157+
*/
158+
server?: boolean
163159
}
164160
```
165-
### 使用预处理器
166-
`unplugin-vue-cssvars` 包没有集成预处理器, 当你想在预处理器文件中使用 `unplugin-vue-cssvars` 时, 请将预处理器传递给 `unplugin-vue-cssvars`
167-
168-
````typescript
169-
// vite.config.ts
170-
import { defineConfig } from 'vite'
171-
import { viteVueCSSVars } from 'unplugin-vue-cssvars'
172-
import sass from 'sass'
173-
import type { PluginOption } from 'vite'
174-
export default defineConfig({
175-
plugins: [
176-
viteVueCSSVars({
177-
preprocessor: { sass },
178-
includeCompile: ['**/**.css', '**/**.scss'],
179-
}) as PluginOption,
180-
],
181-
})
182-
````
183-
在上面的例子中,如果你的项目使用了 `scss`,那么你需要配置 `preprocessor: { sass }`,
184-
值得注意的是,你还需要配置 `includeCompile: ['**/**.css', '**/**.scss']`,
185-
因为读取哪些文件(.sass 或 .less,还是 .styl)来使用 `unplugin-vue-cssvars` 完全由你来控制。
186-
187-
### 关于 revoke 详细说明
188-
> 💡 正式版本以解决重复代码问题,正式版本不再需要设置
189-
190-
有如下两个文件 `App.vue``test.css`
191-
````
192-
<script setup lang="ts">
193-
const color = 'red'
194-
</script>
195-
196-
<template>
197-
<div class="test">
198-
TEST
199-
</div>
200-
</template>
201-
202-
<style scoped>
203-
@import "./assets/test";
204-
</style>
205-
206-
````
207-
````
208-
/** test.css **/
209-
div {
210-
color: v-bind(color);
211-
}
212-
````
213-
当未使用 `unplugin-vue-cssvars` 使用 `vite` 构建后
214-
````
215-
/** test.css **/
216-
div[data-v-2e7c9788] {
217-
color: var(--8bcabd20);
218-
}
219-
````
220-
其中 `color: var(--8bcabd20);` 的哈希并不会出现在组件打包产物中,因为 `vue` 不支持在文件中使用 `v-bind`
221-
当使用 `unplugin-vue-cssvars` 使用 `vite` 构建后(`minify: false`
222-
````
223-
/** test.css **/
224-
div[data-v-1dfefb04] {
225-
color: var(--516b0d4a);
226-
}
227-
228-
/* created by @unplugin-vue-cssvars */
229-
/* <inject start> */
230-
div[data-v-1dfefb04]{color:var(--516b0d4a)}
231-
/* <inject end> */
232-
````
233-
可以看到通过 `unplugin-vue-cssvars` 会生成注入代码,并且依赖于 `@vue/compiler-dom`,其哈希值能够出现在组件打包产物中。
234-
但是观察发现,这段代码是重复的。因此,开启 `revoke` 选项,将移除重复代码
235-
````
236-
/** test.css **/
237-
div[data-v-1dfefb04] {
238-
color: var(--516b0d4a);
239-
}
240-
````
241161

242162
## Tips
243163

README.md

Lines changed: 25 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,17 @@ English | [中文](https://github.com/baiwusanyu-c/unplugin-vue-cssvars/blob/mas
66
## Feature
77

88
* 🧩 It is a function extension of vue
9-
* 🌈 Compatible with multiple bundled platforms(vite、rollup、esbuild、webpack)
9+
* 🌈 Compatible with multiple bundled platforms(vite、webpack)
1010
* ⛰ Support css, sass, scss, less, stylus
1111
* ⚡ light and fast
1212

13-
## Core Process
13+
## Core Strategy
1414

15-
```mermaid
16-
graph LR
17-
A[vite] -- plugin --> B((unplugin-vue-cssvars))
18-
B -- 1.Preprocess css files in the project --> C(Generate CSS Module Map to obtain information such as css code including v-bind)
19-
C --> D
20-
B -- 2.Based on step 1 with vue compiler --> D(Obtain the referenced CSS Module according to the SFC component information)
21-
D --> E
22-
B -- 3.Based on vue compiler --> E(Extract SFC component tags)
23-
E --> F
24-
B -- 4.inject code --> F(Match CSS Module and SFC variable injection code)
25-
F --> G((vitejs/plugin-vue))
26-
```
15+
1.When using the development server,
16+
`unplugin-vue-cssvars` will analyze the referenced css file from the component,
17+
and inject styles in the transformation code of `@vitejs/plugin-vue`
18+
2.When building, `unplugin-vue-cssvars` will analyze the referenced css file from the component and inject it into
19+
sfc, don't worry about generating redundant code, packaging tools (such as vite) will automatically handle it.
2720

2821
## Install
2922

@@ -45,13 +38,18 @@ pnpm add unplugin-vue-cssvars -D
4538

4639
```ts
4740
// vite.config.ts
48-
import { resolve } from 'path'
4941
import { defineConfig } from 'vite'
5042
import { viteVueCSSVars } from 'unplugin-vue-cssvars'
5143
import type { PluginOption } from 'vite'
44+
import vue from '@vitejs/plugin-vue'
5245
export default defineConfig({
5346
plugins: [
54-
viteVueCSSVars(/* options */) as PluginOption,
47+
vue(),
48+
viteVueCSSVars({
49+
include: [/.vue/],
50+
includeCompile: ['**/**.scss'],
51+
server: false,
52+
}) as PluginOption,
5553
],
5654
})
5755
```
@@ -63,12 +61,11 @@ export default defineConfig({
6361

6462
```ts
6563
// rollup.config.js
66-
import { resolve } from 'path'
6764
import { rollupVueCSSVars } from 'unplugin-vue-cssvars'
6865
export default {
69-
plugins: [
70-
rollupVueCSSVars(/* options */),
71-
],
66+
plugins: [
67+
rollupVueCSSVars(/* options */),
68+
],
7269
}
7370
```
7471

@@ -124,10 +121,10 @@ build({
124121
export interface Options {
125122
/**
126123
* Provide path which will be transformed
127-
*
128124
* @default process.cwd()
129125
*/
130126
rootDir?: string
127+
131128
/**
132129
* RegExp or glob to match files to be transformed
133130
*/
@@ -144,103 +141,22 @@ export interface Options {
144141
*/
145142
revoke?: boolean
146143

147-
/**
148-
* preprocessor
149-
* the unplugin-vue-cssvars package does not integrate a preprocessor,
150-
* when you want to use unplugin-vue-cssvars in the preprocessor file,
151-
* please pass the preprocessor to unplugin-vue-cssvars
152-
* @property { sass | less | stylus }
153-
*/
154-
preprocessor?: PreProcessor
155-
156144
/**
157145
* Specify the file to be compiled, for example,
158146
* if you want to compile scss, then you can pass in ['** /**.sass']
159147
* @property { ['** /**.css', '** /**.less', '** /**.scss', '** /**.sass', '** /**.styl'] }
160148
* @default ['** /**.css']
161149
*/
162150
includeCompile?: Array<string>
151+
152+
/**
153+
* Flag whether to start with server at development time,
154+
* because unplugin-vue-cssvars uses different strategies for building and server development
155+
* @default true
156+
*/
157+
server?: boolean
163158
}
164159
```
165-
### use preprocessor
166-
the `unplugin-vue-cssvars` package does not integrate a preprocessor,
167-
when you want to use `unplugin-vue-cssvars` in the preprocessor file,
168-
please pass the preprocessor to `unplugin-vue-cssvars`
169-
170-
````typescript
171-
// vite.config.ts
172-
import { defineConfig } from 'vite'
173-
import { viteVueCSSVars } from 'unplugin-vue-cssvars'
174-
import sass from 'sass'
175-
import type { PluginOption } from 'vite'
176-
export default defineConfig({
177-
plugins: [
178-
viteVueCSSVars({
179-
preprocessor: { sass },
180-
includeCompile: ['**/**.css', '**/**.scss'],
181-
}) as PluginOption,
182-
],
183-
})
184-
````
185-
In the above example, if your project uses `scss`, then you need to configure `preprocessor: { sass }`,
186-
It is worth noting that you also need to configure `includeCompile: ['**/**.css', '**/**.scss']`,
187-
Because it is entirely up to you to read which files (.sass or .less, or .styl) to use `unplugin-vue-cssvars`.
188-
189-
### Details about revoke
190-
> 💡 v1.0.0 version to solve the problem of duplicate code, no longer need to set
191-
192-
Suppose there are two files `App.vue` and `test.css`
193-
````
194-
<script setup lang="ts">
195-
const color = 'red'
196-
</script>
197-
198-
<template>
199-
<div class="test">
200-
TEST
201-
</div>
202-
</template>
203-
204-
<style scoped>
205-
@import "./assets/test";
206-
</style>
207-
208-
````
209-
````
210-
/** test.css **/
211-
div {
212-
color: v-bind(color);
213-
}
214-
````
215-
After building with `vite` when `unplugin-vue-cssvars` is not used
216-
````
217-
/** test.css **/
218-
div[data-v-2e7c9788] {
219-
color: var(--8bcabd20);
220-
}
221-
````
222-
Among them, the hash of `color: var(--8bcabd20);` will not appear in the component packaging product, because `vue` does not support the use of `v-bind` in the file.
223-
When built with `vite` using `unplugin-vue-cssvars` (`minify: false`)
224-
````
225-
/** test.css **/
226-
div[data-v-1dfefb04] {
227-
color: var(--516b0d4a);
228-
}
229-
230-
/* created by @unplugin-vue-cssvars */
231-
/* <inject start> */
232-
div[data-v-1dfefb04]{color:var(--516b0d4a)}
233-
/* <inject end> */
234-
````
235-
It can be seen that the code will be injected through `unplugin-vue-cssvars`, and it depends on `@vue/compiler-dom`, whose hash value can appear in the component packaging product.
236-
But observation found that this code is repetitive.
237-
Therefore, turning on the `revoke` option will remove duplicate code
238-
````
239-
/** test.css **/
240-
div[data-v-1dfefb04] {
241-
color: var(--516b0d4a);
242-
}
243-
````
244160

245161
## Tips
246162

0 commit comments

Comments
 (0)