Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reading less options from its script tag data attributes #2243

Merged
merged 4 commits into from
Oct 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
};