Skip to content

Commit

Permalink
Merge pull request #2243 from lejenome/master
Browse files Browse the repository at this point in the history
Support reading less options from its script tag data attributes
  • Loading branch information
lukeapage committed Oct 23, 2014
2 parents 72cc9e5 + 815e977 commit bbbbd5d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/less-browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/*global window, document, location */

var less;
var addDataAttr = require("./utils").addDataAttr;

/*
TODO - options is now hidden - we should expose it on the less object, but not have it "as" the less object
Expand All @@ -14,6 +15,13 @@ var less;
var isFileProtocol = /^(file|chrome(-extension)?|resource|qrc|app):/.test(window.location.protocol),
options = window.less || {};

// use options from the current script tag data attribues
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 @@ -123,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
10 changes: 10 additions & 0 deletions lib/less-browser/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,15 @@ module.exports = {
.replace(/\.[a-zA-Z]+$/, '' ) // Remove simple extension
.replace(/[^\.\w-]+/g, '-') // Replace illegal characters
.replace(/\./g, ':'); // Replace dots with colons(for valid id)
},
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] = tag.dataset[opt];
else
options[opt] = JSON.parse(tag.dataset[opt]);
}
return options;
}
};

0 comments on commit bbbbd5d

Please sign in to comment.