weapp-tailwindcss 2.3.3 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -596
- package/dist/babel/index.d.ts +3 -4
- package/dist/cli.js +7 -7
- package/dist/cli.mjs +7 -7
- package/dist/gulp/index.d.ts +1 -1
- package/dist/gulp.js +8 -8
- package/dist/gulp.mjs +8 -8
- package/dist/index.js +8 -8
- package/dist/index.mjs +8 -8
- package/dist/js/index.d.ts +1 -1
- package/dist/{options-3a695e84.js → options-450d76c2.js} +179 -195
- package/dist/{options-ed6f4a3a.mjs → options-5c083791.mjs} +179 -195
- package/dist/postcss/plugin.d.ts +2 -2
- package/dist/{postcss-dc9eeafc.js → postcss-0c103b15.js} +5 -6
- package/dist/{postcss-1f9a5153.mjs → postcss-e3fcf6f1.mjs} +5 -6
- package/dist/postcss.js +2 -2
- package/dist/postcss.mjs +2 -2
- package/dist/replace.d.ts +2 -3
- package/dist/replace.js +2 -2
- package/dist/replace.mjs +3 -3
- package/dist/{shared-eae1dc7a.mjs → shared-89ea7f77.mjs} +59 -60
- package/dist/{shared-c2953d9d.js → shared-9744fdd1.js} +59 -60
- package/dist/tailwindcss/patcher.d.ts +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/utils.d.ts +1 -3
- package/dist/vite.js +7 -7
- package/dist/vite.mjs +7 -7
- package/dist/webpack/BaseUnifiedPlugin/v5.d.ts +1 -1
- package/dist/webpack.js +9 -9
- package/dist/webpack.mjs +9 -9
- package/dist/wxml/index.d.ts +2 -3
- package/package.json +13 -17
package/README.md
CHANGED
|
@@ -1,42 +1,60 @@
|
|
|
1
|
-

|
|
2
1
|
|
|
3
|
-
|
|
2
|
+
<p align="center">
|
|
3
|
+
|
|
4
|
+
<a href="https://weapp-tw.icebreaker.top">
|
|
5
|
+
|
|
6
|
+
<img src="./assets/logo.png" alt="weapp-tailwindcss-logo" width="128">
|
|
7
|
+
</a>
|
|
8
|
+
|
|
9
|
+
<br>
|
|
10
|
+
|
|
11
|
+
<h1 align="center">weapp-tailwindcss</h1>
|
|
12
|
+
|
|
13
|
+
</p>
|
|
14
|
+
<!-- <p align="center">
|
|
15
|
+
<a href="https://weapp-tw.icebreaker.top#gh-light-mode-only">
|
|
16
|
+
<img src="./assets/logo-text-light.png#gh-light-mode-only" alt="weapp-tailwindcss-logo" width="300">
|
|
17
|
+
</a>
|
|
18
|
+
<a href="https://weapp-tw.icebreaker.top#gh-dark-mode-only">
|
|
19
|
+
<img src="./assets/logo-text-dark.png#gh-dark-mode-only" alt="weapp-tailwindcss-logo" width="300">
|
|
20
|
+
</a>
|
|
21
|
+
<br> -->
|
|
4
22
|
|
|
5
23
|

|
|
6
|
-

|
|
25
|
+

|
|
7
26
|

|
|
8
27
|
[](https://github.com/sonofmagic/weapp-tailwindcss-webpack-plugin/actions/workflows/test.yml)
|
|
9
28
|
[](https://codecov.io/gh/sonofmagic/weapp-tailwindcss-webpack-plugin)
|
|
10
29
|
|
|
11
|
-
>
|
|
12
|
-
|
|
13
|
-
\[[国内部署的文档地址](https://weapp-tw.icebreaker.top)\] \| \[[1.x文档]('./v1.md')\]
|
|
14
|
-
|
|
15
|
-
- [
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
- [
|
|
26
|
-
- [
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
- [
|
|
31
|
-
- [
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
- [Bugs \& Issues](#bugs--issues)
|
|
30
|
+
> `小程序` + `tailwindcss` 全方面解决方案
|
|
31
|
+
|
|
32
|
+
\[[国内部署的文档地址](https://weapp-tw.icebreaker.top)\] \| \[[备用Github Page](https://sonofmagic.github.io/weapp-tailwindcss-webpack-plugin/)\] \| \[[1.x文档]('./v1.md')\]
|
|
33
|
+
|
|
34
|
+
- [Tips](#tips)
|
|
35
|
+
- [特性](#特性)
|
|
36
|
+
- [插件介绍](#插件介绍)
|
|
37
|
+
- [安装与使用方式](#安装与使用方式)
|
|
38
|
+
- [从 v1 迁移到 v2](#从-v1-迁移到-v2)
|
|
39
|
+
- [配置项参考](#配置项参考)
|
|
40
|
+
- [使用tailwindcss任意值(arbitrary values)](#使用tailwindcss任意值arbitrary-values)
|
|
41
|
+
- [常见问题](#常见问题)
|
|
42
|
+
- [变更日志](#变更日志)
|
|
43
|
+
- [Related projects](#related-projects)
|
|
44
|
+
- [CLI 工具](#cli-工具)
|
|
45
|
+
- [模板 template](#模板-template)
|
|
46
|
+
- [如何选择?](#如何选择)
|
|
47
|
+
- [使用`uni-app cli`进行构建 `vscode`开发](#使用uni-app-cli进行构建-vscode开发)
|
|
48
|
+
- [使用`hbuilderx` 进行构建和开发](#使用hbuilderx-进行构建和开发)
|
|
49
|
+
- [使用`tarojs`进行构建 `vscode`开发](#使用tarojs进行构建-vscode开发)
|
|
50
|
+
- [原生小程序开发模板](#原生小程序开发模板)
|
|
51
|
+
- [tailwindcss plugin](#tailwindcss-plugin)
|
|
52
|
+
- [tailwindcss preset](#tailwindcss-preset)
|
|
53
|
+
- [Bugs \& Issues](#bugs--issues)
|
|
54
|
+
|
|
55
|
+
## Tips
|
|
56
|
+
|
|
57
|
+
自从 `2.3.3` 版本开始,我发布了一个额外的包叫 `weapp-tailwindcss`,它和 `weapp-tailwindcss-webpack-plugin` 代码版本完全一致,且保持发布版本的同步。以后可以都去安装 `weapp-tailwindcss` 这个包(当然现在 `webpack-plugin` 这个包也不会废弃,也会时刻保持版本的同步)。为什么要这么做的原因,主要是因为 `weapp-tailwindcss-webpack-plugin` 这个名字,已经不适合描述现在这种,多插件并存的状态了,为了以后的发展改个名字哈哈。
|
|
40
58
|
|
|
41
59
|
## 特性
|
|
42
60
|
|
|
@@ -44,583 +62,36 @@
|
|
|
44
62
|
| --------------------------------------------------- | ----------------------------------------------- |
|
|
45
63
|
|  |  |
|
|
46
64
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
这个版本新增了 `UnifiedWebpackPluginV5`
|
|
50
|
-
和 `UnifiedViteWeappTailwindcssPlugin` 这种 `Unified` 开头的插件。
|
|
65
|
+
核心插件支持 `webpack`/`vite`/`gulp`进行打包,涵盖了市面上几乎所有的主流开发小程序的框架。
|
|
51
66
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
相比`v1`版本只有处理`wxss`,`wxml`静态`class`的能力,使用`v2`版本新的插件,你再也不需要在 `js` 里引入并调用标记方法 `replaceJs`了!`2.x` 插件有精准转化 `js`/`jsx` 的能力,大大提升了 `taro` 这种动态模板框架的开发体验。
|
|
67
|
+
这些插件能够自动识别并精确处理所有 `tailwindcss` 的工具类来适配小程序环境。同时这些插件还有对`tailwindcss`生成的工具类名,进行压缩和混淆的能力。这个能力可以缩短`css`选择器的长度,减小生成样式的体积,同时让生产环境中的类名变得不可阅读。
|
|
55
68
|
|
|
56
69
|
### 插件介绍
|
|
57
70
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
目前,这个方案只支持 `tailwindcss v3.x.x` 版本和 `webpack5`。同时这个方案依赖 `monkey patch`,所以你应该把
|
|
61
|
-
|
|
62
|
-
```json
|
|
63
|
-
"scripts": {
|
|
64
|
-
+ "postinstall": "weapp-tw patch"
|
|
65
|
-
}
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
加入你的 `package.json`。当然在安装或者更新 `tailwindcss` 后,手动执行 `npx weapp-tw patch` 效果也是一样的,看到 `patch .... successfully` 表示成功。
|
|
69
|
-
|
|
70
|
-
`UnifiedViteWeappTailwindcssPlugin` 为 `vite` 专用插件,配置项和使用方式也是和上面一致的。
|
|
71
|
-
|
|
72
|
-
## Usage
|
|
73
|
-
|
|
74
|
-
### 1. 安装配置 tailwindcss
|
|
75
|
-
|
|
76
|
-
<details><summary>安装 tailwindcss</summary><br/>
|
|
77
|
-
|
|
78
|
-
#### 1. 安装 `tailwindcss`
|
|
79
|
-
|
|
80
|
-
```sh
|
|
81
|
-
# npm / yarn / pnpm
|
|
82
|
-
npm install -D tailwindcss postcss autoprefixer
|
|
83
|
-
npx tailwindcss init
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
#### 2. 把 `tailwindcss` 注册进 `postcss.config.js`
|
|
87
|
-
|
|
88
|
-
```js
|
|
89
|
-
// postcss.config.js
|
|
90
|
-
// 假如你使用的框架/工具不支持 postcss.config.js,则可以使用内联的写法
|
|
91
|
-
module.exports = {
|
|
92
|
-
plugins: {
|
|
93
|
-
tailwindcss: {},
|
|
94
|
-
autoprefixer: {},
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
#### 3. 配置 `tailwind.config.js`
|
|
100
|
-
|
|
101
|
-
```js
|
|
102
|
-
/** @type {import('tailwindcss').Config} */
|
|
103
|
-
module.exports = {
|
|
104
|
-
// 这里给出了一份 uni-app /taro 通用示例,具体要根据你自己项目的目录进行配置
|
|
105
|
-
// 不在 content 包括的文件内,不会生成工具类
|
|
106
|
-
content: ['./public/index.html', './src/**/*.{html,js,ts,jsx,tsx,vue}'],
|
|
107
|
-
|
|
108
|
-
corePlugins: {
|
|
109
|
-
// 不需要 preflight,因为这主要是给 h5 的,如果你要同时开发小程序和 h5 端,你应该使用环境变量来控制它
|
|
110
|
-
preflight: false
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
#### 4. 引入 `tailwindcss`
|
|
116
|
-
|
|
117
|
-
在你的项目入口引入 `tailwindcss`
|
|
118
|
-
|
|
119
|
-
比如 `uni-app` 的 `App.vue`
|
|
120
|
-
|
|
121
|
-
```html
|
|
122
|
-
<style>
|
|
123
|
-
@tailwind base;
|
|
124
|
-
@tailwind utilities;
|
|
125
|
-
/* 使用 scss */
|
|
126
|
-
/* @import 'tailwindcss/base'; */
|
|
127
|
-
/* @import 'tailwindcss/utilities'; */
|
|
128
|
-
</style>
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
又或者 `Taro` 的 `app.scss`
|
|
132
|
-
|
|
133
|
-
```scss
|
|
134
|
-
@import 'tailwindcss/base';
|
|
135
|
-
@import 'tailwindcss/utilities';
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
然后在 `app.ts` 里引入
|
|
139
|
-
|
|
140
|
-
> Q&A: 为什么没有引入 `tailwindcss/components`? 是因为里面默认存放的是 pc 端自适应相关的样式,对小程序环境来说没有用处。如果你有 @layer components 相关的工具类需要使用,可以引入。
|
|
141
|
-
|
|
142
|
-
<br/></details>
|
|
143
|
-
|
|
144
|
-
### 2. `rem` 转 `px` 或 `rpx`
|
|
145
|
-
|
|
146
|
-
<details><summary>配置tailwindcss单位转化</summary><br/>
|
|
147
|
-
|
|
148
|
-
#### 1. 两种转化方式(二者选其一即可)
|
|
149
|
-
|
|
150
|
-
假如你想要把项目里,所有满足条件的 `rem` 都转化成 `rpx`,那么 `postcss plugin`: [postcss-rem-to-responsive-pixel](https://www.npmjs.com/package/postcss-rem-to-responsive-pixel) 适合你。
|
|
151
|
-
|
|
152
|
-
假如你想缩小一下范围,只把 `tailwindcss` 中默认的工具类的单位(非`jit`生成的`class`),从 `rem` 转变为 `rpx`,那么 `tailwindcss preset`: [tailwindcss-rem2px-preset](https://www.npmjs.com/package/tailwindcss-rem2px-preset) 适合你。
|
|
153
|
-
|
|
154
|
-
#### 2. `postcss-rem-to-responsive-pixel`
|
|
155
|
-
|
|
156
|
-
```sh
|
|
157
|
-
npm i -D postcss-rem-to-responsive-pixel
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
安装好之后,把它注册进你的 `postcss.config.js` 即可:
|
|
161
|
-
|
|
162
|
-
```js
|
|
163
|
-
// postcss.config.js
|
|
164
|
-
module.exports = {
|
|
165
|
-
plugins: {
|
|
166
|
-
tailwindcss: {},
|
|
167
|
-
autoprefixer: {},
|
|
168
|
-
'postcss-rem-to-responsive-pixel': {
|
|
169
|
-
// 32 意味着 1rem = 32rpx
|
|
170
|
-
rootValue: 32,
|
|
171
|
-
// 默认所有属性都转化
|
|
172
|
-
propList: ['*'],
|
|
173
|
-
// 转化的单位,可以变成 px / rpx
|
|
174
|
-
transformUnit: 'rpx',
|
|
175
|
-
},
|
|
176
|
-
},
|
|
177
|
-
};
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
#### 3. `tailwindcss-rem2px-preset`
|
|
181
|
-
|
|
182
|
-
```sh
|
|
183
|
-
npm i -D tailwindcss-rem2px-preset
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
然后在 `tailwind.config.js` 中,添加:
|
|
187
|
-
|
|
188
|
-
```js
|
|
189
|
-
// tailwind.config.js
|
|
190
|
-
|
|
191
|
-
module.exports = {
|
|
192
|
-
presets: [
|
|
193
|
-
require('tailwindcss-rem2px-preset').createPreset({
|
|
194
|
-
// 32 意味着 1rem = 32rpx
|
|
195
|
-
fontSize: 32,
|
|
196
|
-
// 转化的单位,可以变成 px / rpx
|
|
197
|
-
unit: 'rpx'
|
|
198
|
-
})
|
|
199
|
-
],
|
|
200
|
-
// ...
|
|
201
|
-
}
|
|
202
|
-
```
|
|
203
|
-
|
|
204
|
-
这样即可完成 `tailwindcss` 默认 `rem` 单位,转化 `rpx` 的配置了。
|
|
205
|
-
|
|
206
|
-
<br/></details>
|
|
207
|
-
|
|
208
|
-
### 3. 安装这个插件
|
|
209
|
-
|
|
210
|
-
```sh
|
|
211
|
-
# npm / yarn /pnpm
|
|
212
|
-
npm i -D weapp-tailwindcss-webpack-plugin
|
|
213
|
-
# 可以执行一下 patch 方法
|
|
214
|
-
npx weapp-tw patch
|
|
215
|
-
```
|
|
216
|
-
|
|
217
|
-
然后把下列脚本,添加进你的 `package.json` 的 `scripts` 字段里:
|
|
218
|
-
|
|
219
|
-
```json
|
|
220
|
-
"scripts": {
|
|
221
|
-
+ "postinstall": "weapp-tw patch"
|
|
222
|
-
}
|
|
223
|
-
```
|
|
224
|
-
|
|
225
|
-
#### 各个框架注册的方式
|
|
226
|
-
|
|
227
|
-
<details><summary>uni-app (vue2/3)</summary><br/>
|
|
228
|
-
|
|
229
|
-
**在创建uni-app项目时,请选择uni-app alpha版本**
|
|
230
|
-
|
|
231
|
-
```sh
|
|
232
|
-
vue create -p dcloudio/uni-preset-vue#alpha my-alpha-project
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
这是因为默认创建的版本还是 `@vue/cli 4.x` 的版本,使用 `webpack4` 和 `postcss7`,而 `alpha` 版本使用 `@vue/cli 5.x` 即 `webpack5` 和 `postcss8`,这可以使用最新版本的 `tailwindcss` 和本插件。
|
|
236
|
-
|
|
237
|
-
```js
|
|
238
|
-
// 在 vue.config.js 里注册
|
|
239
|
-
const { UnifiedWebpackPluginV5 } = require('weapp-tailwindcss-webpack-plugin/webpack')
|
|
240
|
-
/**
|
|
241
|
-
* @type {import('@vue/cli-service').ProjectOptions}
|
|
242
|
-
*/
|
|
243
|
-
const config = {
|
|
244
|
-
// some option...
|
|
245
|
-
configureWebpack: (config) => {
|
|
246
|
-
config.plugins.push(
|
|
247
|
-
new UnifiedWebpackPluginV5({
|
|
248
|
-
appType: 'uni-app'
|
|
249
|
-
})
|
|
250
|
-
)
|
|
251
|
-
}
|
|
252
|
-
// other option...
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
module.exports = config
|
|
256
|
-
```
|
|
257
|
-
|
|
258
|
-
<br/></details>
|
|
259
|
-
|
|
260
|
-
<details><summary>uni-app vite(vue3)</summary><br/>
|
|
261
|
-
|
|
262
|
-
```js
|
|
263
|
-
// vite.config.[jt]s
|
|
264
|
-
import { UnifiedViteWeappTailwindcssPlugin as uvwt } from 'weapp-tailwindcss-webpack-plugin/vite'
|
|
265
|
-
|
|
266
|
-
const vitePlugins = [uni(),uvwt()]
|
|
267
|
-
|
|
268
|
-
export default defineConfig({
|
|
269
|
-
plugins: vitePlugins,
|
|
270
|
-
// 假如 postcss.config.js 不起作用,请使用内联 postcss Latset
|
|
271
|
-
// css: {
|
|
272
|
-
// postcss: {
|
|
273
|
-
// plugins: postcssPlugins,
|
|
274
|
-
// },
|
|
275
|
-
// },
|
|
276
|
-
});
|
|
277
|
-
|
|
278
|
-
```
|
|
279
|
-
|
|
280
|
-
<br/></details>
|
|
281
|
-
|
|
282
|
-
<details><summary>Taro v3 (all frameworks)</summary><br/>
|
|
283
|
-
|
|
284
|
-
**在使用Taro时,检查一下把 config/index 的配置项 compiler 设置为 'webpack5'**
|
|
285
|
-
|
|
286
|
-
```js
|
|
287
|
-
// config/index
|
|
288
|
-
const { UnifiedWebpackPluginV5 } = require('weapp-tailwindcss-webpack-plugin/webpack')
|
|
289
|
-
|
|
290
|
-
{
|
|
291
|
-
mini: {
|
|
292
|
-
webpackChain(chain, webpack) {
|
|
293
|
-
chain.merge({
|
|
294
|
-
plugin: {
|
|
295
|
-
install: {
|
|
296
|
-
plugin: UnifiedWebpackPluginV5,
|
|
297
|
-
args: [{
|
|
298
|
-
appType: 'taro'
|
|
299
|
-
}]
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
})
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
```
|
|
307
|
-
|
|
308
|
-
<br/></details>
|
|
309
|
-
|
|
310
|
-
<details><summary>mpx (原生增强)</summary><br/>
|
|
311
|
-
|
|
312
|
-
在 `vue.config.js` 中注册:
|
|
313
|
-
|
|
314
|
-
```js
|
|
315
|
-
// vue.config.js
|
|
316
|
-
const { defineConfig } = require('@vue/cli-service')
|
|
317
|
-
const { UnifiedWebpackPluginV5 } = require('weapp-tailwindcss-webpack-plugin/webpack')
|
|
318
|
-
|
|
319
|
-
module.exports = defineConfig({
|
|
320
|
-
// other options
|
|
321
|
-
configureWebpack(config) {
|
|
322
|
-
config.plugins.push(new UnifiedWebpackPluginV5({
|
|
323
|
-
appType: 'mpx'
|
|
324
|
-
}))
|
|
325
|
-
}
|
|
326
|
-
})
|
|
327
|
-
|
|
328
|
-
```
|
|
329
|
-
|
|
330
|
-
<br/></details>
|
|
331
|
-
|
|
332
|
-
<details><summary>rax (react)</summary><br/>
|
|
333
|
-
|
|
334
|
-
在根目录下创建一个 `build.plugin.js` 文件,然后在 `build.json` 中注册:
|
|
335
|
-
|
|
336
|
-
```json
|
|
337
|
-
{
|
|
338
|
-
"plugins": [
|
|
339
|
-
"./build.plugin.js"
|
|
340
|
-
],
|
|
341
|
-
}
|
|
342
|
-
```
|
|
343
|
-
|
|
344
|
-
回到 `build.plugin.js`
|
|
345
|
-
|
|
346
|
-
```js
|
|
347
|
-
// build.plugin.js
|
|
348
|
-
const { UnifiedWebpackPluginV5 } = require('weapp-tailwindcss-webpack-plugin/webpack')
|
|
349
|
-
module.exports = ({ context, onGetWebpackConfig }) => {
|
|
350
|
-
onGetWebpackConfig((config) => {
|
|
351
|
-
config.plugin('UnifiedWebpackPluginV5').use(UnifiedWebpackPluginV5, [
|
|
352
|
-
{
|
|
353
|
-
appType: 'rax',
|
|
354
|
-
},
|
|
355
|
-
]);
|
|
356
|
-
});
|
|
357
|
-
};
|
|
358
|
-
|
|
359
|
-
```
|
|
360
|
-
|
|
361
|
-
<br/></details>
|
|
362
|
-
|
|
363
|
-
<details><summary>原生开发 (webpack5/gulp)</summary><br/>
|
|
364
|
-
|
|
365
|
-
#### webpack5
|
|
366
|
-
|
|
367
|
-
直接在 `webpack.config.js` 注册即可
|
|
368
|
-
|
|
369
|
-
```js
|
|
370
|
-
// webpack.config.js
|
|
371
|
-
plugins: [
|
|
372
|
-
new UnifiedWebpackPluginV5({
|
|
373
|
-
appType: 'native',
|
|
374
|
-
}),
|
|
375
|
-
],
|
|
376
|
-
```
|
|
377
|
-
|
|
378
|
-
#### gulp
|
|
379
|
-
|
|
380
|
-
这个配置稍微繁琐一些
|
|
381
|
-
|
|
382
|
-
```js
|
|
383
|
-
// gulpfile.js
|
|
384
|
-
|
|
385
|
-
const { createPlugins } = require('weapp-tailwindcss-webpack-plugin/gulp')
|
|
386
|
-
// 在 gulp 里使用,先使用 postcss 转化 css,触发 tailwindcss 运行,转化 transformWxss,然后再 transformJs, transformWxml
|
|
387
|
-
// createPlugins 参数 options 就是本插件的配置项
|
|
388
|
-
const { transformJs, transformWxml, transformWxss } = createPlugins()
|
|
389
|
-
|
|
390
|
-
// 参考顺序
|
|
391
|
-
// transformWxss
|
|
392
|
-
function sassCompile() {
|
|
393
|
-
return gulp
|
|
394
|
-
.src(paths.src.scssFiles)
|
|
395
|
-
.pipe(sass({ errLogToConsole: true, outputStyle: 'expanded' }).on('error', sass.logError))
|
|
396
|
-
.pipe(postcss())
|
|
397
|
-
.pipe(transformWxss())
|
|
398
|
-
.pipe(
|
|
399
|
-
rename({
|
|
400
|
-
extname: '.wxss'
|
|
401
|
-
})
|
|
402
|
-
)
|
|
403
|
-
.pipe(replace('.scss', '.wxss'))
|
|
404
|
-
.pipe(gulp.dest(paths.dist.baseDir))
|
|
405
|
-
}
|
|
406
|
-
// transformJs
|
|
407
|
-
function compileTsFiles() {
|
|
408
|
-
return gulp.src(paths.src.jsFiles, {}).pipe(plumber()).pipe(tsProject()).pipe(transformJs()).pipe(gulp.dest(paths.dist.baseDir))
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
// transformWxml
|
|
412
|
-
function copyWXML() {
|
|
413
|
-
return gulp.src(paths.src.wxmlFiles, {}).pipe(transformWxml()).pipe(gulp.dest(paths.dist.baseDir))
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
// 注意 sassCompile 在 copyWXML 和 compileTsFiles, 这是为了先触发 tailwindcss 处理,从而在运行时获取到上下文
|
|
417
|
-
const buildTasks = [cleanTmp, copyBasicFiles, sassCompile, copyWXML, compileTsFiles]
|
|
418
|
-
// 注册默认任务 (串行)
|
|
419
|
-
gulp.task('default', gulp.series(buildTasks))
|
|
420
|
-
```
|
|
71
|
+
从 `weapp-tailwindcss/webpack` 导出的`UnifiedWebpackPluginV5` 是一个核心插件,所有使用 `webpack` 进行打包的框架都可以使用它。
|
|
421
72
|
|
|
422
|
-
|
|
73
|
+
从 `weapp-tailwindcss/vite` 导出的`UnifiedViteWeappTailwindcssPlugin` 为 `vite` 专用插件,配置项和使用方式和 `webpack` 插件是一致的。
|
|
423
74
|
|
|
424
|
-
|
|
75
|
+
而我们的 `gulp` 插件方法,可以从 `weapp-tailwindcss/gulp` 导出。
|
|
425
76
|
|
|
426
|
-
|
|
77
|
+
目前,这些插件支持最新版本的 `tailwindcss v3.x.x` 版本和 `webpack5`,`vite` 和 `gulp`。
|
|
427
78
|
|
|
428
|
-
|
|
79
|
+
> 如果你还在使用 `tailwindcss@2` 版本,那你应该使用本插件的 `1.x` 版本。另外请确保你的 `nodejs` 版本 `>=16`。目前低于 `16` 的长期维护版本(`偶数版本`) 都已经结束了生命周期,建议安装 `nodejs` 的 `LTS`版本,详见 [nodejs/release](https://github.com/nodejs/release)
|
|
429
80
|
|
|
430
|
-
|
|
81
|
+
## [安装与使用方式](https://weapp-tw.icebreaker.top/docs/quick-start/install)
|
|
431
82
|
|
|
432
|
-
|
|
433
|
-
import vwt from 'weapp-tailwindcss-webpack-plugin/vite';
|
|
434
|
-
```
|
|
83
|
+
## [从 v1 迁移到 v2](https://weapp-tw.icebreaker.top/docs/migrations/v1)
|
|
435
84
|
|
|
436
|
-
|
|
85
|
+
<!-- ## [js文件内容中taiwlindcss类名的精确转化与忽略策略](https://weapp-tw.icebreaker.top/docs/options/comments) -->
|
|
437
86
|
|
|
438
|
-
|
|
439
|
-
// UnifiedViteWeappTailwindcssPlugin 就是新的插件
|
|
440
|
-
import { UnifiedViteWeappTailwindcssPlugin } from 'weapp-tailwindcss-webpack-plugin/vite';
|
|
441
|
-
```
|
|
87
|
+
## [配置项参考](https://weapp-tw.icebreaker.top/docs/options/)
|
|
442
88
|
|
|
443
|
-
|
|
89
|
+
## [使用tailwindcss任意值(arbitrary values)](https://tailwindcss.com/docs/adding-custom-styles#using-arbitrary-values)
|
|
444
90
|
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
```json
|
|
448
|
-
"scripts": {
|
|
449
|
-
+ "postinstall": "weapp-tw patch"
|
|
450
|
-
}
|
|
451
|
-
```
|
|
452
|
-
|
|
453
|
-
添加进你的 `package.json` 里,然后清除原先的打包缓存之后重新打包运行。
|
|
454
|
-
|
|
455
|
-
<!-- 所以用 `uni-app` 的,建议你使用 `@vue/cli5`版本,`taro` 则切换到 `webpack5`。 -->
|
|
456
|
-
|
|
457
|
-
## 精确转化与忽略
|
|
458
|
-
|
|
459
|
-
默认对所有 `jsx`,`js`,`wxml`,`wxss`中出现的`tailwindcss`运行时工具类进行转化,如果不需要转化可以使用 `/*weapp-tw ignore*/` 前置注释。
|
|
460
|
-
|
|
461
|
-
例如:
|
|
462
|
-
|
|
463
|
-
```js
|
|
464
|
-
<view :class="classArray">classArray</view>
|
|
465
|
-
const classArray = [
|
|
466
|
-
'text-[30rpx]',
|
|
467
|
-
/*weapp-tw ignore*/ 'bg-[#00ff00]'
|
|
468
|
-
]
|
|
469
|
-
```
|
|
470
|
-
|
|
471
|
-
此时只有 `'text-[30rpx]'` 会被转化,`'bg-[#00ff00]'`被忽视
|
|
472
|
-
|
|
473
|
-
另外有可能出现的问题,我也写进了 [常见问题](#常见问题) 中,可以进行参考。
|
|
474
|
-
<!-- ### HBuilderX 创建的项目
|
|
475
|
-
|
|
476
|
-
需要创建 `vite` 版本或者 `HBuilderX`最新`alpha`版,方式同上
|
|
477
|
-
|
|
478
|
-
### uni-app 构建成 `android/ios` app
|
|
479
|
-
|
|
480
|
-
[建议配置方式](./docs/uni-app-android-and-ios.md) -->
|
|
481
|
-
|
|
482
|
-
## Options 配置项
|
|
483
|
-
|
|
484
|
-
### htmlMatcher
|
|
485
|
-
|
|
486
|
-
类型: `((assetPath:string)=>boolean)`\|`string`\|`string[]`
|
|
487
|
-
描述: 匹配 `wxml`等等模板进行处理的方法
|
|
488
|
-
|
|
489
|
-
### cssMatcher
|
|
490
|
-
|
|
491
|
-
类型: `((assetPath:string)=>boolean)`\|`string`\|`string[]`
|
|
492
|
-
描述: 匹配 `wxss`等等样式文件的方法
|
|
493
|
-
|
|
494
|
-
### jsMatcher
|
|
495
|
-
|
|
496
|
-
类型: `((assetPath:string)=>boolean)`\|`string`\|`string[]`
|
|
497
|
-
描述: 匹配 `js`文件进行处理的方法,用于`jsx`相关模板
|
|
498
|
-
|
|
499
|
-
### mainCssChunkMatcher
|
|
500
|
-
|
|
501
|
-
类型: `(assetPath:string)=>boolean`
|
|
502
|
-
描述: 匹配 `tailwindcss jit` 生成的核心 `css chunk` 的方法
|
|
503
|
-
|
|
504
|
-
### customRuleCallback
|
|
505
|
-
|
|
506
|
-
类型: `(node: Postcss.Rule, options: Readonly<RequiredStyleHandlerOptions>) => void`
|
|
507
|
-
描述: 可根据 Postcss walk 自由定制处理方案的 callback 方法
|
|
508
|
-
|
|
509
|
-
### disabled
|
|
510
|
-
|
|
511
|
-
类型: `boolean`
|
|
512
|
-
描述: 是否禁用该插件,默认为 `false`,一般用于多平台构建,有些平台比如 `h5` 不需要开启此插件,所以可以根据环境变量进行禁用。
|
|
513
|
-
|
|
514
|
-
### cssPreflightRange
|
|
515
|
-
|
|
516
|
-
类型: `'view'` \| `'all'`
|
|
517
|
-
描述: 全局`dom`选择器,只有在这个选择器作用范围内的`dom`会被注入 `cssPreflight` 的变量和默认样式。默认为 `'view'` 即只对所有的 `view` 和伪元素生效,想要对所有的元素生效,可切换为 `'all'`,此时需要自行处理和客户端默认样式的冲突
|
|
518
|
-
|
|
519
|
-
### replaceUniversalSelectorWith
|
|
520
|
-
|
|
521
|
-
类型: `string` \| `false`
|
|
522
|
-
描述: 把`css`中的全局选择器 **`*`** 替换为指定值,默认替换为 `'view'`,设置为 `false` 时不进行替换,此时小程序会由于不认识`*`选择器而报错
|
|
523
|
-
|
|
524
|
-
### customAttributes
|
|
525
|
-
|
|
526
|
-
类型: `Record<string, string | Regexp | (string | Regexp)[]>`
|
|
527
|
-
描述: **这是一个重要的配置!**
|
|
528
|
-
|
|
529
|
-
它可以自定义`wxml`标签上的`attr`转化属性。默认转化所有的`class`和`hover-class`,这个`Map`的 `key`为匹配标签,`value`为属性字符串或者匹配正则数组。如果你想要增加转化的属性,你可以添加 `*`: `(string | Regexp)[]` 这样的键值对,使其中属性的转化,在所有标签上生效,更复杂的情况,可以传一个Map实例。
|
|
530
|
-
|
|
531
|
-
假如你要把 `className` 通过组件的prop传递给子组件,又或者想要自定义转化的标签属性时,需要用到此配置,案例详见:[issue#134](https://github.com/sonofmagic/weapp-tailwindcss-webpack-plugin/issues/134) [issue#129](https://github.com/sonofmagic/weapp-tailwindcss-webpack-plugin/issues/129)
|
|
532
|
-
|
|
533
|
-
### customReplaceDictionary
|
|
534
|
-
|
|
535
|
-
类型: `'simple' | 'complex' | Record<string, string>`
|
|
536
|
-
描述: 默认为 `'complex'` 模式,这个配置项,用来自定义转化`class`名称字典,你可以使用这个选项来简化生成的`class`
|
|
537
|
-
|
|
538
|
-
插件中内置了`'simple'`模式和`'complex'`模式:
|
|
539
|
-
- `'simple'`模式: 把小程序中不允许的字符串,转义为**相等长度**的代替字符串,这种情况不追求转化目标字符串的一比一绝对等价,即无法从生成结果,反推原先的`class`
|
|
540
|
-
- `'complex'`模式: 把小程序中不允许的字符串,转义为**更长**的代替字符串,这种情况转化前后的字符串是等价的,可以从结果进行反推,缺点就是会生成更长的 `class` 导致 `wxml`和`wxss`这类的体积增大
|
|
541
|
-
|
|
542
|
-
当然,你也可以自定义,传一个 `Record<string, string>` 类型,只需保证转化后 css 中的 `class` 选择器,不会和自己定义的 `class` 产生冲突即可,示例见[dic.ts](https://github.com/sonofmagic/weapp-tailwindcss-webpack-plugin/blob/main/src/dic.ts)
|
|
543
|
-
|
|
544
|
-
### minifiedJs
|
|
545
|
-
|
|
546
|
-
类型: `boolean`
|
|
547
|
-
描述: 是否压缩生成的js文件内容,默认使用环境变量判断: `process.env.NODE_ENV === 'production'`
|
|
548
|
-
|
|
549
|
-
### mangle (2.3.0+)
|
|
550
|
-
|
|
551
|
-
类型: `boolean` \| `IMangleOptions`
|
|
552
|
-
描述: 是否压缩混淆 `wxml`,`js` 和 `wxss` 中指定范围的 `class` 以避免选择器过长问题,默认为`false`不开启,详细配置见 [unplugin-tailwindcss-mangle](https://github.com/sonofmagic/tailwindcss-mangle/tree/main/packages/unplugin-tailwindcss-mangle)
|
|
553
|
-
|
|
554
|
-
### cssPreflight
|
|
555
|
-
|
|
556
|
-
类型: `Record<string,string>`\| `false`
|
|
557
|
-
描述: 在所有 `view`节点添加的 `css` 预设,可根据情况自由的禁用原先的规则,或者添加新的规则。 详细用法如下:
|
|
558
|
-
```js
|
|
559
|
-
// default 默认:
|
|
560
|
-
cssPreflight: {
|
|
561
|
-
'box-sizing': 'border-box',
|
|
562
|
-
'border-width': '0',
|
|
563
|
-
'border-style': 'solid',
|
|
564
|
-
'border-color': 'currentColor'
|
|
565
|
-
}
|
|
566
|
-
// result
|
|
567
|
-
// box-sizing: border-box;
|
|
568
|
-
// border-width: 0;
|
|
569
|
-
// border-style: solid;
|
|
570
|
-
// border-color: currentColor
|
|
571
|
-
|
|
572
|
-
// case 禁用所有
|
|
573
|
-
cssPreflight: false
|
|
574
|
-
// result
|
|
575
|
-
// none
|
|
576
|
-
|
|
577
|
-
// case 禁用单个属性
|
|
578
|
-
cssPreflight: {
|
|
579
|
-
'box-sizing': false
|
|
580
|
-
}
|
|
581
|
-
// border-width: 0;
|
|
582
|
-
// border-style: solid;
|
|
583
|
-
// border-color: currentColor
|
|
584
|
-
|
|
585
|
-
// case 更改和添加单个属性
|
|
586
|
-
cssPreflight: {
|
|
587
|
-
'box-sizing': 'content-box',
|
|
588
|
-
'background': 'black'
|
|
589
|
-
}
|
|
590
|
-
// result
|
|
591
|
-
// box-sizing: content-box;
|
|
592
|
-
// border-width: 0;
|
|
593
|
-
// border-style: solid;
|
|
594
|
-
// border-color: currentColor;
|
|
595
|
-
// background: black
|
|
596
|
-
```
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
### supportCustomLengthUnitsPatch
|
|
600
|
-
|
|
601
|
-
类型: `ILengthUnitsPatchOptions` \| `boolean`
|
|
602
|
-
描述: 自从`tailwindcss 3.2.0`对任意值添加了长度单位的校验后,小程序中的`rpx`这个`wxss`单位,由于不在长度合法名单中,于是被识别成了颜色,导致与预期不符,详见:[issues/110](https://github.com/sonofmagic/weapp-tailwindcss-webpack-plugin/issues/110)。所以这个选项是用来给`tailwindcss`运行时,自动打上一个支持`rpx`单位的补丁。默认开启,在绝大部分情况下,你都可以忽略这个配置项,除非你需要更高级的自定义。
|
|
603
|
-
> 目前自动检索存在一定的缺陷,它会在第一次运行的时候不生效,关闭后第二次运行才生效。这是因为 nodejs 运行时先加载好了 `tailwindcss` 模块 ,然后再来运行这个插件,自动给 `tailwindcss` 运行时打上 `patch`。此时由于 `tailwindcss` 模块已经加载,所以 `patch` 在第一次运行时不生效,`ctrl+c` 关闭之后,再次运行才生效。这种情况可以使用:
|
|
604
|
-
```json
|
|
605
|
-
"scripts": {
|
|
606
|
-
+ "postinstall": "weapp-tw patch"
|
|
607
|
-
}
|
|
608
|
-
```
|
|
609
|
-
|
|
610
|
-
使用 `npm hooks` 的方式来给 `tailwindcss` 自动打 `patch`
|
|
611
|
-
|
|
612
|
-
## 使用任意值(arbitrary values)
|
|
613
|
-
|
|
614
|
-
详见 [tailwindcss/using-arbitrary-values 章节](https://tailwindcss.com/docs/adding-custom-styles#using-arbitrary-values)
|
|
91
|
+
## [常见问题](https://weapp-tw.icebreaker.top/docs/issues/)
|
|
615
92
|
|
|
616
93
|
## [变更日志](./CHANGELOG.md)
|
|
617
94
|
|
|
618
|
-
## 常见问题
|
|
619
|
-
|
|
620
|
-
> 目前微信开发者工具会默认开启 `代码自动热重载 (compileHotReLoad)` 功能,这个功能在原生开发中表现良好,但在 `uni-app` 和 `taro` 等等的框架中,存在一定的问题,详见[issues#37](https://github.com/sonofmagic/weapp-tailwindcss-webpack-plugin/issues/37),所以如果你遇到了此类问题,建议关闭 `代码自动热重载` 功能。
|
|
621
|
-
|
|
622
|
-
[常见问题见 FAQ.md](./docs/faq.md)
|
|
623
|
-
|
|
624
95
|
## Related projects
|
|
625
96
|
|
|
626
97
|
### CLI 工具
|
|
@@ -639,14 +110,10 @@ cssPreflight: {
|
|
|
639
110
|
|
|
640
111
|
[uni-app-vite-vue3-tailwind-vscode-template](https://github.com/sonofmagic/uni-app-vite-vue3-tailwind-vscode-template)
|
|
641
112
|
|
|
642
|
-
~~[uni-app-vue3-tailwind-vscode-template](https://github.com/sonofmagic/uni-app-vue3-tailwind-vscode-template)(不推荐,此版本为webpack5打包vue3,建议使用上面的vite打包vue3的模板)~~
|
|
643
|
-
|
|
644
113
|
[uni-app-vue2-tailwind-vscode-template](https://github.com/sonofmagic/uni-app-vue2-tailwind-vscode-template)
|
|
645
114
|
|
|
646
115
|
#### 使用`hbuilderx` 进行构建和开发
|
|
647
116
|
|
|
648
|
-
~~[uni-app-vue2-tailwind-hbuilder-template](https://github.com/sonofmagic/uni-app-vue2-tailwind-hbuilder-template)(不推荐,此版本收到hbuilderx的限制,无法升级到最新的tailwindcss)~~
|
|
649
|
-
|
|
650
117
|
[uni-app-vue3-tailwind-hbuilder-template](https://github.com/sonofmagic/uni-app-vue3-tailwind-hbuilder-template)
|
|
651
118
|
|
|
652
119
|
#### 使用`tarojs`进行构建 `vscode`开发
|