weapp-tailwindcss 2.3.3
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/LICENSE +21 -0
- package/README.md +674 -0
- package/bin/weapp-tailwindcss.js +7 -0
- package/dist/babel/index.d.ts +4 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +26 -0
- package/dist/cli.mjs +24 -0
- package/dist/constants.d.ts +6 -0
- package/dist/defaults.d.ts +2 -0
- package/dist/dic.d.ts +44 -0
- package/dist/env.d.ts +2 -0
- package/dist/escape.d.ts +3 -0
- package/dist/extractors/split.d.ts +3 -0
- package/dist/gulp/index.d.ts +8 -0
- package/dist/gulp.d.ts +1 -0
- package/dist/gulp.js +86 -0
- package/dist/gulp.mjs +78 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +31 -0
- package/dist/index.mjs +21 -0
- package/dist/js/index.d.ts +5 -0
- package/dist/mangle/store.d.ts +15 -0
- package/dist/options-3a695e84.js +574 -0
- package/dist/options-ed6f4a3a.mjs +542 -0
- package/dist/options.d.ts +4 -0
- package/dist/postcss/index.d.ts +3 -0
- package/dist/postcss/mp.d.ts +4 -0
- package/dist/postcss/plugin.d.ts +6 -0
- package/dist/postcss/preflight.d.ts +3 -0
- package/dist/postcss/selectorParser.d.ts +3 -0
- package/dist/postcss/shared.d.ts +1 -0
- package/dist/postcss-1f9a5153.mjs +105 -0
- package/dist/postcss-dc9eeafc.js +114 -0
- package/dist/postcss.d.ts +1 -0
- package/dist/postcss.js +22 -0
- package/dist/postcss.mjs +6 -0
- package/dist/reg.d.ts +26 -0
- package/dist/replace.d.ts +4 -0
- package/dist/replace.js +36 -0
- package/dist/replace.mjs +27 -0
- package/dist/shared-c2953d9d.js +354 -0
- package/dist/shared-eae1dc7a.mjs +333 -0
- package/dist/tailwindcss/patcher.d.ts +8 -0
- package/dist/tailwindcss/supportCustomUnit.d.ts +6 -0
- package/dist/types.d.ts +118 -0
- package/dist/utils.d.ts +9 -0
- package/dist/vite/index.d.ts +3 -0
- package/dist/vite.d.ts +1 -0
- package/dist/vite.js +82 -0
- package/dist/vite.mjs +78 -0
- package/dist/webpack/BaseUnifiedPlugin/v5.d.ts +9 -0
- package/dist/webpack/index.d.ts +1 -0
- package/dist/webpack.d.ts +1 -0
- package/dist/webpack.js +101 -0
- package/dist/webpack.mjs +97 -0
- package/dist/wxml/index.d.ts +3 -0
- package/dist/wxml/shared.d.ts +2 -0
- package/dist/wxml/utils.d.ts +7 -0
- package/package.json +224 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 ice breaker
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,674 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
# weapp-tailwindcss-webpack-plugin
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+
[](https://github.com/sonofmagic/weapp-tailwindcss-webpack-plugin/actions/workflows/test.yml)
|
|
9
|
+
[](https://codecov.io/gh/sonofmagic/weapp-tailwindcss-webpack-plugin)
|
|
10
|
+
|
|
11
|
+
> 把 `tailwindcss` 带入小程序开发吧!
|
|
12
|
+
|
|
13
|
+
\[[国内部署的文档地址](https://weapp-tw.icebreaker.top)\] \| \[[1.x文档]('./v1.md')\]
|
|
14
|
+
|
|
15
|
+
- [weapp-tailwindcss-webpack-plugin](#weapp-tailwindcss-webpack-plugin)
|
|
16
|
+
- [特性](#特性)
|
|
17
|
+
- [插件介绍](#插件介绍)
|
|
18
|
+
- [Usage](#usage)
|
|
19
|
+
- [1. 安装配置 tailwindcss](#1-安装配置-tailwindcss)
|
|
20
|
+
- [2. `rem` 转 `px` 或 `rpx`](#2-rem-转-px-或-rpx)
|
|
21
|
+
- [3. 安装这个插件](#3-安装这个插件)
|
|
22
|
+
- [各个框架注册的方式](#各个框架注册的方式)
|
|
23
|
+
- [从 v1 迁移](#从-v1-迁移)
|
|
24
|
+
- [精确转化与忽略](#精确转化与忽略)
|
|
25
|
+
- [Options 配置项](#options-配置项)
|
|
26
|
+
- [使用任意值(arbitrary values)](#使用任意值arbitrary-values)
|
|
27
|
+
- [变更日志](#变更日志)
|
|
28
|
+
- [常见问题](#常见问题)
|
|
29
|
+
- [Related projects](#related-projects)
|
|
30
|
+
- [CLI 工具](#cli-工具)
|
|
31
|
+
- [模板 template](#模板-template)
|
|
32
|
+
- [如何选择?](#如何选择)
|
|
33
|
+
- [使用`uni-app cli`进行构建 `vscode`开发](#使用uni-app-cli进行构建-vscode开发)
|
|
34
|
+
- [使用`hbuilderx` 进行构建和开发](#使用hbuilderx-进行构建和开发)
|
|
35
|
+
- [使用`tarojs`进行构建 `vscode`开发](#使用tarojs进行构建-vscode开发)
|
|
36
|
+
- [原生小程序开发模板](#原生小程序开发模板)
|
|
37
|
+
- [tailwindcss plugin](#tailwindcss-plugin)
|
|
38
|
+
- [tailwindcss preset](#tailwindcss-preset)
|
|
39
|
+
- [Bugs \& Issues](#bugs--issues)
|
|
40
|
+
|
|
41
|
+
## 特性
|
|
42
|
+
|
|
43
|
+
| 不仅仅是`webpack` | 主流框架与原生开发支持 |
|
|
44
|
+
| --------------------------------------------------- | ----------------------------------------------- |
|
|
45
|
+
|  |  |
|
|
46
|
+
|
|
47
|
+
> `2.2.0` 版本后,所以 v1 版本的插件被去除,如果你还是想用 v1 插件,请锁定你的版本在 `2.1.5`
|
|
48
|
+
|
|
49
|
+
这个版本新增了 `UnifiedWebpackPluginV5`
|
|
50
|
+
和 `UnifiedViteWeappTailwindcssPlugin` 这种 `Unified` 开头的插件。
|
|
51
|
+
|
|
52
|
+
它们能够自动识别并精确处理所有 `tailwindcss` 的工具类。这意味着它可以同时处理所有文件中的静态或动态的 `class`。
|
|
53
|
+
|
|
54
|
+
相比`v1`版本只有处理`wxss`,`wxml`静态`class`的能力,使用`v2`版本新的插件,你再也不需要在 `js` 里引入并调用标记方法 `replaceJs`了!`2.x` 插件有精准转化 `js`/`jsx` 的能力,大大提升了 `taro` 这种动态模板框架的开发体验。
|
|
55
|
+
|
|
56
|
+
### 插件介绍
|
|
57
|
+
|
|
58
|
+
`UnifiedWebpackPluginV5` 是一个核心插件,所有使用 `webpack` 进行打包的框架都可以使用它,只需要传入 `appType` 配置项: `uni-app`/`taro`/`rax`/`remax`/`mpx` 等等,如果不传的话,插件会去猜测公共的样式文件位置,并进行转化(有可能不准确)。
|
|
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
|
+
```
|
|
421
|
+
|
|
422
|
+
具体可以参考 [weapp-tailwindcss-gulp-template(gulp打包)](https://github.com/sonofmagic/weapp-tailwindcss-webpack-plugin/tree/main/demo/gulp-app) 模板项目的配置。
|
|
423
|
+
|
|
424
|
+
<br/></details>
|
|
425
|
+
|
|
426
|
+
## 从 v1 迁移
|
|
427
|
+
|
|
428
|
+
在 `2.x` 版本中,可以把之前使用的 `webpack` 插件,全部更换为 `UnifiedWebpackPluginV5` 插件,不过 `vite` 插件的导出有一些小变化:
|
|
429
|
+
|
|
430
|
+
`1.x`:
|
|
431
|
+
|
|
432
|
+
```js
|
|
433
|
+
import vwt from 'weapp-tailwindcss-webpack-plugin/vite';
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
`2.x`:
|
|
437
|
+
|
|
438
|
+
```js
|
|
439
|
+
// UnifiedViteWeappTailwindcssPlugin 就是新的插件
|
|
440
|
+
import { UnifiedViteWeappTailwindcssPlugin } from 'weapp-tailwindcss-webpack-plugin/vite';
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
另外新的 `UnifiedWebpackPluginV5` 可以直接从 `weapp-tailwindcss-webpack-plugin` 引入,同时在新的 `UnifiedWebpackPluginV5` 中,之前所有的配置项都被继承了过来,只需要用它直接替换原先插件即可。
|
|
444
|
+
|
|
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)
|
|
615
|
+
|
|
616
|
+
## [变更日志](./CHANGELOG.md)
|
|
617
|
+
|
|
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
|
+
## Related projects
|
|
625
|
+
|
|
626
|
+
### CLI 工具
|
|
627
|
+
|
|
628
|
+
[weapp-ide-cli](https://github.com/sonofmagic/utils/tree/main/packages/weapp-ide-cli): 一个微信开发者工具命令行,快速方便的直接启动 ide 进行登录,开发,预览,上传代码等等功能。
|
|
629
|
+
|
|
630
|
+
### 模板 template
|
|
631
|
+
|
|
632
|
+
#### 如何选择?
|
|
633
|
+
|
|
634
|
+
假如你仅仅是开发一个`小程序` + `h5` 的组合,那么使用 `vscode` 模板就足够了
|
|
635
|
+
|
|
636
|
+
假如你的项目构建的重点平台是 `app` 那么还是推荐使用 `hbuilderx` 模板,因为 `hbuilderx` 自带了一套 `app` 构建和调试的工具链,可以更好的支持你的开发。
|
|
637
|
+
|
|
638
|
+
#### 使用`uni-app cli`进行构建 `vscode`开发
|
|
639
|
+
|
|
640
|
+
[uni-app-vite-vue3-tailwind-vscode-template](https://github.com/sonofmagic/uni-app-vite-vue3-tailwind-vscode-template)
|
|
641
|
+
|
|
642
|
+
~~[uni-app-vue3-tailwind-vscode-template](https://github.com/sonofmagic/uni-app-vue3-tailwind-vscode-template)(不推荐,此版本为webpack5打包vue3,建议使用上面的vite打包vue3的模板)~~
|
|
643
|
+
|
|
644
|
+
[uni-app-vue2-tailwind-vscode-template](https://github.com/sonofmagic/uni-app-vue2-tailwind-vscode-template)
|
|
645
|
+
|
|
646
|
+
#### 使用`hbuilderx` 进行构建和开发
|
|
647
|
+
|
|
648
|
+
~~[uni-app-vue2-tailwind-hbuilder-template](https://github.com/sonofmagic/uni-app-vue2-tailwind-hbuilder-template)(不推荐,此版本收到hbuilderx的限制,无法升级到最新的tailwindcss)~~
|
|
649
|
+
|
|
650
|
+
[uni-app-vue3-tailwind-hbuilder-template](https://github.com/sonofmagic/uni-app-vue3-tailwind-hbuilder-template)
|
|
651
|
+
|
|
652
|
+
#### 使用`tarojs`进行构建 `vscode`开发
|
|
653
|
+
|
|
654
|
+
[taro-react-tailwind-vscode-template](https://github.com/sonofmagic/taro-react-tailwind-vscode-template)
|
|
655
|
+
|
|
656
|
+
#### 原生小程序开发模板
|
|
657
|
+
|
|
658
|
+
[weapp-tailwindcss-gulp-template(gulp打包)](https://github.com/sonofmagic/weapp-tailwindcss-webpack-plugin/tree/main/demo/gulp-app)
|
|
659
|
+
|
|
660
|
+
[weapp-native-mina-tailwindcss-template(webpack打包)](https://github.com/sonofmagic/weapp-native-mina-tailwindcss-template)
|
|
661
|
+
|
|
662
|
+
### tailwindcss plugin
|
|
663
|
+
|
|
664
|
+
[weapp-tailwindcss-children](https://github.com/sonofmagic/weapp-tailwindcss-children)
|
|
665
|
+
|
|
666
|
+
### tailwindcss preset
|
|
667
|
+
|
|
668
|
+
[tailwindcss-miniprogram-preset](https://github.com/sonofmagic/tailwindcss-miniprogram-preset)
|
|
669
|
+
|
|
670
|
+
## Bugs & Issues
|
|
671
|
+
|
|
672
|
+
目前这个插件正在快速的开发中,如果遇到 `Bug` 或者想提出 `Issue`
|
|
673
|
+
|
|
674
|
+
[欢迎提交到此处](https://github.com/sonofmagic/weapp-tailwindcss-webpack-plugin/issues)
|