forked from steelydylan/gulp-less-to-scss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (78 loc) · 2.78 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var through = require('through2');
var gutil = require('gulp-util');
var ext = gutil.replaceExtension;
module.exports = function(){
var lessToScss = function (file,enc,cb){
var content = file.contents.toString();
//import先をlessからscssに
content = content.replace(/\/less\//g, '/scss/')
.replace(/\.less/g, '.scss')
//@を$に変換
.replace(/@/g, '$')
//argbを除去
.replace(/\%\((.*?)\);/g,function(all){
var arr = all.match(/argb\(.*?\)/g);
if(arr instanceof Array){
for(var i = 0,n = arr.length; i < n; i++){
arr[i] = arr[i].replace(/argb\(\$(.*?)\)/g,"${$1}");
}
}
all = all.replace(/,argb\(.*?\)/g,"");
var i = -1;
all = all.replace(/\%d/g,function(al){
i++;
return arr[i];
});
all = all.replace(/\%/,'');
return all;
})
.replace(/ e\(/g, ' unquote(')
//@mixinの定義
.replace(/\.([\w\-]*)\s*\((.*)\)\s*\{/g, '@mixin $1($2){')
.replace(/@mixin\s*([\w\-]*)\s*\((.*)\)\s*\{\s*\}/g, '// @mixin $1($2){}')
.replace(/@mixin\s*([\w\-]*)\s*\((.*);(.*)\)/g,function(all){
all = all.replace(/;/g,',');
return all;
})
//@includeの設定
.replace(/(\s)\.(hook[a-zA-Z\-\d]+);/g, '$1@include $2();')
.replace(/(\s)\.([\w\-]*)\s*\((.*)\);*/g,'$1@include $2($3);')
.replace(/(\s)\.([^\d\s\"]+);/g,'$1@include $2;')
//@includeの引き数の分割を;から,に
.replace(/@include\s*([\w\-]*)\s*\((.*);(.*)\)/g,function(all){
all = all.replace(/;/g,',');
return all;
})
//@includeにおける!importantの位置
.replace(/\).*?;!important/," !important)")
.replace(/\$(import|charset|media|font-face|page[\s:]|-ms-viewport|keyframes|-webkit-keyframes|-moz-keyframes|-o-keyframes|-moz-document)/g, '@$1')
.replace(/\$\{/g, '#{$')
.replace(/~("[^"]+")/g, 'unquote($1)')
//extend定義
.replace(/&:extend\((.*?)\)/g,"@extend $1")
.replace(/@extend\s*(.*?)\s*?all;/g,"@extend $1;")
//spin関数をadjust-hueに変換
.replace(/([\W])spin\(/g,'$1adjust-hue(')
.replace(/(\W)fade\(([^)]+?)% *\)/g,'$1rgba($2%/100.0%)')
.replace(/(\W)fade\(/g,'$1rgba(')
//~を除去
.replace(/~(\s*['"])/g,'$1')
//&が単語にくっついていたら話す
.replace(/(.[\w\-]+?)&/g,"$1 &")
//namespaceを定義
.replace(/#([\w\-]*)\s*\{([^\}]*@mixin[\s\S]*)\}/g,function(all,$1,$2){
all = all.replace(/#[\w\-]*\s*\{([^\}]*@mixin[\s\S]*)\}/,"$1");
all = all.replace(/@mixin\s*([\w\-]*)/g,"@mixin "+$1+"_$1");
return all;
})
//namespaceをinclude
.replace(/#([\w\-]*)\s*>\s@include\s([\w\-]*)\((.*)\);/g,"@include $1_$2($3);")
//handle multiple & in a row
.replace(/&(&+)/g, function(match, p1){return "&" + p1.replace(/&/g,"#{&}")});
file.contents = new Buffer(content);
file.path = ext(file.path, '.scss');
this.push(file);
cb();
}
return through.obj(lessToScss)
}