forked from ekryski/less2sass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (94 loc) · 3.49 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// 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()
.convertReferenceImports()
.convertUrlImports()
.convertAbsoluteImports()
.convertVariables()
.convertTildaStrings()
.includeMixins()
.convertMixins()
.convertExtend()
.convertColourHelpers()
.convertFileExtensions()
.convertFunctionUnit();
return this.file;
};
Less2Sass.prototype.convertReferenceImports = function() {
var includeRegex = /^@import\s*\(reference\)/gm;
this.file = this.file.replace(includeRegex, '@import');
return this;
};
Less2Sass.prototype.convertUrlImports = function() {
var includeRegex = /^@import url\((.*)\);/gm;
this.file = this.file.replace(includeRegex, '@import $1;');
return this;
};
Less2Sass.prototype.convertAbsoluteImports = function() {
var includeRegex = /^@import '@kr-modules\//gm;
this.file = this.file.replace(includeRegex, '@import \'~@kr-modules/');
return this;
};
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|kr-modules|supports|value)(\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();