postcss-pxtorem icon indicating copy to clipboard operation
postcss-pxtorem copied to clipboard

Transform within default variables

Open bart-jaskulski opened this issue 4 years ago • 4 comments

Transforming px values to rem is not working inside default values in custom properties.

CSS below would be sufficient to reproduce the issue.

body {
  padding: var(--site-padding, 16px) 
}

Desired outcome should be:

body {
  padding: var(--site-padding, 1rem)
}

Nonetheless, such piece of code is not transformed. Is this a flaw possible to fix or design choice?

bart-jaskulski avatar Nov 09 '21 01:11 bart-jaskulski

Yes, it is a design choise, see this regexp

/"[^"]+"|'[^']+'|url\([^)]+\)|var\([^)]+\)|(\d*\.?\d+)px/g

it ignored all the var statements, I think it's because of the variable names, which can include something like --1px. We just need to improve regexp and there will be no such problem:

/"[^"]+"|'[^']+'|url\([^)]+\)|((?<=^|[^\w-])-?\d*\.?\d+)px\b/g

NikolayMakhonin avatar Oct 02 '22 07:10 NikolayMakhonin

是的,这是一种设计选择,请参看此正则表达式

/"[^"]+"|'[^']+'|url\([^)]+\)|var\([^)]+\)|(\d*\.?\d+)px/g

它忽略了所有的var语言句子,我认为这是因为变量名,它可以包含类似的--1px。我们只需要改变进入正则表达式就不会出现这样的问题:

/"[^"]+"|'[^']+'|url\([^)]+\)|((?<=^|[^\w-])-?\d*\.?\d+)px\b/g

left: -10px; not working

AAAAAAAAAdai avatar Feb 28 '23 14:02 AAAAAAAAAdai

any progress?

citrus327 avatar Sep 15 '23 02:09 citrus327

@yangmingshan @cuth Please add the ability to convert px to em inside var in the callback ignoring custom variables. Can u merge and release a new version?

dimitry-blinov avatar Feb 19 '24 06:02 dimitry-blinov