mirror of
https://github.com/less/less.js.git
synced 2026-04-09 03:00:20 -04:00
seperate out bootstrapping less and the browser api
This commit is contained in:
@@ -46,7 +46,7 @@ module.exports = function (grunt) {
|
||||
|
||||
browserify: {
|
||||
browser: {
|
||||
src: ['./lib/less-browser/index.js'],
|
||||
src: ['./lib/less-browser/bootstrap.js'],
|
||||
options: {
|
||||
exclude: ["promise"],
|
||||
require: ["promise/polyfill.js"],
|
||||
|
||||
42
lib/less-browser/add-default-options.js
Normal file
42
lib/less-browser/add-default-options.js
Normal file
@@ -0,0 +1,42 @@
|
||||
var addDataAttr = require("./utils").addDataAttr,
|
||||
browser = require("./browser");
|
||||
|
||||
module.exports = function(window, options) {
|
||||
|
||||
// use options from the current script tag data attribues
|
||||
addDataAttr(options, browser.currentScript(window));
|
||||
|
||||
if (options.isFileProtocol === undefined) {
|
||||
options.isFileProtocol = /^(file|chrome(-extension)?|resource|qrc|app):/.test(window.location.protocol);
|
||||
}
|
||||
|
||||
// Load styles asynchronously (default: false)
|
||||
//
|
||||
// This is set to `false` by default, so that the body
|
||||
// doesn't start loading before the stylesheets are parsed.
|
||||
// Setting this to `true` can result in flickering.
|
||||
//
|
||||
options.async = options.async || false;
|
||||
options.fileAsync = options.fileAsync || false;
|
||||
|
||||
// Interval between watch polls
|
||||
options.poll = options.poll || (options.isFileProtocol ? 1000 : 1500);
|
||||
|
||||
options.env = options.env || (window.location.hostname == '127.0.0.1' ||
|
||||
window.location.hostname == '0.0.0.0' ||
|
||||
window.location.hostname == 'localhost' ||
|
||||
(window.location.port &&
|
||||
window.location.port.length > 0) ||
|
||||
options.isFileProtocol ? 'development'
|
||||
: 'production');
|
||||
|
||||
var dumpLineNumbers = /!dumpLineNumbers:(comments|mediaquery|all)/.exec(window.location.hash);
|
||||
if (dumpLineNumbers) {
|
||||
options.dumpLineNumbers = dumpLineNumbers[1];
|
||||
}
|
||||
|
||||
if (options.useFileCache === undefined) {
|
||||
options.useFileCache = true;
|
||||
}
|
||||
|
||||
};
|
||||
24
lib/less-browser/bootstrap.js
vendored
Normal file
24
lib/less-browser/bootstrap.js
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Kicks off less and compiles any stylesheets
|
||||
* used in the browser distributed version of less
|
||||
* to kick-start less using the browser api
|
||||
*/
|
||||
/*global window */
|
||||
|
||||
// shim Promise if required
|
||||
require('promise/polyfill.js');
|
||||
|
||||
var options = window.less || {};
|
||||
require("./add-default-options")(window, options);
|
||||
|
||||
var less = module.exports = require("./index")(window, options);
|
||||
|
||||
if (/!watch/.test(window.location.hash)) {
|
||||
less.watch();
|
||||
}
|
||||
|
||||
less.pageLoadFinished = less.registerStylesheets().then(
|
||||
function () {
|
||||
return less.refresh(less.env === 'development');
|
||||
}
|
||||
);
|
||||
@@ -53,5 +53,12 @@ module.exports = {
|
||||
throw new Error("Couldn't reassign styleSheet.cssText.");
|
||||
}
|
||||
}
|
||||
},
|
||||
currentScript: function(window) {
|
||||
var document = window.document;
|
||||
return document.currentScript || (function() {
|
||||
var scripts = document.getElementsByTagName("script");
|
||||
return scripts[scripts.length - 1];
|
||||
})();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*global window, XMLHttpRequest */
|
||||
|
||||
module.exports = function(options, isFileProtocol, logger) {
|
||||
module.exports = function(options, logger) {
|
||||
|
||||
var PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise,
|
||||
AbstractFileManager = require("../less/environment/abstract-file-manager.js");
|
||||
@@ -8,7 +8,6 @@ var PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : P
|
||||
var fileCache = {};
|
||||
|
||||
//TODOS - move log somewhere. pathDiff and doing something similar in node. use pathDiff in the other browser file for the initial load
|
||||
// isFileProtocol is global
|
||||
|
||||
function getXMLHttpRequest() {
|
||||
if (window.XMLHttpRequest && (window.location.protocol !== "file:" || !("ActiveXObject" in window))) {
|
||||
@@ -41,7 +40,7 @@ FileManager.prototype.join = function join(basePath, laterPath) {
|
||||
FileManager.prototype.doXHR = function doXHR(url, type, callback, errback) {
|
||||
|
||||
var xhr = getXMLHttpRequest();
|
||||
var async = isFileProtocol ? options.fileAsync : options.async;
|
||||
var async = options.isFileProtocol ? options.fileAsync : options.async;
|
||||
|
||||
if (typeof(xhr.overrideMimeType) === 'function') {
|
||||
xhr.overrideMimeType('text/css');
|
||||
@@ -60,7 +59,7 @@ FileManager.prototype.doXHR = function doXHR(url, type, callback, errback) {
|
||||
}
|
||||
}
|
||||
|
||||
if (isFileProtocol && !options.fileAsync) {
|
||||
if (options.isFileProtocol && !options.fileAsync) {
|
||||
if (xhr.status === 0 || (xhr.status >= 200 && xhr.status < 300)) {
|
||||
callback(xhr.responseText);
|
||||
} else {
|
||||
|
||||
@@ -1,77 +1,30 @@
|
||||
//
|
||||
// browser.js - client-side engine
|
||||
// index.js
|
||||
// Should expose the additional browser functions on to the less object
|
||||
//
|
||||
/*global window, document, location */
|
||||
var addDataAttr = require("./utils").addDataAttr,
|
||||
browser = require("./browser");
|
||||
|
||||
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
|
||||
less = { fileAsync: true }
|
||||
then access as less.environment.options.fileAsync ?
|
||||
*/
|
||||
|
||||
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');
|
||||
|
||||
module.exports = less = require('../less')();
|
||||
module.exports = function(window, options) {
|
||||
var document = window.document;
|
||||
var less = require('../less')();
|
||||
module.exports = less;
|
||||
less.options = options;
|
||||
var environment = less.environment,
|
||||
FileManager = require("./file-manager")(options, isFileProtocol, less.logger),
|
||||
FileManager = require("./file-manager")(options, less.logger),
|
||||
fileManager = new FileManager();
|
||||
environment.addFileManager(fileManager);
|
||||
less.FileManager = FileManager;
|
||||
|
||||
require("./log-listener")(less, options);
|
||||
var errors = require("./error-reporting")(window, less, options);
|
||||
var browser = require("./browser");
|
||||
var cache = less.cache = options.cache || require("./cache")(window, options, less.logger);
|
||||
|
||||
options.env = options.env || (window.location.hostname == '127.0.0.1' ||
|
||||
window.location.hostname == '0.0.0.0' ||
|
||||
window.location.hostname == 'localhost' ||
|
||||
(window.location.port &&
|
||||
window.location.port.length > 0) ||
|
||||
isFileProtocol ? 'development'
|
||||
: 'production');
|
||||
|
||||
// Load styles asynchronously (default: false)
|
||||
//
|
||||
// This is set to `false` by default, so that the body
|
||||
// doesn't start loading before the stylesheets are parsed.
|
||||
// Setting this to `true` can result in flickering.
|
||||
//
|
||||
options.async = options.async || false;
|
||||
options.fileAsync = options.fileAsync || false;
|
||||
|
||||
// Interval between watch polls
|
||||
options.poll = options.poll || (isFileProtocol ? 1000 : 1500);
|
||||
|
||||
//Setup user functions
|
||||
if (options.functions) {
|
||||
less.functions.functionRegistry.addMultiple(options.functions);
|
||||
}
|
||||
|
||||
var dumpLineNumbers = /!dumpLineNumbers:(comments|mediaquery|all)/.exec(location.hash);
|
||||
if (dumpLineNumbers) {
|
||||
options.dumpLineNumbers = dumpLineNumbers[1];
|
||||
}
|
||||
|
||||
if (options.useFileCache === undefined) {
|
||||
options.useFileCache = true;
|
||||
}
|
||||
|
||||
var typePattern = /^text\/(x-)?less$/;
|
||||
|
||||
function postProcessCSS(styles) {
|
||||
@@ -132,7 +85,8 @@ function loadStyles(modifyVars) {
|
||||
|
||||
function loadStyleSheet(sheet, callback, reload, remaining, modifyVars) {
|
||||
|
||||
var instanceOptions = addDataAttr(clone(options), sheet);
|
||||
var instanceOptions = clone(options);
|
||||
addDataAttr(instanceOptions, sheet);
|
||||
instanceOptions.mime = sheet.type;
|
||||
|
||||
if (modifyVars) {
|
||||
@@ -224,10 +178,6 @@ less.watch = function () {
|
||||
|
||||
less.unwatch = function () {clearInterval(less.watchTimer); this.watchMode = false; return false; };
|
||||
|
||||
if (/!watch/.test(location.hash)) {
|
||||
less.watch();
|
||||
}
|
||||
|
||||
//
|
||||
// Get all <link> tags with the 'rel' attribute set to "stylesheet/less"
|
||||
//
|
||||
@@ -252,11 +202,11 @@ less.registerStylesheets = function() {
|
||||
// CSS without reloading less-files
|
||||
//
|
||||
less.modifyVars = function(record) {
|
||||
return less.refresh(false, record);
|
||||
return less.refresh(true, record, false);
|
||||
};
|
||||
|
||||
less.refresh = function (reload, modifyVars) {
|
||||
if (reload) {
|
||||
less.refresh = function (reload, modifyVars, clearFileCache) {
|
||||
if ((reload || clearFileCache) && clearFileCache !== false) {
|
||||
fileManager.clearFileCache();
|
||||
}
|
||||
return new Promise(function (resolve, reject) {
|
||||
@@ -295,9 +245,5 @@ less.refresh = function (reload, modifyVars) {
|
||||
};
|
||||
|
||||
less.refreshStyles = loadStyles;
|
||||
|
||||
less.pageLoadFinished = less.registerStylesheets().then(
|
||||
function () {
|
||||
return less.refresh(less.env === 'development');
|
||||
}
|
||||
);
|
||||
return less;
|
||||
};
|
||||
|
||||
@@ -7,13 +7,14 @@ module.exports = {
|
||||
.replace(/\./g, ':'); // Replace dots with colons(for valid id)
|
||||
},
|
||||
addDataAttr: function(options, tag) {
|
||||
for (var opt in tag.dataset)
|
||||
for (var opt in tag.dataset) {
|
||||
if (tag.dataset.hasOwnProperty(opt)) {
|
||||
if (opt === "env" || opt === "dumpLineNumbers" || opt === "rootpath" || opt === "errorReporting")
|
||||
if (opt === "env" || opt === "dumpLineNumbers" || opt === "rootpath" || opt === "errorReporting") {
|
||||
options[opt] = tag.dataset[opt];
|
||||
else
|
||||
} else {
|
||||
options[opt] = JSON.parse(tag.dataset[opt]);
|
||||
}
|
||||
}
|
||||
return options;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user