Skip to content

Commit e56af60

Browse files
committedNov 21, 2012
Added requirejs css and less module loader examples that work with livestyle
1 parent 3de6ce4 commit e56af60

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed
 

‎README.md

+21
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,27 @@ Currently there are some troubles with updating stylesheets using
110110
http://www.stevesouders.com/blog/2009/04/09/dont-use-import/
111111

112112

113+
Module loaders
114+
==============
115+
Livestyle supports asynchronous loading and injection of stylesheets.
116+
If you are using requirejs you might want to take a look at css.js and
117+
less.js, wich can be used as module loaders for both less and css
118+
files using requirejs like so:
119+
120+
``` javascript
121+
define([
122+
'less!bootstrap/theme.less',
123+
'css!styles/myLoginBox.css'
124+
], function () {
125+
// My module depending on certain styles
126+
})
127+
```
128+
129+
These two loaders are both usable without livestyle.
130+
The less.js loader will change behavior depending on wether you have
131+
set the `--compiless` flag for livestyle to make live updates possible.
132+
133+
113134
CSS preprocessors
114135
=================
115136
Since livestyle watches the css files that are actually served to the

‎css.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
define({
2+
load: function (name, req, load, config, map) {
3+
var url = name,
4+
path = url.split('/'),
5+
linkElement = document.createElement('link'),
6+
headElement = document.getElementsByTagName('head')[0],
7+
parentModuleName = map && map.parentMap && map.parentMap.name,
8+
parentScriptElement,
9+
i,
10+
childNode;
11+
12+
linkElement.setAttribute('href', url);
13+
linkElement.setAttribute('rel', 'stylesheet');
14+
15+
if (parentModuleName) {
16+
for (i = 0 ; i < headElement.childNodes.length ; i += 1) {
17+
childNode = headElement.childNodes[i];
18+
if (childNode.nodeType === 1 && childNode.nodeName.toLowerCase() === 'script' && childNode.getAttribute('data-requiremodule') === parentModuleName) {
19+
parentScriptElement = childNode;
20+
break;
21+
}
22+
}
23+
}
24+
25+
if (parentScriptElement) {
26+
headElement.insertBefore(linkElement, parentScriptElement);
27+
} else {
28+
headElement.appendChild(linkElement);
29+
}
30+
load();
31+
}
32+
});

‎less.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*global less, console*/
2+
define([
3+
'css'
4+
], function (css) {
5+
var lessLoader = {
6+
load: function (name, req, load, config) {
7+
var url = name + (name.indexOf('.less') === -1 ? '.less' : ''),
8+
path = url.split('/');
9+
10+
path.pop();
11+
if (path.length !== 0) {
12+
path = '/' + path.join('/') + '/';
13+
}
14+
15+
$.ajax({
16+
url: url,
17+
success: function (data) {
18+
var parser = new less.Parser({
19+
paths: [path]
20+
});
21+
22+
parser.parse(data, function (e, tree) {
23+
var style = null,
24+
css = null,
25+
lessLink;
26+
27+
if (e) {
28+
console.error(e);
29+
} else {
30+
style = document.createElement('style');
31+
style.type = "text/css";
32+
style.id = 'less:' + url.replace(/\//g, '-').replace('.less', '');
33+
34+
css = tree.toCSS();
35+
36+
if (style.styleSheet) {
37+
style.styleSheet.cssText = css;
38+
} else {
39+
style.innerHTML = css;
40+
}
41+
document.getElementsByTagName('head')[0].appendChild(style);
42+
43+
lessLink = document.createElement('link');
44+
lessLink.rel = 'stylesheet/less';
45+
lessLink.type = 'text/css';
46+
lessLink.href = url;
47+
document.getElementsByTagName('head')[0].appendChild(lessLink);
48+
less.sheets.push(lessLink);
49+
50+
load(css);
51+
}
52+
});
53+
}
54+
});
55+
}
56+
};
57+
58+
if (window.liveStyle && window.liveStyle.compiless) {
59+
lessLoader = css;
60+
}
61+
62+
return lessLoader;
63+
});

‎livestyle-client.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
'use strict';
44

55
var liveStyleOptions = {}; // The options will be injected by the server
6+
window.liveStyle = liveStyleOptions; // Make it detectable on the client that livestyle is in use
67

78
function log(msg) {
89
if (liveStyleOptions.debug && window.console) {

0 commit comments

Comments
 (0)