-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathindex.js
108 lines (76 loc) · 2.81 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// http://stackoverflow.com/questions/14970224/anyone-know-of-a-good-way-to-convert-from-less-to-sass
function Less2Sass(){
}
Less2Sass.prototype.convert = function(file) {
this.file = file;
this.convertInterpolatedVariables()
.convertVariables()
.convertTildaStrings()
.convertMixins()
.includeMixins()
.convertExtend()
.convertColourHelpers()
.convertFileExtensions()
.convertFunctionUnit();
return this.file;
};
Less2Sass.prototype.includeMixins = function() {
var includeRegex = /^(\s*)\.([a-zA-Z][\w\-]*\(?[^;{}]*\)?;{1}$)/gm;
this.file = this.file.replace(includeRegex, '$1@include $2');
return this;
};
Less2Sass.prototype.convertMixins = function() {
// Simple form: no semicolons.
const mixinRegexNoSemicolon = /^(\s*?)\.([\w\-]*?)\s*\(([\s\S][^\;]+?)?\)\s*\{$/gm;
this.file = this.file.replace(mixinRegexNoSemicolon, '$1@mixin $2($3) {');
// With semicolons.
const mixinRegexWithSemicolon = /^(\s*?)\.([\w\-]*?)\s*\(([\s\S][^\,]+?)?\)\s*\{$/gm;
this.file = this.file.replace(mixinRegexWithSemicolon, function (match, g1, g2, g3) {
return g1 + '@mixin ' + g2 + '(' + g3.replace(/;/g, ',') + ') {';
});
return this;
};
Less2Sass.prototype.convertFunctionUnit = function() {
// Two-args.
const unitTwoArgRegex = /unit\((\S+),(\S+)\)/g;
this.file = this.file.replace(unitTwoArgRegex, '0$2 + $1');
// One-arg.
const unitOneArgRegex = /unit\(([^,]+)\)/g;
this.file = this.file.replace(unitOneArgRegex, 'unit-less($1)');
return this;
};
Less2Sass.prototype.convertExtend = function() {
// http://lesscss.org/features/#extend-feature
// &:extend syntax.
const andExtendRegex = /&:extend\((.[\w]*)\);/g;
this.file = this.file.replace(andExtendRegex, '@extend $1;');
return this;
};
Less2Sass.prototype.convertColourHelpers = function() {
var helperRegex = /spin\(/g;
this.file = this.file.replace(helperRegex, 'adjust-hue(');
// TODO (EK): Flag other colour helpers for manual conversion that SASS does not have
return this;
};
Less2Sass.prototype.convertTildaStrings = function() {
var tildaRegex = /~("|')/g;
this.file = this.file.replace(tildaRegex, '$1');
return this;
};
Less2Sass.prototype.convertInterpolatedVariables = function() {
var interpolationRegex = /@\{(?!(\s|\())/g;
this.file = this.file.replace(interpolationRegex, '#{$');
return this;
};
Less2Sass.prototype.convertVariables = function() {
// Matches any @ that doesn't have 'media ' or 'import ' after it.
var atRegex = /@(?!(media|import|mixin|font-face|keyframes)(\s|\())/g;
this.file = this.file.replace(atRegex, '$');
return this;
};
Less2Sass.prototype.convertFileExtensions = function() {
var extensionRegex = /\.less/g;
this.file = this.file.replace(extensionRegex, '.scss');
return this;
};
module.exports = new Less2Sass();