[Pkg-javascript-commits] [less.js] 118/285: allow lessc to load plugins. rename visitor directory to visitors to match others. Fix indexes in URL

Jonas Smedegaard dr at jones.dk
Mon Oct 26 23:23:45 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 e357dae7bc24faf311e07340069a8e31ec3a6b41
Author: Luke Page <luke.a.page at gmail.com>
Date:   Tue Sep 9 21:20:04 2014 +0100

    allow lessc to load plugins. rename visitor directory to visitors to match others. Fix indexes in URL
---
 bin/lessc                                          | 14 ++++++++---
 lib/less-node/index.js                             |  8 +++---
 lib/less-node/node-plugin-manager.js               | 29 ++++++++++++++++++++++
 lib/less/functions/data-uri.js                     |  6 ++---
 lib/less/functions/function-caller.js              |  3 ++-
 lib/less/functions/svg.js                          |  2 +-
 lib/less/index.js                                  | 11 ++++++--
 lib/less/parser/parser.js                          | 10 ++++----
 lib/less/plugin-manager.js                         | 17 +++++++++++++
 lib/less/transform-tree.js                         | 16 ++++++------
 lib/less/tree/call.js                              |  2 +-
 lib/less/tree/url.js                               |  5 ++--
 lib/less/visitor/index.js                          |  9 -------
 lib/less/{visitor => visitors}/extend-visitor.js   |  4 +--
 lib/less/{visitor => visitors}/import-visitor.js   |  4 +--
 lib/less/visitors/index.js                         |  9 +++++++
 .../{visitor => visitors}/join-selector-visitor.js |  2 +-
 lib/less/{visitor => visitors}/to-css-visitor.js   |  4 +--
 lib/less/{visitor => visitors}/visitor.js          |  2 +-
 19 files changed, 111 insertions(+), 46 deletions(-)

diff --git a/bin/lessc b/bin/lessc
index d8e1953..50fc18f 100755
--- a/bin/lessc
+++ b/bin/lessc
@@ -5,7 +5,9 @@ var path = require('path'),
     os = require('os'),
     mkdirp;
 
-var less = require('../lib/less-node');
+var less = require('../lib/less-node'),
+    pluginManager = new less.PluginManager(less);
+
 var args = process.argv.slice(1);
 var options = {
     depends: false,
@@ -251,9 +253,12 @@ args = args.filter(function (arg) {
             }
             break;
         default:
-            require('../lib/less/lessc_helper').printUsage();
-            continueProcessing = false;
-            currentErrorcode = 1;
+            if (!pluginManager.interpretCommandLineArgument(arg, match[2])) {
+                // TODO more of an explanation
+                require('../lib/less/lessc_helper').printUsage();
+                continueProcessing = false;
+                currentErrorcode = 1;
+            }
             break;
     }
 });
@@ -368,6 +373,7 @@ var parseLessFile = function (e, data) {
     options.sourceMapFilename = options.sourceMap;
     options.sourceMap = Boolean(options.sourceMap);
     options.sourceMapRootpath = options.sourceMapRootpath || "";
+    options.plugins = pluginManager;
 
     less.render(data, options)
         .then(function(css) {
diff --git a/lib/less-node/index.js b/lib/less-node/index.js
index c4124f9..57174b4 100644
--- a/lib/less-node/index.js
+++ b/lib/less-node/index.js
@@ -1,17 +1,19 @@
 var environment = require("./environment"),
     createFromEnvironment = require("../less"),
-    less = createFromEnvironment(environment);
+    less = createFromEnvironment(environment),
+    lesscHelper = require('./lessc-helper');
 
 // allow people to create less with their own environment
 less.createFromEnvironment = createFromEnvironment;
-
+less.lesscHelper = lesscHelper;
+less.PluginManager = require("./node-plugin-manager");
 less.formatError = function(ctx, options) {
     options = options || {};
 
     var message = "";
     var extract = ctx.extract;
     var error = [];
-    var stylize = options.color ? require('./lessc-helper').stylize : function (str) { return str; };
+    var stylize = options.color ? lesscHelper.stylize : function (str) { return str; };
 
     // only output a stack if it isn't a less error
     if (ctx.stack && !ctx.type) { return stylize(ctx.stack, 'red'); }
diff --git a/lib/less-node/node-plugin-manager.js b/lib/less-node/node-plugin-manager.js
new file mode 100644
index 0000000..547d0c7
--- /dev/null
+++ b/lib/less-node/node-plugin-manager.js
@@ -0,0 +1,29 @@
+var PluginManager = require("../less/plugin-manager");
+/**
+ * Node Plugin Manager
+ */
+var NodePluginManager = function(less) {
+    PluginManager.call(this, less);
+};
+NodePluginManager.prototype = new PluginManager();
+NodePluginManager.prototype.interpretCommandLineArgument = function(name, argument) {
+    var plugin = this.tryRequirePlugin(name);
+    if (plugin) {
+        this.addPlugin(plugin);
+        return true;
+    }
+    return false;
+};
+NodePluginManager.prototype.tryRequirePlugin = function(name) {
+    try {
+        return require("less-plugin-"+name);
+    }
+    catch(e) {
+    }
+    try {
+        return require("../../../less-plugin-"+name);
+    }
+    catch(e) {
+    }
+};
+module.exports = NodePluginManager;
diff --git a/lib/less/functions/data-uri.js b/lib/less/functions/data-uri.js
index 03f086c..3112d80 100644
--- a/lib/less/functions/data-uri.js
+++ b/lib/less/functions/data-uri.js
@@ -6,7 +6,7 @@ module.exports = function(environment) {
     functionRegistry.add("data-uri", function(mimetypeNode, filePathNode) {
 
         if (!environment.supportsDataURI(this.env)) {
-            return new URL(filePathNode || mimetypeNode, this.currentFileInfo).eval(this.env);
+            return new URL(filePathNode || mimetypeNode, this.index, this.currentFileInfo).eval(this.env);
         }
 
         var mimetype = mimetypeNode.value;
@@ -60,7 +60,7 @@ module.exports = function(environment) {
                     console.warn("Skipped data-uri embedding of %s because its size (%dKB) exceeds IE8-safe %dKB!", filePath, fileSizeInKB, DATA_URI_MAX_KB);
                 }
 
-                return new URL(filePathNode || mimetypeNode, this.currentFileInfo).eval(this.env);
+                return new URL(filePathNode || mimetypeNode, this.index, this.currentFileInfo).eval(this.env);
             }
         }
 
@@ -68,6 +68,6 @@ module.exports = function(environment) {
             : encodeURIComponent(buf);
 
         var uri = "\"data:" + mimetype + ',' + buf + fragment + "\"";
-        return new(URL)(new(Anonymous)(uri));
+        return new URL(new Anonymous(uri), this.index, this.currentFileInfo);
     });
 };
diff --git a/lib/less/functions/function-caller.js b/lib/less/functions/function-caller.js
index 056b817..1e4f796 100644
--- a/lib/less/functions/function-caller.js
+++ b/lib/less/functions/function-caller.js
@@ -1,8 +1,9 @@
 var functionRegistry = require("./function-registry");
 
-var functionCaller = function(name, env, currentFileInfo) {
+var functionCaller = function(name, env, index, currentFileInfo) {
     this.name = name.toLowerCase();
     this.function = functionRegistry.get(this.name);
+    this.index = index;
     this.env = env;
     this.currentFileInfo = currentFileInfo;
 };
diff --git a/lib/less/functions/svg.js b/lib/less/functions/svg.js
index 27294e0..5bd8fc6 100644
--- a/lib/less/functions/svg.js
+++ b/lib/less/functions/svg.js
@@ -78,6 +78,6 @@ module.exports = function(environment) {
         }
 
         returner = "'data:image/svg+xml" + (useBase64 ? ";base64" : "") + "," + returner + "'";
-        return new(URL)(new(Anonymous)(returner));
+        return new URL(new Anonymous(returner), this.index, this.currentFileInfo);
     });
 };
diff --git a/lib/less/index.js b/lib/less/index.js
index bd2ea81..da4731f 100644
--- a/lib/less/index.js
+++ b/lib/less/index.js
@@ -3,12 +3,19 @@ module.exports = function(environment) {
         version: [2, 0, 0],
         data: require('./data'),
         tree: require('./tree'),
-        visitor: require('./visitor'),
+        visitors: require('./visitors'),
         Parser: require('./parser/parser'),
         functions: require('./functions')(environment),
         contexts: require("./contexts"),
         environment: environment,
-        render: require("./render")(environment)
+        render: require("./render")(environment),
+        // ParseTree: require('./parse-tree'), // TODO - move environment to constructor? make available to consumers
+        //SourceMapOutput: require('./source-map-output.js'), // TODO - move environment to constructor? make available to consumers
+        getImportManager: require('./imports'), // TODO: change to class? add static ways of replacing file-manager?
+        LessError: require('./less-error'),
+        transformTree: require('./transform-tree'),
+        utils: require('./utils'),
+        PluginManager: require('./plugin-manager')
     };
 
     return less;
diff --git a/lib/less/parser/parser.js b/lib/less/parser/parser.js
index 3246e5f..d623223 100644
--- a/lib/less/parser/parser.js
+++ b/lib/less/parser/parser.js
@@ -1,6 +1,6 @@
 var LessError = require('../less-error'),
     tree = require("../tree"),
-    visitor = require("../visitor"),
+    visitors = require("../visitors"),
     getParserInput = require("./parser-input"),
     utils = require("../utils");
 
@@ -170,7 +170,7 @@ var Parser = function Parser(env, imports) {
             };
 
             if (env.processImports !== false) {
-                new visitor.ImportVisitor(imports, finish)
+                new visitors.ImportVisitor(imports, finish)
                     .run(root);
             } else {
                 return finish();
@@ -381,7 +381,7 @@ var Parser = function Parser(env, imports) {
                 // to be enclosed within a string, so it can't be parsed as an Expression.
                 //
                 url: function () {
-                    var value;
+                    var value, index = parserInput.i;
 
                     if (parserInput.currentChar() !== 'u' || !parserInput.$re(/^url\(/)) {
                         return;
@@ -396,8 +396,8 @@ var Parser = function Parser(env, imports) {
 
                     expectChar(')');
 
-                    return new(tree.URL)((value.value != null || value instanceof tree.Variable)
-                                        ? value : new(tree.Anonymous)(value), env.currentFileInfo);
+                    return new(tree.URL)((value.value != null || value instanceof tree.Variable) ?
+                                        value : new(tree.Anonymous)(value), index, env.currentFileInfo);
                 },
 
                 //
diff --git a/lib/less/plugin-manager.js b/lib/less/plugin-manager.js
new file mode 100644
index 0000000..c4be752
--- /dev/null
+++ b/lib/less/plugin-manager.js
@@ -0,0 +1,17 @@
+/**
+ * Plugin Manager
+ */
+var PluginManager = function(less) {
+    this.less = less;
+    this.visitors = [];
+};
+PluginManager.prototype.addPlugin = function(plugin) {
+    plugin.install(this.less, this);
+};
+PluginManager.prototype.addVisitor = function(visitor) {
+    this.visitors.push(visitor);
+};
+PluginManager.prototype.getVisitors = function() {
+    return this.visitors;
+};
+module.exports = PluginManager;
diff --git a/lib/less/transform-tree.js b/lib/less/transform-tree.js
index cd74539..7c88bb0 100644
--- a/lib/less/transform-tree.js
+++ b/lib/less/transform-tree.js
@@ -1,5 +1,5 @@
 var contexts = require("./contexts"),
-    visitor = require("./visitor"),
+    visitor = require("./visitors"),
     tree = require("./tree");
 
 module.exports = function(root, options) {
@@ -44,14 +44,16 @@ module.exports = function(root, options) {
         ], i;
 
     if (options.plugins) {
-        for(i =0; i < options.plugins.length; i++) {
-            if (options.plugins[i].isPreEvalVisitor) {
-                preEvalVisitors.push(options.plugins[i]);
+        var pluginVisitors = options.plugins.getVisitors();
+        for(i =0; i < pluginVisitors.length; i++) {
+            var pluginVisitor = pluginVisitors[i];
+            if (pluginVisitor.isPreEvalVisitor) {
+                preEvalVisitors.push(pluginVisitor);
             } else {
-                if (options.plugins[i].isPreVisitor) {
-                    visitors.splice(0, 0, options.plugins[i]);
+                if (pluginVisitor.isPreVisitor) {
+                    visitors.splice(0, 0, pluginVisitor);
                 } else {
-                    visitors.push(options.plugins[i]);
+                    visitors.push(pluginVisitor);
                 }
             }
         }
diff --git a/lib/less/tree/call.js b/lib/less/tree/call.js
index 38bcbc0..ef7199b 100644
--- a/lib/less/tree/call.js
+++ b/lib/less/tree/call.js
@@ -31,7 +31,7 @@ Call.prototype.accept = function (visitor) {
 //
 Call.prototype.eval = function (env) {
     var args = this.args.map(function (a) { return a.eval(env); }),
-        result, funcCaller = new FunctionCaller(this.name, env, this.currentFileInfo);
+        result, funcCaller = new FunctionCaller(this.name, env, this.index, this.currentFileInfo);
 
     if (funcCaller.isValid()) { // 1.
         try {
diff --git a/lib/less/tree/url.js b/lib/less/tree/url.js
index 6dd2fa6..b528fff 100644
--- a/lib/less/tree/url.js
+++ b/lib/less/tree/url.js
@@ -1,8 +1,9 @@
 var Node = require("./node");
 
-var URL = function (val, currentFileInfo, isEvald) {
+var URL = function (val, index, currentFileInfo, isEvald) {
     this.value = val;
     this.currentFileInfo = currentFileInfo;
+    this.index = index;
     this.isEvald = isEvald;
 };
 URL.prototype = new Node();
@@ -45,6 +46,6 @@ URL.prototype.eval = function (ctx) {
         }
     }
 
-    return new(URL)(val, this.currentFileInfo, true);
+    return new URL(val, this.index, this.currentFileInfo, true);
 };
 module.exports = URL;
diff --git a/lib/less/visitor/index.js b/lib/less/visitor/index.js
deleted file mode 100644
index 8a09617..0000000
--- a/lib/less/visitor/index.js
+++ /dev/null
@@ -1,9 +0,0 @@
-var visitors = {
-    Visitor: require("./visitor"),
-    ImportVisitor: require('./import-visitor.js'),
-    ExtendVisitor: require('./extend-visitor.js'),
-    JoinSelectorVisitor: require('./join-selector-visitor.js'),
-    ToCSSVisitor: require('./to-css-visitor.js')
-};
-
-module.exports = visitors;
diff --git a/lib/less/visitor/extend-visitor.js b/lib/less/visitors/extend-visitor.js
similarity index 99%
rename from lib/less/visitor/extend-visitor.js
rename to lib/less/visitors/extend-visitor.js
index 9616cd6..6d17db2 100644
--- a/lib/less/visitor/extend-visitor.js
+++ b/lib/less/visitors/extend-visitor.js
@@ -1,5 +1,5 @@
-var tree = require("../tree/index.js"),
-    Visitor = require("./visitor.js");
+var tree = require("../tree"),
+    Visitor = require("./visitor");
 
 /*jshint loopfunc:true */
 
diff --git a/lib/less/visitor/import-visitor.js b/lib/less/visitors/import-visitor.js
similarity index 98%
rename from lib/less/visitor/import-visitor.js
rename to lib/less/visitors/import-visitor.js
index f7068df..015f3bf 100644
--- a/lib/less/visitor/import-visitor.js
+++ b/lib/less/visitors/import-visitor.js
@@ -1,5 +1,5 @@
-var contexts = require("../contexts.js"),
-    Visitor = require("./visitor.js");
+var contexts = require("../contexts"),
+    Visitor = require("./visitor");
 
 var ImportVisitor = function(importer, finish, evalEnv, onceFileDetectionMap, recursionDetector) {
     this._visitor = new Visitor(this);
diff --git a/lib/less/visitors/index.js b/lib/less/visitors/index.js
new file mode 100644
index 0000000..fc8e683
--- /dev/null
+++ b/lib/less/visitors/index.js
@@ -0,0 +1,9 @@
+var visitors = {
+    Visitor: require("./visitor"),
+    ImportVisitor: require('./import-visitor'),
+    ExtendVisitor: require('./extend-visitor'),
+    JoinSelectorVisitor: require('./join-selector-visitor'),
+    ToCSSVisitor: require('./to-css-visitor')
+};
+
+module.exports = visitors;
diff --git a/lib/less/visitor/join-selector-visitor.js b/lib/less/visitors/join-selector-visitor.js
similarity index 97%
rename from lib/less/visitor/join-selector-visitor.js
rename to lib/less/visitors/join-selector-visitor.js
index 05f8732..b144696 100644
--- a/lib/less/visitor/join-selector-visitor.js
+++ b/lib/less/visitors/join-selector-visitor.js
@@ -1,4 +1,4 @@
-var Visitor = require("./visitor.js");
+var Visitor = require("./visitor");
 
 var JoinSelectorVisitor = function() {
     this.contexts = [[]];
diff --git a/lib/less/visitor/to-css-visitor.js b/lib/less/visitors/to-css-visitor.js
similarity index 99%
rename from lib/less/visitor/to-css-visitor.js
rename to lib/less/visitors/to-css-visitor.js
index 1a8f5e2..e9e19d1 100644
--- a/lib/less/visitor/to-css-visitor.js
+++ b/lib/less/visitors/to-css-visitor.js
@@ -1,5 +1,5 @@
-var tree = require("../tree/index.js"),
-    Visitor = require("./visitor.js");
+var tree = require("../tree"),
+    Visitor = require("./visitor");
 
 var ToCSSVisitor = function(env) {
     this._visitor = new Visitor(this);
diff --git a/lib/less/visitor/visitor.js b/lib/less/visitors/visitor.js
similarity index 98%
rename from lib/less/visitor/visitor.js
rename to lib/less/visitors/visitor.js
index cd0b328..5c0b5a5 100644
--- a/lib/less/visitor/visitor.js
+++ b/lib/less/visitors/visitor.js
@@ -1,4 +1,4 @@
-var tree = require("../tree/index.js");
+var tree = require("../tree");
 
 var _visitArgs = { visitDeeper: true },
     _hasIndexed = false;

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