2016-08-02 122 views
0

將大量冗餘的蹩腳css文件轉換爲scss文件後,我有一堆scss文件。我很確定在這些文件中有很多常見的CSS,我想提取這些代碼。如何從多個文件中提取常用的scss代碼?

舉個例子,假設我有SCSS代碼塊(我們稱之爲塊A):

.test { 
    color: white; 

    .toto { 
     background: red; 
     font-size: 12px; 
    } 
} 

而另一個塊(我們會打電話給塊B):

.test { 
    color: black; 

    .toto { 
     background: blue; 
     font-size: 12px; 
     text-align: center; 
    } 
} 

我希望能夠從塊A和B提取下列常見SCSS代碼:

.test { 
    .toto { 
     font-size: 12px; 
    } 
} 

這似乎是一個簡單的TA sk做的,但是有很多長scss文件的列表,手動執行它真的很痛苦。搜索了一段時間後,我沒有找到任何工具。

一箇中間解決方案可以將sass代碼轉換爲多維的關聯數組,並處理數組以找到交集,但是我找不到任何簡單的解決方案來做到這一點,所以任何幫助,將不勝感激。

+0

你想只維護相等的屬性還是刪除那些重複的屬性? – blonfu

回答

0

有幾個方法,但在這種情況下,我會選擇一個變量:

$base-font-size: 12px; 

.test { 
    color: white; 

    .toto { 
     background: red; 
     font-size: $base-font-size; 
    } 
} 

.test { 
    color: black; 

    .toto { 
     background: blue; 
     font-size: $base-font-size; 
     text-align: center; 
    } 
} 

或者你可以用一些默認添加TOTO混入和使用:

@mixin toto($background: red, $text-align: left, $font-size: 12px) { 
    .toto { 
     background: $background; 
     text-align: $text-align; 
     font-size: $font-size; 
    } 
} 


.test { 
    color: white; 
    @include toto(); 
} 

.test { 
    color: black; 
    @include toto(blue, center); 
} 

編輯:或使用擴展:

.font-size-12 { 
    font-size: 12px; 
} 

.test { 
    color: white; 

    .toto { 
     @extend .font-size-12; 
     background: red; 
    } 
} 

.test { 
    color: black; 

    .toto { 
     @extend .font-size-12; 
     background: blue; 
     text-align: center; 
    } 
} 
+0

感謝您的回答,但我需要的不是手動重寫此代碼的方式,而更像是一個閱讀文件列表的腳本,並給出了所有文件中的常見規則。就像「extract-scss-intersection --output = intersection.scss file1.scss file2.scss ... fileN.scss」,它會將file1解析爲fileN並輸出intersection.scss – vbourdeix

+0

那麼您可以將它們加載到PHP腳本中或無論你感到舒服,把它們變成數組 - 然後你可以刪除具有匹配鍵的元素並重寫這些文件... http://stackoverflow.com/questions/6205017/parse-a-string-of-css-declarations-並將其轉換爲數組 – Bat

+0

這確實是一個想法,這意味着我需要找到一個可行的Sass解析器,或者實現一個解析器(但這聽起來像一個棘手的任務)。如果你知道一個好的,能夠將一個sass文件轉換成一個PHP多維數組,請讓我知道:) – vbourdeix

相關問題