|
| 1 | +## 如何使用 |
| 2 | + |
| 3 | +### 0. 创建项目 |
| 4 | + |
| 5 | +使用最新版本 `Hbuilderx` 创建一个 `uni-app x` 项目,然后执行 |
| 6 | + |
| 7 | +```bash npm2yarn |
| 8 | +npm init -y |
| 9 | +``` |
| 10 | + |
| 11 | +初始化一个 `package.json` 文件 (当然你也可以手动创建) |
| 12 | + |
| 13 | +### 1. [安装并引入 tailwindcss3](/docs/quick-start/install) |
| 14 | + |
| 15 | + |
| 16 | +```bash npm2yarn |
| 17 | +# 安装 tailwindcss@3 版本的依赖 |
| 18 | +npm i -D tailwindcss@3 postcss autoprefixer |
| 19 | +``` |
| 20 | + |
| 21 | +```bash npm2yarn |
| 22 | +# 初始化一个 tailwind.config.js 文件 |
| 23 | +npx tailwindcss init |
| 24 | +``` |
| 25 | + |
| 26 | +然后,在你的根目录中的 `App.uvue` 中引入 `tailwindcss` 使它在应用全局生效 |
| 27 | + |
| 28 | +```html title="App.uvue" |
| 29 | +<style> |
| 30 | +@tailwind base; |
| 31 | +@tailwind components; |
| 32 | +@tailwind utilities; |
| 33 | +</style> |
| 34 | +``` |
| 35 | + |
| 36 | +### 2. [安装 weapp-tailwindcss](/docs/quick-start/this-plugin) |
| 37 | + |
| 38 | +在项目目录下,执行: |
| 39 | + |
| 40 | +```bash npm2yarn |
| 41 | +npm i -D weapp-tailwindcss |
| 42 | +``` |
| 43 | + |
| 44 | +然后把下列脚本,添加进你的 `package.json` 的 `scripts` 字段里: |
| 45 | + |
| 46 | +```json title="package.json" |
| 47 | + "scripts": { |
| 48 | + // highlight-next-line |
| 49 | + "postinstall": "weapp-tw patch" |
| 50 | + } |
| 51 | +``` |
| 52 | + |
| 53 | +想知道原因的同学可以查看 [**这个链接**](https://tw.icebreaker.top/docs/quick-start/this-plugin) |
| 54 | + |
| 55 | +### 3. [uni-app-x 注册 weapp-tailwindcss](/docs/quick-start/frameworks/uni-app-x) |
| 56 | + |
| 57 | +#### 0. 创建工具类 |
| 58 | + |
| 59 | +在项目中创建 `shared.js` 文件,用于存放一些工具函数: |
| 60 | + |
| 61 | +```js title="shared.js" |
| 62 | +const path = require('node:path') |
| 63 | + |
| 64 | +function r(...args) { |
| 65 | + return path.resolve(__dirname, ...args) |
| 66 | +} |
| 67 | + |
| 68 | +module.exports = { |
| 69 | + r, |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +#### 1. 注册插件 |
| 74 | + |
| 75 | +创建 `vite.config.ts` 文件,注册插件: |
| 76 | + |
| 77 | +> 这里特别注意 `uniAppX` 是从 `weapp-tailwindcss/presets` 这个预设中导出的 |
| 78 | +
|
| 79 | +```js title="vite.config.ts" |
| 80 | +import { defineConfig } from 'vite'; |
| 81 | +import uni from '@dcloudio/vite-plugin-uni'; |
| 82 | +import { UnifiedViteWeappTailwindcssPlugin } from 'weapp-tailwindcss/vite' |
| 83 | +import { r } from './shared' |
| 84 | +import { uniAppX } from 'weapp-tailwindcss/presets' |
| 85 | +import tailwindcss from 'tailwindcss' |
| 86 | + |
| 87 | +export default defineConfig({ |
| 88 | + plugins: [ |
| 89 | + uni(), |
| 90 | + UnifiedViteWeappTailwindcssPlugin( |
| 91 | + uniAppX({ |
| 92 | + base: __dirname, |
| 93 | + rem2rpx: true, |
| 94 | + }), |
| 95 | + ), |
| 96 | + ], |
| 97 | + css: { |
| 98 | + postcss: { |
| 99 | + plugins: [ |
| 100 | + tailwindcss({ |
| 101 | + config: r('./tailwind.config.js'), |
| 102 | + }), |
| 103 | + ] |
| 104 | + } |
| 105 | + } |
| 106 | +}); |
| 107 | +``` |
| 108 | + |
| 109 | +#### 2. 更改 tailwindcss 配置 |
| 110 | + |
| 111 | +使用绝对路径,包括所有的提取文件 |
| 112 | + |
| 113 | +```js title="tailwind.config.js" |
| 114 | +const { r } = require('./shared') |
| 115 | + |
| 116 | +/** @type {import('tailwindcss').Config} */ |
| 117 | +module.exports = { |
| 118 | + content: [ |
| 119 | + r('./pages/**/*.{uts,uvue}'), |
| 120 | + r('./components/**/*.{uts,uvue}') |
| 121 | + ], |
| 122 | + corePlugins: { |
| 123 | + preflight: false, |
| 124 | + }, |
| 125 | +} |
| 126 | + |
| 127 | +``` |
| 128 | + |
| 129 | +假如你需要从更多的目录中提取 `CSS Token`,你需要添加更多的 `glob` 表达式在 `content` 配置中。 |
| 130 | + |
| 131 | +--- |
| 132 | + |
| 133 | +在完成这 3 步之后, `uni-app x` 就集成 `tailwindcss` 原子化样式解决方案。 |
0 commit comments