[Pkg-javascript-commits] [less.js] 132/285: make the import manager one instance per render

Jonas Smedegaard dr at jones.dk
Mon Oct 26 23:23:47 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 a53be80014a6329b4545d3c6d68e13c7184f9846
Author: Luke Page <luke.a.page at gmail.com>
Date:   Sun Sep 21 16:36:06 2014 +0100

    make the import manager one instance per render
---
 lib/less/imports.js | 140 +++++++++++++++++++++++++++-------------------------
 lib/less/index.js   |   6 +--
 lib/less/render.js  |   5 +-
 3 files changed, 78 insertions(+), 73 deletions(-)

diff --git a/lib/less/imports.js b/lib/less/imports.js
index 3cb9cfb..a5fcdd4 100644
--- a/lib/less/imports.js
+++ b/lib/less/imports.js
@@ -1,85 +1,91 @@
 var contexts = require("./contexts"),
     Parser = require('./parser/parser');
 
-var getImportManager = module.exports = function(environment, env) {
-    var rootFilename = env && env.filename;
-    return {
-        paths: env.paths || [],  // Search paths, when importing
-        queue: [],               // Files which haven't been imported yet
-        files: env.files,        // Holds the imported parse trees
-        contents: env.contents,  // Holds the imported file contents
-        contentsIgnoredChars: env.contentsIgnoredChars, // lines inserted, not in the original less
-        mime:  env.mime,         // MIME type of .less files
-        error: null,             // Error in parsing/evaluating an import
-        getAbsolutePath: function(env, filename) {
-            // proxy needed for "DebugInfo"
-            // I hope one day we can remove this function
-            return environment.getAbsolutePath(env, filename);
-        },
-        push: function (path, currentFileInfo, importOptions, callback) {
-            var parserImports = this;
-            this.queue.push(path);
+// TODO - why does environment need env passed everywhere?
+// Now we have one import manager per parse, can we move things from env to the import manager
+// and then move the import manager onto env (if required there - if not, keep seperate)
 
-            var fileParsedFunc = function (e, root, fullPath) {
-                parserImports.queue.splice(parserImports.queue.indexOf(path), 1); // Remove the path from the queue
+module.exports = function(environment) {
+    var ImportManager = function(env) {
+        this.rootFilename = env && env.filename;
+        this.paths = env.paths || [];  // Search paths, when importing
+        this.queue = []; // Files which haven't been imported yet
+        this.files = env.files;        // Holds the imported parse trees
+        this.contents = env.contents; // Holds the imported file contents
+        this.contentsIgnoredChars = env.contentsIgnoredChars; // lines inserted, not in the original less
+        this.mime = env.mime;
+        this.error = null;
+        this.env = env;
+    };
+    ImportManager.prototype.getAbsolutePath = function(env, filename) {
+        // proxy needed for "DebugInfo"
+        // I hope one day we can remove this function
+        return environment.getAbsolutePath(env, filename);
+    };
+    ImportManager.prototype.push = function (path, currentFileInfo, importOptions, callback) {
+        var parserImports = this;
+        this.queue.push(path);
 
-                var importedPreviously = fullPath === rootFilename;
+        var fileParsedFunc = function (e, root, fullPath) {
+            parserImports.queue.splice(parserImports.queue.indexOf(path), 1); // Remove the path from the queue
 
-                parserImports.files[fullPath] = root;                        // Store the root
+            var importedPreviously = fullPath === parserImports.rootFilename;
 
-                if (e && !parserImports.error) { parserImports.error = e; }
+            parserImports.files[fullPath] = root;                        // Store the root
 
-                callback(e, root, importedPreviously, fullPath);
-            };
+            if (e && !parserImports.error) { parserImports.error = e; }
 
-            var newFileInfo = {
-                relativeUrls: env.relativeUrls,
-                entryPath: currentFileInfo.entryPath,
-                rootpath: currentFileInfo.rootpath,
-                rootFilename: currentFileInfo.rootFilename
-            };
+            callback(e, root, importedPreviously, fullPath);
+        };
 
-            environment.loadFile(env, path, currentFileInfo.currentDirectory, function loadFileCallback(e, contents, resolvedFilename) {
-                if (e) {
-                    fileParsedFunc(e);
-                    return;
-                }
+        var newFileInfo = {
+            relativeUrls: this.env.relativeUrls,
+            entryPath: currentFileInfo.entryPath,
+            rootpath: currentFileInfo.rootpath,
+            rootFilename: currentFileInfo.rootFilename
+        };
 
-                // Pass on an updated rootpath if path of imported file is relative and file
-                // is in a (sub|sup) directory
-                //
-                // Examples:
-                // - If path of imported file is 'module/nav/nav.less' and rootpath is 'less/',
-                //   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 = environment.getPath(env, resolvedFilename);
-                if(newFileInfo.relativeUrls) {
-                    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);
-                    }
+        environment.loadFile(this.env, path, currentFileInfo.currentDirectory, function loadFileCallback(e, contents, resolvedFilename) {
+            if (e) {
+                fileParsedFunc(e);
+                return;
+            }
+
+            // Pass on an updated rootpath if path of imported file is relative and file
+            // is in a (sub|sup) directory
+            //
+            // Examples:
+            // - If path of imported file is 'module/nav/nav.less' and rootpath is 'less/',
+            //   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 = environment.getPath(parserImports.env, resolvedFilename);
+            if(newFileInfo.relativeUrls) {
+                newFileInfo.rootpath = environment.join((parserImports.env.rootpath || ""), environment.pathDiff(newFileInfo.currentDirectory, newFileInfo.entryPath));
+                if (!environment.isPathAbsolute(parserImports.env, newFileInfo.rootpath) && environment.alwaysMakePathsAbsolute()) {
+                    newFileInfo.rootpath = environment.join(newFileInfo.entryPath, newFileInfo.rootpath);
                 }
-                newFileInfo.filename = resolvedFilename;
+            }
+            newFileInfo.filename = resolvedFilename;
 
-                var newEnv = new contexts.parseEnv(env);
+            var newEnv = new contexts.parseEnv(parserImports.env);
 
-                newEnv.currentFileInfo = newFileInfo;
-                newEnv.processImports = false;
-                newEnv.contents[resolvedFilename] = contents;
+            newEnv.currentFileInfo = newFileInfo;
+            newEnv.processImports = false;
+            newEnv.contents[resolvedFilename] = contents;
 
-                if (currentFileInfo.reference || importOptions.reference) {
-                    newFileInfo.reference = true;
-                }
+            if (currentFileInfo.reference || importOptions.reference) {
+                newFileInfo.reference = true;
+            }
 
-                if (importOptions.inline) {
-                    fileParsedFunc(null, contents, resolvedFilename);
-                } else {
-                    new(Parser)(newEnv, getImportManager(environment, env)).parse(contents, function (e, root) {
-                        fileParsedFunc(e, root, resolvedFilename);
-                    });
-                }
-            });
-        }
+            if (importOptions.inline) {
+                fileParsedFunc(null, contents, resolvedFilename);
+            } else {
+                new(Parser)(newEnv, parserImports).parse(contents, function (e, root) {
+                    fileParsedFunc(e, root, resolvedFilename);
+                });
+            }
+        });
     };
+    return ImportManager;
 };
diff --git a/lib/less/index.js b/lib/less/index.js
index 5b45e65..5b1f5ce 100644
--- a/lib/less/index.js
+++ b/lib/less/index.js
@@ -1,5 +1,5 @@
 module.exports = function(environment) {
-    var SourceMapOutput, SourceMapBuilder, ParseTree;
+    var SourceMapOutput, SourceMapBuilder, ParseTree, ImportManager;
 
     var less = {
         version: [2, 0, 0],
@@ -13,8 +13,8 @@ module.exports = function(environment) {
         SourceMapOutput: (SourceMapOutput = require('./source-map-output.js')(environment)),
         SourceMapBuilder: (SourceMapBuilder = require('./source-map-builder.js')(SourceMapOutput)),
         ParseTree: (ParseTree = require('./parse-tree')(SourceMapBuilder)),
-        render: require("./render")(environment, ParseTree),
-        getImportManager: require('./imports'), // TODO: change to class? add static ways of replacing file-manager?
+        ImportManager: (ImportManager = require('./imports')(environment)),
+        render: require("./render")(environment, ParseTree, ImportManager),
         LessError: require('./less-error'),
         transformTree: require('./transform-tree'),
         utils: require('./utils'),
diff --git a/lib/less/render.js b/lib/less/render.js
index 182e91b..5be9f5b 100644
--- a/lib/less/render.js
+++ b/lib/less/render.js
@@ -1,9 +1,8 @@
 var PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise,
     contexts = require("./contexts"),
-    getImportManager = require("./imports"),
     Parser = require('./parser/parser');
 
-module.exports = function(environment, ParseTree) {
+module.exports = function(environment, ParseTree, ImportManager) {
     var render = function (input, options, callback) {
         options = options || {};
 
@@ -22,7 +21,7 @@ module.exports = function(environment, ParseTree) {
                 });
         } else {
             var env = new contexts.parseEnv(options),
-                imports = getImportManager(environment, env);
+                imports = new ImportManager(env);
 
             var parser = new(Parser)(env, imports);
 

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