[Pkg-javascript-commits] [less.js] 75/285: Clean up dependency injection so environment is injected at the top level

Jonas Smedegaard dr at jones.dk
Mon Oct 26 23:23:40 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 ca72b4d92fc7fb769ffd6e2360967521666a8f69
Author: Luke Page <luke.a.page at gmail.com>
Date:   Tue Aug 26 22:26:27 2014 +0100

    Clean up dependency injection so environment is injected at the top level
---
 lib/less/browser.js             | 25 +++++++++++++++++--------
 lib/less/environment/browser.js |  8 ++++----
 lib/less/functions/data-uri.js  | 14 +++++++-------
 lib/less/functions/index.js     |  6 +++---
 lib/less/functions/svg.js       |  4 ++--
 lib/less/index.js               |  5 ++---
 lib/less/non-node-index.js      | 26 +++++++++++++-------------
 lib/less/parser/imports.js      | 12 ++++++------
 lib/less/parser/parser.js       | 15 ++++++++-------
 lib/less/source-map-output.js   |  6 +++---
 lib/less/tree/call.js           |  4 ++--
 11 files changed, 67 insertions(+), 58 deletions(-)

diff --git a/lib/less/browser.js b/lib/less/browser.js
index 61d7691..e254735 100644
--- a/lib/less/browser.js
+++ b/lib/less/browser.js
@@ -3,9 +3,6 @@
 //
 /*global window, document, location */
 
-var less = require('./non-node-index.js'),
-    options = window.less;
-
 var logLevel = {
     debug: 3,
     info: 2,
@@ -13,16 +10,28 @@ var logLevel = {
     none: 0
 };
 
+var less;
+
 function log(str, level) {
     if (typeof(console) !== 'undefined' && less.logLevel >= level) {
         console.log('less: ' + str);
     }
 }
 
-var isFileProtocol = /^(file|chrome(-extension)?|resource|qrc|app):/.test(location.protocol);
+/*
+  TODO - options is now hidden - we should expose it on the less object, but not have it "as" the less object
+         alternately even have it on environment
+         e.g. less.environment.options.fileAsync = true;
+         is it weird you do
+         less = { fileAsync: true }
+         then access as less.environment.options.fileAsync ?
+ */
+
+var isFileProtocol = /^(file|chrome(-extension)?|resource|qrc|app):/.test(location.protocol),
+    options = window.less,
+    environment = require("./environment/browser.js")(options, isFileProtocol, log, logLevel);
 
-less.environment = require("./environment/browser.js")(less, isFileProtocol, log, logLevel);
-window.less = less;
+window.less = less = require('./non-node-index.js')(environment);
 
 less.env = options.env || (location.hostname == '127.0.0.1' ||
                         location.hostname == '0.0.0.0'   ||
@@ -46,8 +55,8 @@ less.logLevel = typeof(options.logLevel) != 'undefined' ? options.logLevel : (le
 // doesn't start loading before the stylesheets are parsed.
 // Setting this to `true` can result in flickering.
 //
-less.async = less.async || false;
-less.fileAsync = less.fileAsync || false;
+options.async = options.async || false;
+options.fileAsync = options.fileAsync || false;
 
 // Interval between watch polls
 less.poll = less.poll || (isFileProtocol ? 1000 : 1500);
diff --git a/lib/less/environment/browser.js b/lib/less/environment/browser.js
index 30d2b5c..85a9f64 100644
--- a/lib/less/environment/browser.js
+++ b/lib/less/environment/browser.js
@@ -1,10 +1,10 @@
 /*global window, XMLHttpRequest */
 
-module.exports = function(less, isFileProtocol, log, logLevel) {
+module.exports = function(options, isFileProtocol, log, logLevel) {
 
 var fileCache = {};
 
-//TODOS - move log somewhere. pathDiff and doing something similiar in node. use pathDiff in the other browser file for the initial load
+//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() {
@@ -135,7 +135,7 @@ return {
     doXHR: function doXHR(url, type, callback, errback) {
 
         var xhr = getXMLHttpRequest();
-        var async = isFileProtocol ? less.fileAsync : less.async;
+        var async = isFileProtocol ? options.fileAsync : options.async;
 
         if (typeof(xhr.overrideMimeType) === 'function') {
             xhr.overrideMimeType('text/css');
@@ -154,7 +154,7 @@ return {
             }
         }
 
-        if (isFileProtocol && !less.fileAsync) {
+        if (isFileProtocol && !options.fileAsync) {
             if (xhr.status === 0 || (xhr.status >= 200 && xhr.status < 300)) {
                 callback(xhr.responseText);
             } else {
diff --git a/lib/less/functions/data-uri.js b/lib/less/functions/data-uri.js
index 47e1273..ad5ac65 100644
--- a/lib/less/functions/data-uri.js
+++ b/lib/less/functions/data-uri.js
@@ -1,11 +1,11 @@
-module.exports = function(less) {
+module.exports = function(environment) {
     var Anonymous = require("../tree/anonymous.js"),
         URL = require("../tree/url.js"),
         functionRegistry = require("./function-registry.js");
 
     functionRegistry.add("data-uri", function(mimetypeNode, filePathNode) {
 
-        if (!less.environment.supportsDataURI(this.env)) {
+        if (!environment.supportsDataURI(this.env)) {
             return new URL(filePathNode || mimetypeNode, this.currentFileInfo).eval(this.env);
         }
 
@@ -27,19 +27,19 @@ module.exports = function(less) {
 
         if (this.env.isPathRelative(filePath)) {
             if (this.currentFileInfo.relativeUrls) {
-                filePath = less.environment.join(this.currentFileInfo.currentDirectory, filePath);
+                filePath = environment.join(this.currentFileInfo.currentDirectory, filePath);
             } else {
-                filePath = less.environment.join(this.currentFileInfo.entryPath, filePath);
+                filePath = environment.join(this.currentFileInfo.entryPath, filePath);
             }
         }
 
         // detect the mimetype if not given
         if (arguments.length < 2) {
 
-            mimetype = less.environment.mimeLookup(this.env, filePath);
+            mimetype = environment.mimeLookup(this.env, filePath);
 
             // use base 64 unless it's an ASCII or UTF-8 format
-            var charset = less.environment.charsetLookup(this.env, mimetype);
+            var charset = environment.charsetLookup(this.env, mimetype);
             useBase64 = ['US-ASCII', 'UTF-8'].indexOf(charset) < 0;
             if (useBase64) { mimetype += ';base64'; }
         }
@@ -47,7 +47,7 @@ module.exports = function(less) {
             useBase64 = /;base64$/.test(mimetype);
         }
 
-        var buf = less.environment.readFileSync(filePath);
+        var buf = environment.readFileSync(filePath);
 
         // IE8 cannot handle a data-uri larger than 32KB. If this is exceeded
         // and the --ieCompat flag is enabled, return a normal url() instead.
diff --git a/lib/less/functions/index.js b/lib/less/functions/index.js
index 261b4bc..ab605b6 100644
--- a/lib/less/functions/index.js
+++ b/lib/less/functions/index.js
@@ -1,4 +1,4 @@
-module.exports = function(less) {
+module.exports = function(environment) {
     var functions = {
         functionRegistry: require("./function-registry.js"),
         functionCaller: require("./function-caller.js")
@@ -8,11 +8,11 @@ module.exports = function(less) {
     require("./default.js");
     require("./color.js");
     require("./color-blending.js");
-    require("./data-uri.js")(less);
+    require("./data-uri.js")(environment);
     require("./math.js");
     require("./number.js");
     require("./string.js");
-    require("./svg.js")(less);
+    require("./svg.js")(environment);
     require("./types.js");
 
     return functions;
diff --git a/lib/less/functions/svg.js b/lib/less/functions/svg.js
index 439a218..2927f0b 100644
--- a/lib/less/functions/svg.js
+++ b/lib/less/functions/svg.js
@@ -1,4 +1,4 @@
-module.exports = function(less) {
+module.exports = function(environment) {
     var Dimension = require("../tree/dimension.js"),
         Color = require("../tree/color.js"),
         Anonymous = require("../tree/anonymous.js"),
@@ -71,7 +71,7 @@ module.exports = function(less) {
 
         if (useBase64) {
             try {
-                returner = less.environment.encodeBase64(this.env, returner);
+                returner = environment.encodeBase64(this.env, returner);
             } catch(e) {
                 useBase64 = false;
             }
diff --git a/lib/less/index.js b/lib/less/index.js
index 5d4a0bd..c83153c 100644
--- a/lib/less/index.js
+++ b/lib/less/index.js
@@ -1,5 +1,6 @@
 var PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise;
-var less = require("./non-node-index.js");
+var environment = require("./environment/node");
+var less = require("./non-node-index.js")(environment);
 
 less.render = function (input, options, callback) {
     options = options || {};
@@ -87,6 +88,4 @@ less.writeError = function (ctx, options) {
     console.error(less.formatError(ctx, options));
 };
 
-less.environment = require("./environment/node");
-
 module.exports = less;
diff --git a/lib/less/non-node-index.js b/lib/less/non-node-index.js
index 9279f8d..0a618c3 100644
--- a/lib/less/non-node-index.js
+++ b/lib/less/non-node-index.js
@@ -1,14 +1,14 @@
-var less = {
-    version: [1, 6, 3],
-    data: require('./data/index.js')
-};
-
-less.tree = require('./tree/index.js');
-less.visitor = require('./visitor/index.js');
-less.Parser = (require('./parser/parser.js'))(less);
-less.functions = require('./functions/index.js')(less);
-less.contexts = require("./contexts.js");
+module.exports = function(environment) {
+    var less = {
+        version: [2, 0, 0],
+        data: require('./data/index.js'),
+        tree: require('./tree/index.js'),
+        visitor: require('./visitor/index.js'),
+        Parser: require('./parser/parser.js')(environment),
+        functions: require('./functions/index.js')(environment),
+        contexts: require("./contexts.js"),
+        environment: environment
+    };
 
-less.SourceMapOutput = require('./source-map-output.js')(less);
-
-module.exports = less;
+    return less;
+};
diff --git a/lib/less/parser/imports.js b/lib/less/parser/imports.js
index bec1ce9..2b7c4ef 100644
--- a/lib/less/parser/imports.js
+++ b/lib/less/parser/imports.js
@@ -1,5 +1,5 @@
 var contexts = require("../contexts.js");
-module.exports = function(less, env, Parser) {
+module.exports = function(environment, env, Parser) {
     var rootFilename = env && env.filename;
     return {
         paths: env.paths || [],  // Search paths, when importing
@@ -32,7 +32,7 @@ module.exports = function(less, env, Parser) {
                 rootFilename: currentFileInfo.rootFilename
             };
 
-            less.environment.loadFile(env, path, currentFileInfo.currentDirectory, function loadFileCallback(e, contents, resolvedFilename) {
+            environment.loadFile(env, path, currentFileInfo.currentDirectory, function loadFileCallback(e, contents, resolvedFilename) {
                 if (e) {
                     fileParsedFunc(e);
                     return;
@@ -46,11 +46,11 @@ module.exports = function(less, env, Parser) {
                 //   then rootpath should become 'less/module/nav/'
                 // - If path of imported file is '../mixins.less' and rootpath is 'less/',
                 //   then rootpath should become 'less/../'
-                newFileInfo.currentDirectory = less.environment.getPath(env, resolvedFilename);
+                newFileInfo.currentDirectory = environment.getPath(env, resolvedFilename);
                 if(newFileInfo.relativeUrls) {
-                    newFileInfo.rootpath = less.environment.join((env.rootpath || ""), less.environment.pathDiff(newFileInfo.currentDirectory, newFileInfo.entryPath));
-                    if (!less.environment.isPathAbsolute(env, newFileInfo.rootpath) && less.environment.alwaysMakePathsAbsolute()) {
-                        newFileInfo.rootpath = less.environment.join(newFileInfo.entryPath, newFileInfo.rootpath);
+                    newFileInfo.rootpath = environment.join((env.rootpath || ""), environment.pathDiff(newFileInfo.currentDirectory, newFileInfo.entryPath));
+                    if (!environment.isPathAbsolute(env, newFileInfo.rootpath) && environment.alwaysMakePathsAbsolute()) {
+                        newFileInfo.rootpath = environment.join(newFileInfo.entryPath, newFileInfo.rootpath);
                     }
                 }
                 newFileInfo.filename = resolvedFilename;
diff --git a/lib/less/parser/parser.js b/lib/less/parser/parser.js
index 06a36d5..8691ec5 100644
--- a/lib/less/parser/parser.js
+++ b/lib/less/parser/parser.js
@@ -5,7 +5,8 @@ var LessError = require('../less-error.js'),
     getImportManager = require("./imports.js"),
     getParserInput = require("./parser-input.js");
 
-module.exports = function(less) {
+module.exports = function(environment) {
+var SourceMapOutput = require("../source-map-output")(environment);
 //
 // less.js - parser
 //
@@ -51,7 +52,7 @@ var Parser = function Parser(env) {
     }
     this.env = env;
 
-    var imports = this.imports = getImportManager(less, env, Parser);
+    var imports = this.imports = getImportManager(environment, env, Parser);
 
     function expect(arg, msg, index) {
         // some older browsers return typeof 'function' for RegExp
@@ -88,7 +89,7 @@ var Parser = function Parser(env) {
 
     function getDebugInfo(index) {
         var filename = env.currentFileInfo.filename;
-        filename = less.environment.getAbsolutePath(env, filename);
+        filename = environment.getAbsolutePath(env, filename);
 
         return {
             lineNumber: parserInput.getLocation(index).line + 1,
@@ -111,8 +112,8 @@ var Parser = function Parser(env) {
         parse: function (str, callback, additionalData) {
             var root, error = null, globalVars, modifyVars, preText = "";
 
-            globalVars = (additionalData && additionalData.globalVars) ? less.Parser.serializeVars(additionalData.globalVars) + '\n' : '';
-            modifyVars = (additionalData && additionalData.modifyVars) ? '\n' + less.Parser.serializeVars(additionalData.modifyVars) : '';
+            globalVars = (additionalData && additionalData.globalVars) ? Parser.serializeVars(additionalData.globalVars) + '\n' : '';
+            modifyVars = (additionalData && additionalData.modifyVars) ? '\n' + Parser.serializeVars(additionalData.modifyVars) : '';
 
             if (globalVars || (additionalData && additionalData.banner)) {
                 preText = ((additionalData && additionalData.banner) ? additionalData.banner : "") + globalVars;
@@ -206,7 +207,7 @@ var Parser = function Parser(env) {
                         }
 
                         if (options.sourceMap) {
-                            evaldRoot = new less.SourceMapOutput(
+                            evaldRoot = new SourceMapOutput(
                                 {
                                     contentsIgnoredCharsMap: parser.imports.contentsIgnoredChars,
                                     writeSourceMap: options.writeSourceMap,
@@ -231,7 +232,7 @@ var Parser = function Parser(env) {
                         throw new LessError(parser, e, env);
                     }
 
-                    var CleanCSS = less.environment.getCleanCSS();
+                    var CleanCSS = environment.getCleanCSS();
                     if (options.cleancss && CleanCSS) {
                         var cleancssOptions = options.cleancssOptions || {};
 
diff --git a/lib/less/source-map-output.js b/lib/less/source-map-output.js
index 1bb1fac..a70889b 100644
--- a/lib/less/source-map-output.js
+++ b/lib/less/source-map-output.js
@@ -1,4 +1,4 @@
-module.exports = function (less) {
+module.exports = function (environment) {
 
     var SourceMapOutput = function (options) {
         this._css = [];
@@ -14,7 +14,7 @@ module.exports = function (less) {
         }
         this._sourceMapRootpath = options.sourceMapRootpath;
         this._outputSourceFiles = options.outputSourceFiles;
-        this._sourceMapGeneratorConstructor = less.environment.getSourceMapGenerator();
+        this._sourceMapGeneratorConstructor = environment.getSourceMapGenerator();
 
         if (this._sourceMapRootpath && this._sourceMapRootpath.charAt(this._sourceMapRootpath.length-1) !== '/') {
             this._sourceMapRootpath += '/';
@@ -127,7 +127,7 @@ module.exports = function (less) {
             if (this._writeSourceMap) {
                 this._writeSourceMap(sourceMapContent);
             } else {
-                sourceMapURL = "data:application/json;base64," + less.environment.encodeBase64(sourceMapContent);
+                sourceMapURL = "data:application/json;base64," + environment.encodeBase64(sourceMapContent);
             }
 
             if (sourceMapURL) {
diff --git a/lib/less/tree/call.js b/lib/less/tree/call.js
index f7cbfed..6521949 100644
--- a/lib/less/tree/call.js
+++ b/lib/less/tree/call.js
@@ -1,4 +1,5 @@
-var Node = require("./node.js");
+var Node = require("./node.js"),
+    FunctionCaller = require("../functions/function-caller.js");
 //
 // A function call node.
 //
@@ -30,7 +31,6 @@ Call.prototype.accept = function (visitor) {
 //
 Call.prototype.eval = function (env) {
     var args = this.args.map(function (a) { return a.eval(env); }),
-        FunctionCaller = require("../non-node-index.js").functions.functionCaller, //TODO! Move out
         result, funcCaller = new FunctionCaller(this.name, env, this.currentFileInfo);
 
     if (funcCaller.isValid()) { // 1.

-- 
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