@@ -147,6 +147,273 @@ export default Index
147
147
- ` styled(View) ` 写法会产生类型错误,[ #8883 ] ( https://github.com/NervJS/taro/issues/8883 )
148
148
- 与微信小程序直播插件 ` live-player-plugin ` 共用时报错,[ #7389 ] ( https://github.com/NervJS/taro/issues/7389 )
149
149
150
+ ## Panda
151
+
152
+ [ ` pandacss ` ] ( https://panda-css.com/ ) 是个新兴的优秀 ` CSS-in-JS ` 编译时框架,相比 ` linaria ` ,它的配置更加简单,智能提示好,开发者体验也更好。
153
+
154
+ 而且它也吸收了许多 ` tailwindcss ` 的优点,我们可以自由的配置我们的主题与样式,且原子化的类名也更容易进行自由组合。
155
+
156
+ [ ` weapp-pandacss ` ] ( https://github.com/sonofmagic/weapp-pandacss ) 就是让你在小程序开发中使用它。
157
+
158
+ ### 快速开始
159
+
160
+ #### pandacss 安装和配置
161
+
162
+ ##### 0. 安装和初始化 pandacss
163
+
164
+ 首先我们需要把 ` @pandacss/dev ` 这些都安装和配置好,这里我们以 ` tarojs ` 项目为例:
165
+
166
+ ``` bash
167
+ npm install -D @pandacss/dev weapp-pandacss postcss # 或者 yarn / pnpm
168
+ npx panda init
169
+ ```
170
+
171
+ 此时会在当前目录生成一个 ` panda.config.ts ` 和一个包含大量文件的 ` styled-system ` 。
172
+
173
+ > ` panda.config.ts ` 是 ` pandacss ` 的配置文件,` styled-system ` 文件夹里的是 ` pandacss ` 的运行时 ` js ` 。
174
+
175
+ 把 ` styled-system ` 加入我们的 ` .gitignore ` 中去。
176
+
177
+ ``` diff
178
+ # .gitignore
179
+ + styled-system
180
+ ```
181
+
182
+ ##### 1. 配置 postcss
183
+
184
+ 接着在根目录里,添加一个 ` postcss.config.cjs ` 文件,写入以下代码注册 ` pandacss ` :
185
+
186
+ ``` js
187
+ module .exports = {
188
+ plugins: {
189
+ ' @pandacss/dev/postcss' : {}
190
+ }
191
+ }
192
+ ```
193
+
194
+ ##### 2. 检查你的 panda.config.ts
195
+
196
+ 生成的配置文件大概长下面这样,尤其注意 ` include ` 是用来告诉 ` pandacss ` 从哪些文件中提取原子类的,所以这个配置一定要准确
197
+
198
+ ``` ts
199
+ import { defineConfig } from " @pandacss/dev"
200
+
201
+ export default defineConfig ({
202
+ // 小程序不需要
203
+ preflight: process .env .TARO_ENV === ' h5' ,
204
+ // ⚠️这里,假如你使用 vue,记得把 vue 文件格式包括进来!!!
205
+ include: [" ./src/**/*.{js,jsx,ts,tsx}" ],
206
+ exclude: [],
207
+ theme: {
208
+ extend: {}
209
+ },
210
+ outdir: " styled-system" ,
211
+ })
212
+ ```
213
+
214
+ ##### 3. 修改 package.json 脚本
215
+
216
+ 然后,我们添加下方 ` prepare ` 脚本在我们的 ` package.json ` 的 ` scripts ` 块中:
217
+
218
+ ``` diff
219
+ {
220
+ "scripts": {
221
+ + "prepare": "panda codegen",
222
+ }
223
+ }
224
+ ```
225
+
226
+ 这样我们每次重新 ` npm i/yarn/pnpm i ` 的时候,都会执行这个方法,重新生成 ` styled-system ` ,当然你也可以直接通过 ` npm run prepare ` 直接执行这个脚本。
227
+
228
+ ##### 4. 全局 css 注册 pandacss
229
+
230
+ 然后在我们的全局样式文件 ` src/app.scss ` 中注册 ` pandacss ` :
231
+
232
+ ``` css
233
+ @layer reset, base, tokens, recipes, utilities;
234
+ ```
235
+
236
+ 配置好了之后,此时 ` pandacss ` 在 ` h5 ` 平台已经生效了,你可以 ` npm run dev:h5 ` 在 ` h5 ` 平台初步使用了,但是为了开发体验,我们还有一些优化项要做。
237
+
238
+ ##### 5. 配置的优化与别名
239
+
240
+ 来到根目录的 ` tsconfig.json ` 添加:
241
+
242
+ ``` diff
243
+ {
244
+ "compilerOptions": {
245
+ "paths": {
246
+ "@/*": [
247
+ "src/*"
248
+ ],
249
+ + "styled-system/*": [
250
+ + "styled-system/*"
251
+ + ]
252
+ }
253
+ },
254
+ "include": [
255
+ "./src",
256
+ "./types",
257
+ "./config",
258
+ + "styled-system"
259
+ ],
260
+ }
261
+ ```
262
+
263
+ 接着来到 ` config/index.ts ` 添加 ` alias ` ([ 参考链接] ( https://taro-docs.jd.com/docs/config-detail#alias ) ):
264
+
265
+ ``` ts
266
+ import path from ' path'
267
+
268
+ {
269
+ alias : {
270
+ ' styled-system' : path .resolve (__dirname , ' ..' , ' styled-system' )
271
+ },
272
+ }
273
+ ```
274
+
275
+ 这样我们就不需要使用相对路径来使用 ` pandacss ` 了,同时 ` ts ` 智能提示也有了,你可以这样使用它:
276
+
277
+ ``` ts
278
+ import { View , Text } from " @tarojs/components" ;
279
+ import { css } from " styled-system/css" ;
280
+
281
+ const styles = css ({
282
+ bg: " yellow.200" ,
283
+ rounded: " 9999px" ,
284
+ fontSize: " 90px" ,
285
+ p: " 10px 15px" ,
286
+ color: " pink.500" ,
287
+ });
288
+
289
+ export default function Index() {
290
+ return (
291
+ < View className = {styles }>
292
+ <Text >Hello world ! < / Text >
293
+ < / View >
294
+ );
295
+ }
296
+ ```
297
+
298
+ > 此部分参考的官方链接 <https://panda-css.com/docs/installation/postcss>
299
+
300
+ 接下来进入 `weapp-pandacss` 的插件配置,不用担心,相比前面那些繁琐的步骤,这个可简单多了。
301
+
302
+ #### weapp-pandacss 配置
303
+
304
+ > 记得安装好 ` weapp-pandacss ` !
305
+
306
+ ##### 0. 回到 postcss 进行注册
307
+
308
+ 回到项目根目录的 ` postcss.config.cjs ` 注册 ` weapp-pandacss ` ,添加以下配置:
309
+
310
+ ``` diff
311
+ module.exports = {
312
+ plugins: {
313
+ '@pandacss/dev/postcss': {},
314
+ + 'weapp-pandacss/postcss': {}
315
+ }
316
+ }
317
+ ```
318
+
319
+ ##### 1. 回到 package.json 添加生成脚本
320
+
321
+ 然后去 ` package.json ` 你添加 ` prepare ` 脚本的地方,加点代码
322
+
323
+ ``` diff
324
+ {
325
+ "scripts": {
326
+ - "prepare": "panda codegen",
327
+ + "prepare": "panda codegen && weapp-panda codegen",
328
+ }
329
+ }
330
+ ```
331
+
332
+ > 注意这里必须用 ` && ` 而不能用 ` & ` ,` & ` 任务执行会并行不会等待,而 ` && ` 会等待前一个执行完成再执行后一条命令
333
+
334
+ 然后,你再手动执行一下
335
+
336
+ ``` bash
337
+ npm run prepare
338
+ ```
339
+
340
+ 来重新生成 ` styled-system ` , 此时你会发现 ` pandacss ` 的命令行输出中多了 ` 2 ` 行:
341
+
342
+ ``` diff
343
+ ✔️ `src/styled-system/css`: the css function to author styles
344
+ ✔️ `src/styled-system/tokens`: the css variables and js function to query your tokens
345
+ ✔️ `src/styled-system/patterns`: functions to implement apply common layout patterns
346
+ ✔️ `src/styled-system/jsx`: styled jsx elements for react
347
+ + ✔️ `src/styled-system/weapp-panda`: the core escape function for weapp
348
+ + ✔️ `src/styled-system/helpers.mjs`: inject escape function into helpers
349
+ ```
350
+
351
+ 这代表着小程序相关的转义逻辑已经被注入进去,此时 ` panda css ` 生成的类就兼容小程序平台啦,是不是很简单?
352
+
353
+ 当然为了防止你配置失败,我也给出了参考项目: [ taro-react-pandacss-template] ( https://github.com/sonofmagic/taro-react-pandacss-template ) 方便进行排查纠错。
354
+
355
+ ### 常见问题
356
+
357
+ #### 跨平台注意事项
358
+
359
+ 你可能同时开发 ` 小程序 ` 和 ` h5 ` 平台,但是你发现使用 ` weapp-pandacss ` 之后,` h5 ` 平台似乎就不行了?
360
+
361
+ 这时候你可以这样配置:
362
+
363
+ ` process.env.TARO_ENV === 'h5' ` 的时候,不去加载 ` weapp-pandacss/postcss ` (根据环境变量动态加载 ` postcss ` 插件)
364
+
365
+ 同时你也可以执行 ` weapp-panda rollback ` 把 ` css ` 方法进行回滚到最原始适配 ` h5 ` 平台的状态。
366
+
367
+ 当然你恢复到小程序版本也只需要执行 ` weapp-panda codegen ` 就会重新注入了。
368
+
369
+ #### 小程序预览事项
370
+
371
+ 当小程序预览时会出现 ` Error: 非法的文件,错误信息:invalid file: pages/index/index.js, 565:24, SyntaxError: Unexpected token . if (variants[key]?.[value]) ` 错误。
372
+
373
+ 这是因为 ` panda ` 生成的文件 ` cva.mjs ` 使用了 [ ` Optional chaining (?.) ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining ) 语法,这个语法小程序原生不支持,这时候可以开启勾选 ` 将JS编译成ES5 ` 功能再进行预览,或者使用 ` babel ` 预先进行处理再上传预览。
374
+
375
+ ### 高级配置文件
376
+
377
+ 你可以通过 ` npx weapp-panda init ` 命令在当前目录下创建一个 ` weapp-pandacss.config.ts ` 配置文件。
378
+
379
+ 这个配置文件可以用来控制转义代码的生成和一部分 ` postcss ` 插件的行为。
380
+
381
+ ``` ts
382
+ import { defineConfig } from ' weapp-pandacss'
383
+
384
+ export default defineConfig ({
385
+ postcss: {
386
+ // 转义插件是否生效,这只能控制核心插件的生效情况,而核心插件只是一部分
387
+ // 假如你想让整个插件真正不生效,请在 `postcss.config.cjs` 里进行动态加载判断
388
+ disabled: false ,
389
+ // 数组merge默认行为是直接concat 合并,所以传一个空数组是使用的默认数组
390
+ // 转义替换对象
391
+ selectorReplacement: {
392
+ root: [],
393
+ universal: [],
394
+ cascadeLayers: ' a'
395
+ },
396
+ removeNegationPseudoClass: true
397
+ },
398
+ // 生成上下文
399
+ context: {
400
+ // 转义注入判断条件,更改后需要重新生成代码
401
+ escapePredicate: ` process.env.TARO_ENV !== 'h5' && process.env.TARO_ENV !== 'rn' ` ,
402
+ // 插件的 pandaConfig 寻找配置
403
+ pandaConfig: {
404
+ cwd: process .cwd (),
405
+ file: ' path/to/your-panda-config-file'
406
+ }
407
+ }
408
+ })
409
+ ```
410
+
411
+ 当然,你更改相关的配置项之后,要重新执行一下 ` npm run prepare ` 来生成新的注入转义代码。
412
+
413
+ ### 参考示例
414
+
415
+ [ taro-react-pandacss-template] ( https://github.com/sonofmagic/taro-react-pandacss-template )
416
+
150
417
## Fower
151
418
152
419
社区还有另一个方案 ** Fower** ,[ 文档] ( https://fower.vercel.app/docs/use-with-taro )
0 commit comments