forked from One-com/livestyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
less.js
64 lines (54 loc) · 2.28 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
/*global less, console*/
define([
'css'
], function (css) {
var lessLoader = {
load: function (name, req, load, config) {
var url = name + (name.indexOf('.less') === -1 ? '.less' : ''),
path = url.split('/');
path.pop();
if (path.length !== 0) {
path = '/' + path.join('/') + '/';
}
$.ajax({
url: url,
dataType: 'text',
success: function (data) {
var parser = new less.Parser({
paths: [path]
});
parser.parse(data, function (e, tree) {
var style = null,
css = null,
lessLink;
if (e) {
console.error(e);
} else {
style = document.createElement('style');
style.type = "text/css";
style.id = 'less:' + url.replace(/\//g, '-').replace('.less', '');
css = tree.toCSS();
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.innerHTML = css;
}
document.getElementsByTagName('head')[0].appendChild(style);
lessLink = document.createElement('link');
lessLink.rel = 'stylesheet/less';
lessLink.type = 'text/css';
lessLink.href = url;
document.getElementsByTagName('head')[0].appendChild(lessLink);
less.sheets.push(lessLink);
load(css);
}
});
}
});
}
};
if (window.liveStyle && window.liveStyle.compiless) {
lessLoader = css;
}
return lessLoader;
});