Skip to content

Commit ce1150c

Browse files
committed
chore: bump version
1 parent c867c44 commit ce1150c

7 files changed

Lines changed: 106 additions & 18 deletions

File tree

.changeset/loud-birds-arrive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@weapp-tailwindcss/postcss": minor
3+
---
4+
5+
feat: 添加 postcss calc 和 pxtransform 支持

apps/vite-native/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
},
3333
"devDependencies": {
3434
"@egoist/tailwindcss-icons": "^1.9.0",
35-
"@iconify-json/lucide": "^1.2.66",
35+
"@iconify-json/lucide": "^1.2.67",
3636
"@iconify-json/mdi": "^1.2.3",
3737
"@tailwindcss/postcss": "^4.1.13",
3838
"@tailwindcss/vite": "^4.1.13",

packages/postcss/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@
5151
"@weapp-core/escape": "~4.0.1",
5252
"@weapp-tailwindcss/shared": "workspace:*",
5353
"postcss": "~8.5.6",
54+
"postcss-calc": "^10.1.1",
5455
"postcss-preset-env": "^10.3.1",
56+
"postcss-pxtransform": "^4.1.6",
5557
"postcss-rem-to-responsive-pixel": "~6.0.2",
5658
"postcss-selector-parser": "~7.1.0"
5759
},
5860
"devDependencies": {
5961
"@weapp-tailwindcss/mangle": "workspace:*",
60-
"postcss-calc": "^10.1.1",
6162
"postcss-value-parser": "^4.2.0"
6263
}
6364
}

packages/postcss/src/plugins/index.ts

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import type { AcceptedPlugin } from 'postcss'
2+
import type { PxtransformOptions } from 'postcss-pxtransform'
23
import type { IStyleHandlerOptions } from '../types'
3-
// import calc from 'postcss-calc'
4+
import { defuOverrideArray } from '@weapp-tailwindcss/shared'
5+
import postcssCalc from 'postcss-calc'
46
import postcssPresetEnv from 'postcss-preset-env'
7+
import postcssPxtransform from 'postcss-pxtransform'
58
import postcssRem2rpx from 'postcss-rem-to-responsive-pixel'
69
import { createContext } from './ctx'
710
import { postcssWeappTailwindcssPostPlugin as post } from './post'
@@ -12,15 +15,14 @@ import { postcssWeappTailwindcssPrePlugin as pre } from './pre'
1215
* @param options - 样式处理器选项,包含 PostCSS 插件和其他配置。
1316
* @returns AcceptedPlugin[] - 生成的 PostCSS 插件数组。
1417
*/
15-
export function getPlugins(options: IStyleHandlerOptions) {
18+
export function getPlugins(options: IStyleHandlerOptions): AcceptedPlugin[] {
1619
const ctx = createContext()
1720
options.ctx = ctx
18-
const plugins: AcceptedPlugin[] = [
21+
const plugins = [
1922
...(options.postcssOptions?.plugins ?? []),
2023
pre(options),
21-
// calc({ }),
2224
postcssPresetEnv(options.cssPresetEnv),
23-
]
25+
].filter(x => Boolean(x)) as AcceptedPlugin[]
2426
if (options.rem2rpx) {
2527
plugins.push(
2628
postcssRem2rpx(
@@ -34,6 +36,43 @@ export function getPlugins(options: IStyleHandlerOptions) {
3436
),
3537
)
3638
}
39+
if (options.cssCalc) {
40+
plugins.push(
41+
postcssCalc(
42+
options.cssCalc,
43+
),
44+
)
45+
}
46+
47+
if (options.px2rpx) {
48+
plugins.push(
49+
postcssPxtransform(
50+
defuOverrideArray<PxtransformOptions, PxtransformOptions[]>(
51+
typeof options.px2rpx === 'object'
52+
? options.px2rpx
53+
: {},
54+
{
55+
platform: 'weapp',
56+
unitPrecision: 5,
57+
propList: ['*'],
58+
selectorBlackList: [],
59+
replace: true,
60+
mediaQuery: false,
61+
minPixelValue: 0,
62+
designWidth: 750,
63+
deviceRatio: {
64+
375: 2,
65+
640: 2.34 / 2,
66+
750: 1,
67+
828: 1.81 / 2,
68+
},
69+
},
70+
),
71+
72+
),
73+
)
74+
}
75+
3776
plugins.push(post(options))
3877
return plugins
3978
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
declare module 'postcss-pxtransform' {
2+
import type { PluginCreator } from 'postcss'
3+
4+
export interface PxtransformOptions {
5+
platform?: string
6+
designWidth?: number | (() => number)
7+
deviceRatio?: Record<string, number>
8+
unitPrecision?: number
9+
propList?: (string | RegExp)[]
10+
selectorBlackList?: (string | RegExp) []
11+
replace?: boolean
12+
mediaQuery?: boolean
13+
minPixelValue?: number
14+
}
15+
16+
const pxtransform: PluginCreator<PxtransformOptions>
17+
export default pxtransform
18+
}
19+
// 默认值
20+
// unitPrecision: 5,
21+
// propList: ['*'],
22+
// selectorBlackList: [],
23+
// replace: true,
24+
// mediaQuery: false,
25+
// minPixelValue: 0

packages/postcss/src/types.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { IMangleScopeContext } from '@weapp-tailwindcss/mangle'
22
import type { Rule } from 'postcss'
3+
import type { PostCssCalcOptions } from 'postcss-calc'
34
import type { Result } from 'postcss-load-config'
45
import type { pluginOptions as PresetEnvOptions } from 'postcss-preset-env'
5-
import type { UserDefinedOptions as rem2rpxOptions } from 'postcss-rem-to-responsive-pixel'
6+
import type { PxtransformOptions as Px2rpxOptions } from 'postcss-pxtransform'
7+
import type { UserDefinedOptions as Rem2rpxOptions } from 'postcss-rem-to-responsive-pixel'
68
import type { IContext as PostcssContext } from './plugins/ctx'
79
import type { InjectPreflight } from './preflight'
810

@@ -36,6 +38,7 @@ export type RequiredStyleHandlerOptions = {
3638
| 'injectAdditionalCssVarScope'
3739
| 'cssSelectorReplacement'
3840
| 'rem2rpx'
41+
| 'px2rpx'
3942
>
4043

4144
export interface InternalCssSelectorReplacerOptions {
@@ -51,6 +54,7 @@ export type IStyleHandlerOptions = {
5154
cssRemoveProperty?: boolean
5255
cssRemoveHoverPseudoClass?: boolean
5356
cssPresetEnv?: PresetEnvOptions
57+
cssCalc?: PostCssCalcOptions
5458
atRules?: {
5559
property?: boolean
5660
// A 新增 wxss 支持 @supports 反馈详情
@@ -72,7 +76,8 @@ export interface UserDefinedPostcssOptions {
7276
root?: string | string[] | false
7377
universal?: string | string[] | false
7478
}
75-
rem2rpx?: boolean | rem2rpxOptions
79+
rem2rpx?: boolean | Rem2rpxOptions
80+
px2rpx?: boolean | Px2rpxOptions
7681
postcssOptions?: LoadedPostcssOptions
7782
cssRemoveHoverPseudoClass?: boolean
7883
cssRemoveProperty?: boolean

pnpm-lock.yaml

Lines changed: 22 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)