[Pkg-javascript-commits] [less.js] 250/285: seperate out bootstrapping less and the browser api

Jonas Smedegaard dr at jones.dk
Mon Oct 26 23:23:59 UTC 2015


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag v2.0.0
in repository less.js.

commit 9ddd81fc070ac579d31a0e19c536433794dd4dee
Author: Luke Page <luke.a.page at gmail.com>
Date:   Sat Nov 1 17:05:21 2014 +0000

    seperate out bootstrapping less and the browser api
---
 Gruntfile.js                            |  2 +-
 lib/less-browser/add-default-options.js | 42 ++++++++++++++++
 lib/less-browser/bootstrap.js           | 24 +++++++++
 lib/less-browser/browser.js             |  7 +++
 lib/less-browser/file-manager.js        |  7 ++-
 lib/less-browser/index.js               | 86 ++++++---------------------------
 lib/less-browser/utils.js               |  9 ++--
 7 files changed, 98 insertions(+), 79 deletions(-)

diff --git a/Gruntfile.js b/Gruntfile.js
index bd7957a..2d1ba50 100644
--- a/Gruntfile.js
+++ b/Gruntfile.js
@@ -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"],
diff --git a/lib/less-browser/add-default-options.js b/lib/less-browser/add-default-options.js
new file mode 100644
index 0000000..6c0fb7a
--- /dev/null
+++ b/lib/less-browser/add-default-options.js
@@ -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;
+    }
+
+};
diff --git a/lib/less-browser/bootstrap.js b/lib/less-browser/bootstrap.js
new file mode 100644
index 0000000..766665f
--- /dev/null
+++ b/lib/less-browser/bootstrap.js
@@ -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');
+    }
+);
diff --git a/lib/less-browser/browser.js b/lib/less-browser/browser.js
index fdb1d06..13e8671 100644
--- a/lib/less-browser/browser.js
+++ b/lib/less-browser/browser.js
@@ -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];
+        })();
     }
 };
diff --git a/lib/less-browser/file-manager.js b/lib/less-browser/file-manager.js
index 680c45b..867f649 100644
--- a/lib/less-browser/file-manager.js
+++ b/lib/less-browser/file-manager.js
@@ -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 {
diff --git a/lib/less-browser/index.js b/lib/less-browser/index.js
index ea32ee0..9741f41 100644
--- a/lib/less-browser/index.js
+++ b/lib/less-browser/index.js
@@ -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;
+};
diff --git a/lib/less-browser/utils.js b/lib/less-browser/utils.js
index 5a77108..31ef9a5 100644
--- a/lib/less-browser/utils.js
+++ b/lib/less-browser/utils.js
@@ -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;
+        }
     }
 };

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/less.js.git



More information about the Pkg-javascript-commits mailing list