Skip to content

Commit d6994bc

Browse files
satrongQC-L
andauthored
docs(cn): integrations/vite.md (#21)
* docs(cn): integrations/vite.md * docs(cn): add anchor * doc(cn): update * Update integrations/vite.md Co-authored-by: QiChang Li <[email protected]> Co-authored-by: QiChang Li <[email protected]>
1 parent 8b600c3 commit d6994bc

File tree

1 file changed

+72
-73
lines changed

1 file changed

+72
-73
lines changed

integrations/vite.md

Lines changed: 72 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1-
[speed comparison]: https://twitter.com/antfu7/status/1361398324587163648
2-
[CSS directives]: /features/directives
3-
[classes utilities]: /utilities/
1+
[速度对比]: https://twitter.com/antfu7/status/1361398324587163648
2+
[CSS 指令]: /features/directives
3+
[classes 工具类]: /utilities/
44
[migration]: /guide/migration
55

66
<Logo name="vite" class="logo-float-xl"/>
77

8-
# Integration for [Vite](https://vitejs.dev)
8+
# [Vite](https://vitejs.dev) 集成 {#integration-for-vite}
99

1010
<PackageInfo name="vite-plugin-windicss" author="antfu" />
1111

12-
## Features
12+
## 特性 {#features}
1313

14-
- ⚡️ **It's FAST** - 20~100x times faster than Tailwind on Vite
15-
- 🧩 On-demand CSS utilities (Fully compatible with Tailwind CSS v2)
16-
- 📦 On-demand native elements style resetting (preflight)
17-
- 🔥 Hot module replacement (HMR)
18-
- 🍃 Load configurations from `tailwind.config.js`
19-
- 🤝 Framework-agnostic - Vue, React, Svelte and vanilla!
20-
- 📄 CSS `@apply` / `@screen` directives transforms (also works for Vue SFC's `<style>`)
21-
- 🎳 Support Variant Groups - e.g. `bg-gray-200 hover:(bg-gray-100 text-red-300)`
22-
- 😎 ["Design in Devtools"](#design-in-devtools) - if you work this way in the traditional Tailwind, no reason we can't!
14+
- ⚡️ **极速** - 在 Vite 中比 Tailwind 快20~100倍
15+
- 🧩 按需使用 CSS 工具类(与 Tailwind CSS v2 完全兼容)
16+
- 📦 按需使用原生元素样式重置(预检样式)
17+
- 🔥 模块热替换 (HMR)
18+
- 🍃 `tailwind.config.js` 加载配置
19+
- 🤝 与框架无关 - Vue ReactSvelte and vanilla
20+
- 📄 CSS `@apply` / `@screen` 指令转换(也适用于 Vue SFC`<style>`
21+
- 🎳 支持变量组 - 如 `bg-gray-200 hover:(bg-gray-100 text-red-300)`
22+
- 😎 ["Devtools 设计"](#design-in-devtools) - 支持传统的 Tailwind 运行方式
2323

24-
> Check out the [speed comparison] between Windi CSS and Tailwind CSS on Vite.
24+
> 查看 Windi CSS Tailwind CSS Vite 中的[速度对比]
2525
26-
## Install
26+
## 安装 {#install}
2727

28-
Install the package:
28+
安装相关包:
2929

3030
```bash
3131
npm i -D vite-plugin-windicss windicss
3232
```
3333

34-
Then, install the plugin in your Vite configuration:
34+
然后,在你的 Vite 配置中添加插件:
3535

3636
```ts
3737
// vite.config.js
@@ -44,24 +44,24 @@ export default {
4444
}
4545
```
4646

47-
And finally, import `virtual:windi.css` in your Vite entries:
47+
最后,在你的 Vite 入口文件中导入 `virtual:windi.css`
4848

4949
```js
5050
// main.js
5151
import 'virtual:windi.css'
5252
```
5353

54-
That's it! Starting using [classes utilities] or [CSS directives] in your app, and enjoy the speed! ⚡️
54+
现在可以在你的应用中开始使用 [classes 工具类] 或者 [CSS 指令] ,感受一下速度吧!⚡️
5555

56-
> If you are migrating from Tailwind CSS, also check out the [_Migration_ section][migration]
56+
> 如果你是从 Tailwind CSS 迁移过来,也可以查看 [_迁移_ 章节][migration] 文档
5757
58-
## Supports
58+
## 支持 {#supports}
5959

60-
### TypeScript
60+
### TypeScript {#typeScript}
6161

62-
Enable TypeScript for your `tailwind.config.js`? Sure, why not?
62+
希望在你的 `tailwind.config.js` 启用 TypeScript?当然可以。
6363

64-
Rename it to `tailwind.config.ts` and things just work!
64+
将它重命名为 `tailwind.config.ts` 即可。
6565

6666
```ts
6767
// tailwind.config.ts
@@ -84,33 +84,33 @@ export default defineConfig({
8484
})
8585
```
8686

87-
### Pug Support
87+
### Pug Support {#pug-support}
8888

89-
It will automatically enable Pug support for `.pug` and Vue SFC when dependency `pug` is found in the workspace.
89+
当在工作区中找到依赖项 `pug` 时,它将自动启用对 `.pug` Vue SFC 的支持。
9090

91-
### "Design in DevTools"
91+
### "DevTools 设计" {#design-in-devtools}
9292

93-
It might be a common practice when you use the purge-based Tailwind where you have all the classes in your browser and you can try how things work by directly changing the classes in DevTools. While you might think this is some kind of limitation of "on-demand" where the DevTools don't know those you haven't used in your source code yet.
93+
当你使用基于 Purge 的 Tailwind 时,常见的情况是,你能在浏览器中查看所有的类,同时你可以通过直接改变 DevTools 中的类来了解其是如何生效的。但 DevTools 依旧不能探知那些你还没有在源码中使用的类,所以你可能认为这是“按需”的某种限制。
9494

95-
But unfortunately, **we are here to BREAK the limitation** 😎 See the [video demo](https://twitter.com/antfu7/status/1372244287975387145).
95+
但幸运的是 **我们在这里打破了限制** 😎 查看 [视频示例](https://twitter.com/antfu7/status/1372244287975387145).
9696

97-
Just add the following line to your main entry
97+
只需要在你的主入口文件中添加下面这行代码
9898

9999
```js
100100
import 'virtual:windi-devtools'
101101
```
102102

103-
It will be enabled automatically for you, have fun!
103+
它将自动帮你启用。
104104

105-
Oh, and don't worry about the final bundle, in production build `virtual:windi-devtools` will be an empty module and you don't have to do anything about it :)
105+
不用担心最后的打包,在生产版本中 `virtual:windi-devtools` 将是一个空模块,所以你无需专门处理它。
106106

107-
> ⚠️ Please use it with caution, under the hood we use [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) to detect the class changes. Which means not only your manual changes but also the changes made by your scripts will be detected and included in the stylesheet. This could cause some misalignment between dev and the production build when **using dynamically constructed classes** (false-positive). We recommended adding your dynamic parts to the `safelist` or setup UI regression tests for your production build if possible.
107+
> ⚠️ 请谨慎使用它,因为我们在底层使用了 [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) 来检测类的改变。这意味着你的手动更改和脚本所做的更改都将被检测到并包含在样式表中。**使用动态构造类**(不一定)时,这可能会导致开发和生产版本之间产生一些不一致。我们建议你将动态部分添加到 `safelist` 中,或者如果可以的话,为你的生产环境设置 UI 回归测试。
108108
109-
## Configuration
109+
## 配置 {#configuration}
110110

111-
### Preflight (style reseting)
111+
### 预检样式 (样式重置) {#preflight}
112112

113-
Preflight is enabled on-demanded. If you'd like to completely disable it, you can configure it as below
113+
在按需使用下预检是启用的。如果需要禁用,可以按以下方式配置
114114

115115
```ts
116116
// windi.config.ts
@@ -121,16 +121,16 @@ export default defineConfig({
121121
})
122122
```
123123

124-
### Safelist
124+
### 白名单 {#safelist}
125125

126-
By default, we scan your source code statically and find all the usages of the utilities then generated corresponding CSS on-demand. However, there is some limitation that utilities that decided in the runtime can not be matched efficiently, for example
126+
默认情况下,我们会静态扫描你的源码,并查找所有使用的工具类,然后按需生成相应的 CSS。但存在一些局限性:在 runtime 时工具类无法有效匹配,例如
127127

128128
```html
129-
<!-- will not be detected -->
129+
<!-- 不会被检测到 -->
130130
<div className={`p-${size}`}>
131131
```
132132

133-
For that, you will need to specify the possible combinations in the `safelist` options of `vite.config.js`.
133+
因此,你需要在 `vite.config.js``safelist` 选项中指定可能的组合
134134

135135
```ts
136136
// windi.config.ts
@@ -141,7 +141,7 @@ export default defineConfig({
141141
})
142142
```
143143

144-
Or you can do it this way
144+
或者,也可以这样做
145145

146146
```ts
147147
// windi.config.ts
@@ -153,16 +153,15 @@ function range(size, startAt = 1) {
153153

154154
export default defineConfig({
155155
safelist: [
156-
range(30).map(i => `p-${i}`), // p-1 to p-3
157-
range(10).map(i => `mt-${i}`), // mt-1 to mt-10
156+
range(30).map(i => `p-${i}`), // p-1 p-3
157+
range(10).map(i => `mt-${i}`), // mt-1 mt-10
158158
],
159159
})
160160
```
161161

162-
### Scanning
162+
### 扫描 {#scanning}
163163

164-
On server start, `vite-plugin-windicss` will scan your source code and extract the utility usages. By default,
165-
only files under `src/` with extensions `vue, html, mdx, pug, jsx, tsx` will be included. If you want to enable scanning for other file types of locations, you can configure it via:
164+
在服务启动时,`vite-plugin-windicss` 将扫描你的源码并提取使用的工具类。默认情况下,只有 `src/` 下带有 `vue、html、mdx、pug、jsx、tsx` 扩展名的文件被包含。如果你想扫描其他位置的文件类型,则可以通过以下方式进行配置:
166165

167166
```ts
168167
// windi.config.js
@@ -176,7 +175,7 @@ export default defineConfig({
176175
})
177176
```
178177

179-
Or in plugin options:
178+
或者在插件选项中:
180179

181180
```ts
182181
// vite.config.js
@@ -186,18 +185,18 @@ export default defineConfig({
186185
plugins: [
187186
WindiCSS({
188187
scan: {
189-
dirs: ['.'], // all files in the cwd
190-
fileExtensions: ['vue', 'js', 'ts'], // also enabled scanning for js/ts
188+
dirs: ['.'], // 当前目录下所有文件
189+
fileExtensions: ['vue', 'js', 'ts'], // 同时启用扫描vue/js/ts
191190
},
192191
}),
193192
],
194193
})
195194
```
196195

197196

198-
### [Attributify Mode](https://windicss.org/posts/v30.html#attributify-mode)
197+
### [属性模式](https://windicss.org/posts/v30.html#attributify-mode) {#attributify-mode}
199198

200-
Enabled it by
199+
通过以下配置启用
201200

202201
```ts
203202
// windi.config.ts
@@ -206,7 +205,7 @@ export default {
206205
}
207206
```
208207

209-
And use them as you would like:
208+
然后这样使用:
210209

211210
```html
212211
<button
@@ -220,9 +219,9 @@ And use them as you would like:
220219
</button>
221220
```
222221

223-
#### Prefix
222+
#### 前缀 {#prefix}
224223

225-
If you are concerned about naming confliction, you can add custom prefix to attributify mode by:
224+
如果担心命名冲突,可以通过以下方式给属性模式添加自定义前缀:
226225

227226
```ts
228227
// windi.config.ts
@@ -245,7 +244,7 @@ export default {
245244
</button>
246245
```
247246

248-
### [Alias Config](https://windicss.org/posts/v30.html#alias-config)
247+
### [别名配置](https://windicss.org/posts/v30.html#alias-config) {#alias-config}
249248

250249
```ts
251250
// windi.config.ts
@@ -260,11 +259,11 @@ export default {
260259
}
261260
```
262261

263-
### Layers Ordering
262+
### 层顺序 {#layers-ordering}
264263

265-
> Supported from v0.14.x
264+
> v0.14.x 开始支持
266265
267-
By default, importing `virtual:windi.css` will import all the three layers with the order `base - components - utilities`. If you want to have better controls over the orders, you can separate them by:
266+
默认情况下,导入 `virtual:windi.css` 将会按顺序导入全部三个层 `base - components - utilities` 。如果你想更好地控制顺序,可以通过以下方法将它们分开:
268267

269268
```diff
270269
- import 'virtual:windi.css'
@@ -273,7 +272,7 @@ By default, importing `virtual:windi.css` will import all the three layers with
273272
+ import 'virtual:windi-utilities.css'
274273
```
275274

276-
You can also make your custom css been able to be overridden by certain layers:
275+
你还可以使自定义的 css 能够被某些层覆盖:
277276

278277
```diff
279278
import 'virtual:windi-base.css'
@@ -282,27 +281,27 @@ You can also make your custom css been able to be overridden by certain layers:
282281
import 'virtual:windi-utilities.css'
283282
```
284283

285-
### More
284+
### 更多 {#more}
286285

287-
See [options.ts](https://github.com/windicss/vite-plugin-windicss/blob/main/packages/plugin-utils/src/options.ts) for more configuration reference.
286+
有关更多配置参考,请参阅 [options.ts](https://github.com/windicss/vite-plugin-windicss/blob/main/packages/plugin-utils/src/options.ts)
288287

289-
## Caveats
288+
## 注意事项 {#caveats}
290289

291-
### Scoped Style
290+
### Scoped Style {#scoped-style}
292291

293-
You will need to **set `transformCSS: 'pre'` to get Scoped Style work**.
292+
需要 **设置 `transformCSS:'pre'` 才能使 Scoped Style 工作**
294293

295-
`@media` directive with scoped style can **only works** with `css` `postcss` `scss` but not `sass`, `less` nor `stylus`
294+
带有 scoped style `@media` 指令 **只在** `css` `postcss` `scss` 中有效 ,而在 `sass` `less` `stylus` 中无效。
296295

297-
## Example
296+
## 示例 {#example}
298297

299-
See [examples](https://github.com/windicss/vite-plugin-windicss/blob/main/examples) for *react*, *vue* and *vue with pug* sample projects, or [`Vitesse`](https://github.com/antfu/vitesse)
298+
请参阅 [示例](https://github.com/windicss/vite-plugin-windicss/blob/main/examples) 中的 *react**vue* *vue with pug* 示例项目,或 [`Vitesse`](https://github.com/antfu/vitesse)
300299

301300
---
302301

303-
## SvelteKit (as of 1.0.0-next.100)
302+
## SvelteKit (1.0.0-next.100 开始) {#sveltekit-as-of-1-0-0-next-100}
304303

305-
Install plugin with `npm i -D vite-plugin-windicss` and adapt the svelte config:
304+
通过 `npm i -D vite-plugin-windicss` 安装插件并修改 svelte 配置:
306305

307306
```js
308307
// svelte.config.js
@@ -324,17 +323,17 @@ const config = {
324323
export default config
325324
```
326325

327-
Add `import "virtual:windi.css"` to the top of your $layout.svelte file:
326+
在 $layout.svelte 文件的顶部添加 `import "virtual:windi.css"`
328327

329328
```html
330329
<!-- $layout.svelte -->
331330
<script>
332331
import "virtual:windi.css"
333332
334-
// if you want to enable windi devtools
333+
// 如果你想要启用 windi devtools
335334
import { browser } from "$app/env";
336335
if (browser) import("virtual:windi-devtools")
337336
// ...
338337
</script>
339-
<!-- ...rest of $layout.svelte -->
338+
<!-- ...其他的 $layout.svelte 代码 -->
340339
```

0 commit comments

Comments
 (0)