Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(cn): integrations/vite.md #21

Merged
merged 4 commits into from
Jun 2, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 69 additions & 70 deletions integrations/vite.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
[speed comparison]: https://twitter.com/antfu7/status/1361398324587163648
[CSS directives]: /features/directives
[classes utilities]: /utilities/
[速度对比]: https://twitter.com/antfu7/status/1361398324587163648
[CSS指令]: /features/directives
satrong marked this conversation as resolved.
Show resolved Hide resolved
[classes工具类]: /utilities/
satrong marked this conversation as resolved.
Show resolved Hide resolved
[migration]: /guide/migration

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

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

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

## Features
## 功能 {#features}
satrong marked this conversation as resolved.
Show resolved Hide resolved

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

> Check out the [speed comparison] between Windi CSS and Tailwind CSS on Vite.
> 查看 Windi CSS Tailwind CSS Vite 中的[速度对比]。

## Install
## 安装 {#install}

Install the package:
安装包:
satrong marked this conversation as resolved.
Show resolved Hide resolved

```bash
npm i -D vite-plugin-windicss windicss
```

Then, install the plugin in your Vite configuration:
然后,在你的 Vite 配置中安装插件:
satrong marked this conversation as resolved.
Show resolved Hide resolved

```ts
// vite.config.js
Expand All @@ -44,24 +44,24 @@ export default {
}
```

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

```js
// main.js
import 'virtual:windi.css'
```

That's it! Starting using [classes utilities] or [CSS directives] in your app, and enjoy the speed! ⚡️
现在可以在你的应用中开始使用 [classes工具类] 或者 [CSS指令] ,感受一下速度吧!⚡️
satrong marked this conversation as resolved.
Show resolved Hide resolved

> If you are migrating from Tailwind CSS, also check out the [_Migration_ section][migration]
> 如果你是从 Tailwind CSS 迁移过来,也可以查看 [_迁移_ 部分][migration]
satrong marked this conversation as resolved.
Show resolved Hide resolved

## Supports
## 支持 {#supports}

### TypeScript
### TypeScript {#typeScript}

Enable TypeScript for your `tailwind.config.js`? Sure, why not?
需要为你的 `tailwind.config.js` 启用TypeScript?当然可以。
satrong marked this conversation as resolved.
Show resolved Hide resolved

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

```ts
// tailwind.config.ts
Expand All @@ -84,33 +84,33 @@ export default defineConfig({
})
```

### Pug Support
### Pug Support {#pug-support}

It will automatically enable Pug support for `.pug` and Vue SFC when dependency `pug` is found in the workspace.
当在工作区中找到依赖项 `pug` 时,它将自动启用对 `.pug` Vue SFC 的 Pug 支持。
satrong marked this conversation as resolved.
Show resolved Hide resolved

### "Design in DevTools"
### "Design in DevTools" {#design-in-devtools}
satrong marked this conversation as resolved.
Show resolved Hide resolved

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.
satrong marked this conversation as resolved.
Show resolved Hide resolved

But unfortunately, **we are here to BREAK the limitation** 😎 See the [video demo](https://twitter.com/antfu7/status/1372244287975387145).
但遗憾的是 **我们在这里打破了限制** 😎 查看 [视频示例](https://twitter.com/antfu7/status/1372244287975387145).
satrong marked this conversation as resolved.
Show resolved Hide resolved

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

```js
import 'virtual:windi-devtools'
```

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

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 :)
不用担心最后的打包,在生产版本中 `virtualwindi-devtools` 将是一个空模块,你无需为此额外的事情:)
satrong marked this conversation as resolved.
Show resolved Hide resolved

> ⚠️ 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.
> ⚠️ 在使用 [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) 来检测类的更改时请谨慎使用。这意味着你的手动更改和脚本所做的更改都将被检测到并包含在样式表中。**使用动态构造classes**(不一定)时,这可能会导致开发和生产版本之间产生一些不一致。我们建议你将动态部分添加到 `safelist` 中,或在可能的情况下为您的生产版本设置UI回归测试。
satrong marked this conversation as resolved.
Show resolved Hide resolved

## Configuration
## 配置 {#configuration}

### Preflight (style reseting)
### 预检 (样式重置) {#preflight}
satrong marked this conversation as resolved.
Show resolved Hide resolved

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

```ts
// windi.config.ts
Expand All @@ -121,16 +121,16 @@ export default defineConfig({
})
```

### Safelist
### Safelist {#safelist}
satrong marked this conversation as resolved.
Show resolved Hide resolved

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
默认情况下,我们会静态扫描你的源码,并查找所有使用的工具类,然后按需生成相应的CSS。但存在一些局限性:在runtime时工具类无法有效匹配,例如
satrong marked this conversation as resolved.
Show resolved Hide resolved

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

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

```ts
// windi.config.ts
Expand All @@ -141,7 +141,7 @@ export default defineConfig({
})
```

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

```ts
// windi.config.ts
Expand All @@ -159,10 +159,9 @@ export default defineConfig({
})
```

### Scanning
### Scanning {#scanning}
satrong marked this conversation as resolved.
Show resolved Hide resolved

On server start, `vite-plugin-windicss` will scan your source code and extract the utility usages. By default,
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:
在服务启动时,`vite-plugin-windicss` 将扫描你的源码并提取使用的工具类。默认情况下,只有 `src/` 下带有 `vue,html,mdx,pug,jsx,tsx` 扩展名的文件被包含。如果你想启用扫描其他位置的文件类型,则可以通过以下方式进行配置:
satrong marked this conversation as resolved.
Show resolved Hide resolved

```ts
// windi.config.js
Expand All @@ -176,7 +175,7 @@ export default defineConfig({
})
```

Or in plugin options:
或者在插件选项中:

```ts
// vite.config.js
Expand All @@ -186,18 +185,18 @@ export default defineConfig({
plugins: [
WindiCSS({
scan: {
dirs: ['.'], // all files in the cwd
fileExtensions: ['vue', 'js', 'ts'], // also enabled scanning for js/ts
dirs: ['.'], // 当前目录下所有文件
fileExtensions: ['vue', 'js', 'ts'], // 同时启用扫描vue/js/ts
},
}),
],
})
```


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

Enabled it by
通过以下配置启用

```ts
// windi.config.ts
Expand All @@ -206,7 +205,7 @@ export default {
}
```

And use them as you would like:
然后这样使用:

```html
<button
Expand All @@ -220,9 +219,9 @@ And use them as you would like:
</button>
```

#### Prefix
#### Prefix {#preflight-style-reseting}
KimYangOfCat marked this conversation as resolved.
Show resolved Hide resolved

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

```ts
// windi.config.ts
Expand All @@ -245,7 +244,7 @@ export default {
</button>
```

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

```ts
// windi.config.ts
Expand All @@ -260,11 +259,11 @@ export default {
}
```

### Layers Ordering
### 模块顺序 {#layers-ordering}

> Supported from v0.14.x
> 从v0.14.x开始支持

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:
默认情况下,导入 `virtual:windi.css` 将会按顺序导入全部三个模块 `base - components - utilities` 。如果你想更好地控制顺序,可以通过以下方法将它们分开:

```diff
- import 'virtual:windi.css'
Expand All @@ -273,7 +272,7 @@ By default, importing `virtual:windi.css` will import all the three layers with
+ import 'virtual:windi-utilities.css'
```

You can also make your custom css been able to be overridden by certain layers:
你还可以使自定义的css能够被某些模块覆盖:
satrong marked this conversation as resolved.
Show resolved Hide resolved

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

### More
### 更多 {#more}

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

## Caveats
## 注意事项 {#caveats}

### Scoped Style
### Scoped Style {#scoped-style}

You will need to **set `transformCSS: 'pre'` to get Scoped Style work**.
需要 **设置 `transformCSS'pre'` 才能使 Scoped Style 工作**。
satrong marked this conversation as resolved.
Show resolved Hide resolved

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

## Example
## 示例 {#example}

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)
请参阅 [示例](https://github.com/windicss/vite-plugin-windicss/blob/main/examples) 中的 *react**vue* *vue with pug* 示例项目,或 [`Vitesse`](https://github.com/antfu/vitesse)

---

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

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

```js
// svelte.config.js
Expand All @@ -324,17 +323,17 @@ const config = {
export default config
```

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

```html
<!-- $layout.svelte -->
<script>
import "virtual:windi.css"

// if you want to enable windi devtools
// 如果你需要启用 windi devtools
satrong marked this conversation as resolved.
Show resolved Hide resolved
import { browser } from "$app/env";
if (browser) import("virtual:windi-devtools")
// ...
</script>
<!-- ...rest of $layout.svelte -->
<!-- ...其他的 $layout.svelte 代码 -->
```