@@ -15,13 +15,13 @@ English | [中文](https://github.com/baiwusanyu-c/unplugin-vue-cssvars/blob/mas
15
15
``` mermaid
16
16
graph LR
17
17
A[vite] -- plugin --> B((unplugin-vue-cssvars))
18
- B -- 1.预处理项目中css文件 --> C(生成CSS Module Map获得包含 v-bind 的 css 代码等信息 )
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
19
C --> D
20
- B -- 2.基于步骤1与vue编译器 --> D(根据 SFC 组件信息获得其引用的 CSS Module)
20
+ B -- 2.Based on step 1 with vue compiler --> D(Obtain the referenced CSS Module according to the SFC component information )
21
21
D --> E
22
- B -- 3.基于vue编译器 --> E(提取 SFC 组件变量 )
22
+ B -- 3.Based on vue compiler --> E(Extract SFC component tags )
23
23
E --> F
24
- B -- 4.注入提升代码 --> F(匹配CSS Module 与 SFC 变量注入代码 )
24
+ B -- 4.inject code --> F(Match CSS Module and SFC variable injection code )
25
25
F --> G((vitejs/plugin-vue))
26
26
```
27
27
@@ -122,31 +122,31 @@ build({
122
122
123
123
``` typescript
124
124
export interface Options {
125
- /**
126
- * 需要转换的路径,默认是项目根目录
127
- * @default process.cwd()
128
- */
129
- rootDir? : string
130
- /**
131
- * 需要转换的文件名后缀列表(目前只支持.vue)RegExp or glob
132
- */
133
- include? : FilterPattern
125
+ /**
126
+ * Provide path which will be transformed
127
+ *
128
+ * @default process.cwd()
129
+ */
130
+ rootDir? : string
131
+ /**
132
+ * RegExp or glob to match files to be transformed
133
+ */
134
+ include? : FilterPattern
134
135
135
- /**
136
- * 不需要转换的文件名后缀列表(目前只支持.vue) RegExp or glob
137
- */
138
- exclude? : FilterPattern
136
+ /**
137
+ * RegExp or glob to match files to NOT be transformed
138
+ */
139
+ exclude? : FilterPattern
139
140
140
- /**
141
- * `unplugin-vue-cssvars` 只是做了样式提升注入,其编译依旧依赖于 `@vue/compiler-dom`
142
- * 在某些时候可能会生成重复的 `css` 代码(一般不会,因为打包时会将重复代码删除),例如 `vite` 中关闭构建
143
- * 时压缩选项,`revoke` 则可以在打包时将注入的代码删除
144
- */
145
- revoke? : boolean
141
+ /**
142
+ * unplugin-vue-cssvars depends on the vue compiler,
143
+ * there may be duplicate css after packaging, here we clear it
144
+ */
145
+ revoke? : boolean
146
146
}
147
147
```
148
- ### 关于 revoke 详细说明
149
- 有如下两个文件 ` App.vue ` 和 ` test.css `
148
+ ### Details about revoke
149
+ Suppose there are two files ` App.vue ` and ` test.css `
150
150
````
151
151
<script setup lang="ts">
152
152
const color = 'red'
@@ -169,15 +169,15 @@ div {
169
169
color: v-bind(color);
170
170
}
171
171
````
172
- 当未使用 ` unplugin-vue-cssvars ` 使用 ` vite ` 构建后
172
+ After building with ` vite ` when ` unplugin-vue-cssvars ` is not used
173
173
````
174
174
/** test.css **/
175
175
div[data-v-2e7c9788] {
176
176
color: var(--8bcabd20);
177
177
}
178
178
````
179
- 其中 ` color: var(--8bcabd20); ` 的哈希并不会出现在组件打包产物中,因为 ` vue ` 不支持在文件中使用 ` v-bind ` 。
180
- 当使用 ` unplugin-vue-cssvars ` 使用 ` vite ` 构建后( ` minify: false ` )
179
+ 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.
180
+ When built with ` vite ` using ` unplugin-vue-cssvars ` ( ` minify: false ` )
181
181
````
182
182
/** test.css **/
183
183
div[data-v-1dfefb04] {
@@ -189,8 +189,9 @@ div[data-v-1dfefb04] {
189
189
div[data-v-1dfefb04]{color:var(--516b0d4a)}
190
190
/* <inject end> */
191
191
````
192
- 可以看到通过 ` unplugin-vue-cssvars ` 会生成注入代码,并且依赖于 ` @vue/compiler-dom ` ,其哈希值能够出现在组件打包产物中。
193
- 但是观察发现,这段代码是重复的。因此,开启 ` revoke ` 选项,将移除重复代码
192
+ 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.
193
+ But observation found that this code is repetitive.
194
+ Therefore, turning on the ` revoke ` option will remove duplicate code
194
195
````
195
196
/** test.css **/
196
197
div[data-v-1dfefb04] {
@@ -200,20 +201,21 @@ div[data-v-1dfefb04] {
200
201
201
202
## Tips
202
203
203
- ### ● 转换分析时的约定规则
204
- 1 . ` sfc ` 中,如果 ` @import ` 指定了后缀,则根据后缀的文件进行转换分析,否则根据当前 ` script ` 标签的 ` lang ` 属性(默认 ` css ` )进行转换分析
205
- 2 . ` css ` 中规则:` css ` 文件只能引用 ` css ` 文件,只会解析 ` css ` 后缀的文件。
206
- 3 . ` scss ` 、` less ` 、` stylus ` 中规则:` scss ` 、` less ` 、` stylus文件可以引用 ` ` css ` 文件、以及对应的 ` scss ` 或 ` less ` 文件或 ` stylus ` 文件,
207
- 则对同名文件的 ` css ` 文件和对应的预处理器后缀文件进行转换分析。
204
+ ### ● Rules When Transforming Analysis
205
+ 1 . In ` sfc ` , if ` @import ` specifies a suffix, the conversion analysis will be performed according to the suffix file,
206
+ otherwise the conversion analysis will be performed according to the ` lang ` attribute of the current ` script ` tag (default ` css ` )
207
+ 2 . Rules in ` css ` : ` css ` files can only reference ` css ` files, and only files with ` css ` suffixes will be parsed.
208
+ 3 . Rules in ` scss ` , ` less ` , ` stylus ` : ` scss ` , ` less ` , ` stylus ` files can refer to` ` css` files, and corresponding ` scss` or ` less` files or ` stylus` files,
209
+ The ` css ` file of the file with the same name and the corresponding preprocessor suffix file are converted and analyzed.
208
210
209
- ### ● sfc 中变量提取规则
210
- 1 . 对于 ` script setup ` , ` unplugin-vue-cssvars ` 会提取所有变量进行匹配。
211
+ ### ● Variable extraction rules in SFC
212
+ 1 . For ` script setup ` , ` unplugin-vue-cssvars ` will extract all variables to match.
211
213
````
212
214
<script setup>
213
215
const color = 'red'
214
216
</script>
215
217
````
216
- 2 . 对于 ` composition api ` , ` unplugin-vue-cssvars ` 会提取 ` setup ` 函数返回变量进行匹配。
218
+ 2 . For ` composition api ` , ` unplugin-vue-cssvars ` will extract ` setup ` function return variables for matching.
217
219
````
218
220
<script>
219
221
export default {
@@ -226,7 +228,7 @@ div[data-v-1dfefb04] {
226
228
}
227
229
</script>
228
230
````
229
- 3 . 对于 ` options api ` , ` unplugin-vue-cssvars ` 会提取 ` data ` 函数返回变量进行匹配。
231
+ 3 . For ` options api ` , ` unplugin-vue-cssvars ` will extract ` data ` function return variables for matching.
230
232
````
231
233
<script>
232
234
export default {
@@ -239,24 +241,27 @@ div[data-v-1dfefb04] {
239
241
}
240
242
</script>
241
243
````
242
- 4 . 对于普通的 ` script ` , ` unplugin-vue-cssvars ` 会提取所有变量进行匹配。
244
+ 4 . For normal ` script ` , ` unplugin-vue-cssvars ` will extract all variables to match.
243
245
````
244
246
<script>
245
247
const color = 'red'
246
248
</script>
247
249
````
248
250
249
- ### ● sfc 中变量冲突规则
250
- 1 . ` sfc ` 中有 ` options api ` 与 ` composition api ` , 所有变量会进行合并
251
- 变量出现冲突以后面出现的(比如先写了 ` options api ` ,后写 ` composition api ` ,以 ` composition api ` 优先)优先
252
- 2 . ` sfc ` 中有 ` script setup ` 、` options api ` 与 ` composition api ` , 所有变量会进行合并,变量出现冲突以` script setup ` 优先
253
- 3 . ` sfc ` 中普通的 ` script ` ,不会与` options api ` 、 ` composition api ` 同时存在
254
- 4 . ` sfc ` 中普通的 ` script ` 若存在,则必存在` script setup `
255
- 5 . ` sfc ` 中普通的 ` script ` 与 ` script setup ` 所有变量会进行合并,变量出现冲突以` script setup ` 优先
251
+ ### ● Variable conflict rules in SFC
252
+ 1 . In sfc, there are options API and composition API, and all variables will be merged. If there are conflicts in variables,
253
+ the syntax that appears later will take precedence
254
+ (for example, if options API is written first and composition API is written later, composition API takes precedence).
255
+ 2 . There are ` script setup ` , ` options api ` and ` composition api ` in ` sfc ` , all variables will be merged,
256
+ if there is a variable conflict, ` script setup ` will take precedence
257
+ 3 . Ordinary ` script ` in ` sfc ` will not exist at the same time as ` options api ` and ` composition api `
258
+ 4 . If the normal ` script ` exists in ` sfc ` , there must be ` script setup `
259
+ 5 . Common ` script ` and ` script setup ` variables in ` sfc ` will be merged,
260
+ if there is a variable conflict, ` script setup ` will take precedence
256
261
257
- ### ● 样式提升后的优先级
258
- 1 . 从 ` sfc ` 开始,分析 ` style ` 标签中引用的 ` css ` 文件,按照 ` css ` 文件中的引用顺序,深度优先依次提升并注入到 ` sfc ` 中。
259
- 2 . 注入到 ` sfc ` 后,其优先级完全由 ` @vue/compiler-dom ` 的编译器决定。
262
+ ### ● Priority after style injection
263
+ 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 ` .
264
+ 2 . After being injected into ` sfc ` , its priority is completely determined by the compiler of ` @vue/compiler-dom ` .
260
265
261
266
## Thanks
262
267
* [ vue] ( https://github.com/vuejs/core )
0 commit comments