mirror of
https://github.com/less/less.js.git
synced 2026-01-23 22:27:57 -05:00
Merge pull request #2243 from lejenome/master
Support reading less options from its script tag data attributes
This commit is contained in:
@@ -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
|
||||
@@ -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');
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user