-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
less.js
91 lines (74 loc) · 2.51 KB
/
less.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
define(['require'], function(require) {
var lessAPI = {};
lessAPI.pluginBuilder = './less-builder';
if (typeof window == 'undefined') {
lessAPI.load = function(n, r, load) { load(); };
return lessAPI;
}
lessAPI.normalize = function(name, normalize) {
if (name.substr(name.length - 5, 5) == '.less')
name = name.substr(0, name.length - 5);
name = normalize(name);
return name;
};
var head = document.getElementsByTagName('head')[0];
var base = document.getElementsByTagName('base');
base = base && base[0] && base[0] && base[0].href;
var pagePath = (base || window.location.href.split('#')[0].split('?')[0]).split('/');
pagePath[pagePath.length - 1] = '';
pagePath = pagePath.join('/');
var styleCnt = 0;
var curStyle;
lessAPI.inject = function(css) {
if (styleCnt < 31) {
curStyle = document.createElement('style');
curStyle.type = 'text/css';
head.appendChild(curStyle);
styleCnt++;
}
if (curStyle.styleSheet)
curStyle.styleSheet.cssText += css;
else
curStyle.appendChild(document.createTextNode(css));
};
lessAPI.load = function(lessId, req, load, config) {
window.less = config.less || {};
window.less.env = 'development';
require(['./lessc', './normalize'], function(lessc, normalize) {
var fileUrl = req.toUrl(lessId + '.less');
fileUrl = normalize.absoluteURI(fileUrl, pagePath);
//make it compatible with v1 and v2
var generation = (lessc.version || [1])[0];
var renderer;
var cssGetter;
if (generation === 1) {
//v1, use parser and toCSS
var parser = new lessc.Parser(window.less);
renderer = function (input, cb) {
parser.parse.call(parser, input, cb, window.less);
};
cssGetter = function (tree) {
return tree.toCSS(config.less);
};
} else if (generation >= 2) {
//v2 or newer, use render and output
renderer = function (input, cb) {
lessc.render(input, window.less, cb);
};
cssGetter = function (output) {
return output.css;
};
}
renderer('@import (multiple) "' + fileUrl + '";', function(err, output) {
if (err) {
console.log(err + ' at ' + fileUrl + ', line ' + err.line);
return load.error(err);
}
var css = cssGetter(output);
lessAPI.inject(normalize(css, fileUrl, pagePath));
setTimeout(load, 7);
}, window.less);
});
};
return lessAPI;
});