瀏覽代碼

feat: add replace with -1 logic

DaiQiangReal 3 年之前
父節點
當前提交
247303166e

+ 14 - 7
packages/semi-scss-to-css-var/src/transVarPlugin/index.ts

@@ -1,7 +1,8 @@
-import {AcceptedPlugin, Declaration, Postcss, Root} from "postcss";
+import { AcceptedPlugin, Declaration, Postcss, Root } from "postcss";
 import fs from 'fs-extra';
-import parse from 'postcss-value-parser'
+import parse from 'postcss-value-parser';
 import replaceWithCalc from "../utils/replaceWithCalc";
+import replaceOther from "../utils/replaceOther";
 
 
 
@@ -20,12 +21,18 @@ const transVarPlugin=()=>{
             //console.log(root)
         },
         Once(root:Root){
-          //  console.log(root)
+            //  console.log(root)
         },
         Declaration(decl:Declaration){
+
+            console.log(decl.value);
+
             //@ts-ignore
-            if(!decl.isVisited){
-                decl.value=replaceWithCalc(decl.value);
+            if (!decl.isVisited){
+                let value = decl.value;
+                value = replaceOther(value);
+                value = replaceWithCalc(value);
+                decl.value=value;
                 //@ts-ignore
                 decl.isVisited=true;
             }
@@ -37,8 +44,8 @@ const transVarPlugin=()=>{
             // fs.writeFileSync('./test.json',JSON.stringify(valueRoot,null,'    '),{encoding:'utf-8'})
             // console.log(parse(decl.value));
         },
-    } as AcceptedPlugin
-}
+    } as AcceptedPlugin;
+};
 
 
 transVarPlugin.postcss=true;

+ 6 - 0
packages/semi-scss-to-css-var/src/utils/replaceOther.ts

@@ -0,0 +1,6 @@
+const replaceOther = (str:string)=>{
+    return str.replace(/-(\$[\w\d]+)/,"-1 * $1");
+
+};
+
+export default replaceOther;

+ 1 - 1
packages/semi-scss-to-css-var/test/test.scss

@@ -31,7 +31,7 @@ $c:3px;
 $d:4px;
 
 .body{
-  border:  $a - ($b + ($c + $d)+ $c);
+  border:  $a - (-$b + ($c + $d)+ $c); //test
 }