Skip to content

Commit

Permalink
add support to read options from less file link tag data attr
Browse files Browse the repository at this point in the history
  • Loading branch information
lejenome committed Oct 22, 2014
1 parent e2b30eb commit 815e977
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
8 changes: 6 additions & 2 deletions lib/less-browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ var isFileProtocol = /^(file|chrome(-extension)?|resource|qrc|app):/.test(window
options = window.less || {};

// use options from the current script tag data attribues
options = addDataAttr(options);
var script = document.currentScript || (function() {
var scripts = document.getElementsByTagName("script");
return scripts[scripts.length - 1];
})();
options = addDataAttr(options, script);

// shim Promise if required
require('promise/polyfill.js');
Expand Down Expand Up @@ -127,7 +131,7 @@ function loadStyles(modifyVars) {

function loadStyleSheet(sheet, callback, reload, remaining, modifyVars) {

var instanceOptions = clone(options);
var instanceOptions = addDataAttr(clone(options), sheet);
instanceOptions.mime = sheet.type;

if (modifyVars) {
Expand Down
15 changes: 5 additions & 10 deletions lib/less-browser/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* global document */
module.exports = {
extractId: function(href) {
return href.replace(/^[a-z-]+:\/+?[^\/]+/, '' ) // Remove protocol & domain
Expand All @@ -7,17 +6,13 @@ module.exports = {
.replace(/[^\.\w-]+/g, '-') // Replace illegal characters
.replace(/\./g, ':'); // Replace dots with colons(for valid id)
},
addDataAttr: function(options) {
var script = document.currentScript || (function() {
var scripts = document.getElementsByTagName("script");
return scripts[scripts.length - 1];
})();
for (var opt in script.dataset)
if (script.dataset.hasOwnProperty(opt)) {
addDataAttr: function(options, tag) {
for (var opt in tag.dataset)
if (tag.dataset.hasOwnProperty(opt)) {
if (opt === "env" || opt === "dumpLineNumbers" || opt === "rootpath" || opt === "errorReporting")
options[opt] = script.dataset[opt];
options[opt] = tag.dataset[opt];
else
options[opt] = JSON.parse(script.dataset[opt]);
options[opt] = JSON.parse(tag.dataset[opt]);
}
return options;
}
Expand Down

0 comments on commit 815e977

Please sign in to comment.