Skip to content

Commit 366027a

Browse files
committed
feat: 修复 cssCalc 预计算时可能输出重复声明的问题,新增仅在启用 cssCalc 时生效的去重清理
1 parent a9e2214 commit 366027a

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@weapp-tailwindcss/postcss": patch
3+
---
4+
5+
修复 `cssCalc` 预计算时可能输出重复声明的问题,新增仅在启用 `cssCalc` 时生效的去重清理。

packages/postcss/src/pipeline.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { IStyleHandlerOptions } from './types'
44
import postcssPresetEnv from 'postcss-preset-env'
55
import { createColorFunctionalFallback } from './plugins/colorFunctionalFallback'
66
import { createContext } from './plugins/ctx'
7+
import { getCalcDuplicateCleaner } from './plugins/getCalcDuplicateCleaner'
78
import { getCalcPlugin } from './plugins/getCalcPlugin'
89
import { getCustomPropertyCleaner } from './plugins/getCustomPropertyCleaner'
910
import { getPxTransformPlugin } from './plugins/getPxTransformPlugin'
@@ -184,6 +185,21 @@ function createPipelineDefinitions(options: IStyleHandlerOptions): PipelineNodeD
184185
},
185186
})
186187

188+
stages.normal.push({
189+
id: 'normal:calc-duplicate-cleaner',
190+
stage: 'normal',
191+
prepare: () => {
192+
const plugin = getCalcDuplicateCleaner(options)
193+
return plugin
194+
? {
195+
id: 'normal:calc-duplicate-cleaner',
196+
stage: 'normal',
197+
createPlugin: () => plugin,
198+
}
199+
: undefined
200+
},
201+
})
202+
187203
stages.normal.push({
188204
id: 'normal:custom-property-cleaner',
189205
stage: 'normal',
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// 清理由 cssCalc 引入的重复声明,避免输出冗余属性
2+
import type { AcceptedPlugin } from 'postcss'
3+
import type { IStyleHandlerOptions } from '../types'
4+
5+
export function getCalcDuplicateCleaner(options: IStyleHandlerOptions): AcceptedPlugin | null {
6+
if (!options.cssCalc) {
7+
return null
8+
}
9+
10+
return {
11+
postcssPlugin: 'postcss-calc-duplicate-cleaner',
12+
Rule(rule) {
13+
rule.walkDecls((decl) => {
14+
const prev = decl.prev()
15+
if (!prev || prev.type !== 'decl') {
16+
return
17+
}
18+
19+
if (prev.prop !== decl.prop) {
20+
return
21+
}
22+
23+
if (prev.important !== decl.important) {
24+
return
25+
}
26+
27+
if (prev.value !== decl.value) {
28+
return
29+
}
30+
31+
decl.remove()
32+
})
33+
},
34+
}
35+
}

packages/postcss/test/calc.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,20 @@ describe('calc', () => {
192192
expect(css).toContain('margin-left: calc(var(--spacing)*4);')
193193
})
194194

195+
it('removes duplicate literal declarations when cssCalc is enabled', async () => {
196+
const code = `.mt-1{ margin-top: 8rpx; margin-top: 8rpx; }`
197+
198+
const styleHandler = createStyleHandler({
199+
isMainChunk: false,
200+
cssCalc: true,
201+
})
202+
const { css } = await styleHandler(code, {
203+
isMainChunk: false,
204+
})
205+
const count = css.match(/margin-top:\s*8rpx;/g)?.length ?? 0
206+
expect(count).toBe(1)
207+
})
208+
195209
it('cssCalc 传入 --spacing 示例', async () => {
196210
const code = `page,
197211
:root {

0 commit comments

Comments
 (0)