[Pkg-javascript-commits] [less.js] 82/285: Change code to use less.render

Jonas Smedegaard dr at jones.dk
Mon Oct 26 23:23:41 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 544bd3abf084f7ab5586d7b578d3e37494cebc31
Author: Luke Page <luke.a.page at gmail.com>
Date:   Wed Sep 3 16:24:09 2014 +0100

    Change code to use less.render
---
 bin/lessc           | 85 ++++++++++++++++++++++++-----------------------------
 lib/less/browser.js | 55 +++++++++++++++++-----------------
 test/less-test.js   | 34 +++++++--------------
 3 files changed, 76 insertions(+), 98 deletions(-)

diff --git a/bin/lessc b/bin/lessc
index 59defad..f9e0b70 100755
--- a/bin/lessc
+++ b/bin/lessc
@@ -342,60 +342,51 @@ var parseLessFile = function (e, data) {
     options.paths = [path.dirname(input)].concat(options.paths);
     options.filename = input;
 
-    var parser = new(less.Parser)(options);
-    parser.parse(data, function (err, tree) {
-        if (err) {
-            less.writeError(err, options);
-            currentErrorcode = 1;
-            return;
-        } else if (options.depends) {
+    if (options.depends) {
+        var parser = new(less.Parser)(options);
+        parser.parse(data, function (err, tree) {
+            if (err) {
+                less.writeError(err, options);
+                currentErrorcode = 1;
+                return;
+            }
             for(var file in parser.imports.files) {
                 if (parser.imports.files.hasOwnProperty(file)) {
                     process.stdout.write(file + " ");
                 }
             }
             console.log("");
-        } else {
-            try {
-            if (options.lint) { writeSourceMap = function() {}; }
-                var css = tree.toCSS({
-                    silent: options.silent,
-                    verbose: options.verbose,
-                    ieCompat: options.ieCompat,
-                    compress: options.compress,
-                    cleancss: options.cleancss,
-                    cleancssOptions: cleancssOptions,
-                    sourceMap: Boolean(options.sourceMap),
-                    sourceMapFilename: options.sourceMap,
-                    sourceMapURL: options.sourceMapURL,
-                    sourceMapOutputFilename: options.sourceMapOutputFilename,
-                    sourceMapBasepath: options.sourceMapBasepath,
-                    sourceMapRootpath: options.sourceMapRootpath || "",
-                    outputSourceFiles: options.outputSourceFiles,
-                    writeSourceMap: writeSourceMap,
-                    maxLineLen: options.maxLineLen,
-                    strictMath: options.strictMath,
-                    strictUnits: options.strictUnits,
-                    urlArgs: options.urlArgs
-                });
-		if(!options.lint) {
-                    if (output) {
-                        ensureDirectory(output);
-                        fs.writeFileSync(output, css, 'utf8');
-                        if (options.verbose) {
-                            console.log('lessc: wrote ' + output);
-                        }
-                    } else {
-                        process.stdout.write(css);
-            }
-		}
-            } catch (e) {
-                less.writeError(e, options);
-                currentErrorcode = 2;
-                return;
+        });
+        return;
+    }
+
+    if (options.lint) {
+        options.writeSourceMap = function() {};
+    } else {
+        options.writeSourceMap = writeSourceMap;
+    }
+    options.sourceMapFilename = options.sourceMap;
+    options.sourceMap = Boolean(options.sourceMap);
+    options.sourceMapRootpath = options.sourceMapRootpath || "";
+
+    less.render(data, options)
+        .then(function(css) {
+            if(!options.lint) {
+                if (output) {
+                    ensureDirectory(output);
+                    fs.writeFileSync(output, css, 'utf8');
+                    if (options.verbose) {
+                        console.log('lessc: wrote ' + output);
+                    }
+                } else {
+                    process.stdout.write(css);
+                }
             }
-        }
-    });
+        },
+        function(err) {
+            less.writeError(err, options);
+            currentErrorcode = 1;
+        });
 };
 
 if (input != '-') {
diff --git a/lib/less/browser.js b/lib/less/browser.js
index 0b4c688..5b9e9e0 100644
--- a/lib/less/browser.js
+++ b/lib/less/browser.js
@@ -350,8 +350,7 @@ function loadStyles(modifyVars) {
             /*jshint loopfunc:true */
             // use closure to store current style
             less.render(lessText, instanceOptions)
-                .then(
-                bind(function(style, css) {
+                .then(bind(function(style, css) {
                     style.type = 'text/css';
                     if (style.styleSheet) {
                         style.styleSheet.cssText = css;
@@ -368,23 +367,26 @@ function loadStyles(modifyVars) {
 
 function loadStyleSheet(sheet, callback, reload, remaining, modifyVars) {
 
-    var env = new less.contexts.parseEnv(options);
-    env.mime = sheet.type;
+    var instanceOptions = clone(options);
+    instanceOptions.mime = sheet.type;
 
+    if (modifyVars) {
+        instanceOptions.modifyVars = modifyVars;
+    }
     if (modifyVars || options.globalVars) {
-        env.useFileCache = true;
+        instanceOptions.useFileCache = true;
     }
 
-    less.environment.loadFile(env, sheet.href, null, function loadInitialFileCallback(e, data, path, webInfo) {
+    less.environment.loadFile(instanceOptions, sheet.href, null, function loadInitialFileCallback(e, data, path, webInfo) {
 
         var newFileInfo = {
-            currentDirectory: less.environment.getPath(env, path),
+            currentDirectory: less.environment.getPath(instanceOptions, path),
             filename: path,
             rootFilename: path,
-            relativeUrls: env.relativeUrls};
+            relativeUrls: instanceOptions.relativeUrls};
 
         newFileInfo.entryPath = newFileInfo.currentDirectory;
-        newFileInfo.rootpath = env.rootpath || newFileInfo.currentDirectory;
+        newFileInfo.rootpath = instanceOptions.rootpath || newFileInfo.currentDirectory;
 
         if (webInfo) {
             webInfo.remaining = remaining;
@@ -407,19 +409,18 @@ function loadStyleSheet(sheet, callback, reload, remaining, modifyVars) {
         removeError(path);
 
         if (data) {
-            env.currentFileInfo = newFileInfo;
-            new(less.Parser)(env).parse(data, function (e, root) {
-                if (e) { return callback(e, null, null, sheet); }
-                try {
-                    callback(e, root, data, sheet, webInfo, path);
-                } catch (e) {
+            instanceOptions.currentFileInfo = newFileInfo;
+            less.render(data, instanceOptions)
+                .then(function(css) {
+                    callback(null, css, data, sheet, webInfo, path);
+                },
+                function(e) {
                     callback(e, null, null, sheet);
-                }
-            }, {modifyVars: modifyVars, globalVars: options.globalVars});
+                });
         } else {
             callback(e, null, null, sheet, webInfo, path);
         }
-    }, env, modifyVars);
+    });
 }
 
 function loadStyleSheets(callback, reload, modifyVars) {
@@ -432,13 +433,12 @@ function initRunningMode(){
     if (less.env === 'development') {
         less.watchTimer = setInterval(function () {
             if (less.watchMode) {
-                loadStyleSheets(function (e, root, _, sheet, env) {
+                loadStyleSheets(function (e, css, _, sheet, env) {
                     if (e) {
                         error(e, sheet.href);
-                    } else if (root) {
-                        var styles = root.toCSS(options);
-                        styles = postProcessCSS(styles);
-                        createCSS(styles, sheet, env.lastModified);
+                    } else if (css) {
+                        css = postProcessCSS(css);
+                        createCSS(css, sheet, env.lastModified);
                     }
                 });
             }
@@ -496,17 +496,16 @@ less.refresh = function (reload, modifyVars) {
     var startTime, endTime;
     startTime = endTime = new Date();
 
-    loadStyleSheets(function (e, root, _, sheet, webInfo) {
+    loadStyleSheets(function (e, css, _, sheet, webInfo) {
         if (e) {
             return error(e, sheet.href);
         }
         if (webInfo.local) {
             log("loading " + sheet.href + " from cache.", logLevel.info);
         } else {
-            log("parsed " + sheet.href + " successfully.", logLevel.debug);
-            var styles = root.toCSS(options);
-            styles = postProcessCSS(styles);
-            createCSS(styles, sheet, webInfo.lastModified);
+            log("rendered " + sheet.href + " successfully.", logLevel.debug);
+            css = postProcessCSS(css);
+            createCSS(css, sheet, webInfo.lastModified);
         }
         log("css for " + sheet.href + " generated in " + (new Date() - endTime) + 'ms', logLevel.info);
         if (webInfo.remaining === 0) {
diff --git a/test/less-test.js b/test/less-test.js
index 22e45a0..de13db7 100644
--- a/test/less-test.js
+++ b/test/less-test.js
@@ -203,7 +203,6 @@ module.exports = function() {
     }
 
     function toCSS(options, path, callback) {
-        var css;
         options = options || {};
         fs.readFile(path, 'utf8', function (e, str) {
             if (e) { return callback(e); }
@@ -212,31 +211,20 @@ module.exports = function() {
             options.filename = require('path').resolve(process.cwd(), path);
             options.optimization = options.optimization || 0;
 
-            var parser = new(less.Parser)(options);
-            var additionalData = {};
             if (options.globalVars) {
-                additionalData.globalVars = options.getVars(path);
+                options.globalVars = options.getVars(path);
             } else if (options.modifyVars) {
-                additionalData.modifyVars = options.getVars(path);
+                options.modifyVars = options.getVars(path);
             }
-            if (options.banner) {
-                additionalData.banner = options.banner;
-            }
-            parser.parse(str, function (err, tree) {
-                if (err) {
-                    callback(err);
-                } else {
-                    try {
-                        css = tree.toCSS(options);
-                        var css2 = tree.toCSS(options); // integration test that 2nd call gets same output
-                        if (css2 !== css) { throw new Error("css not equal to 2nd call"); }
-                        callback(null, css);
-                    } catch (e) {
-                        callback(e);
-                    }
-                }
-                }, additionalData);
-            });
+
+            less.render(str, options)
+                .then(function(css) {
+                    // TODO integration test that calling toCSS twice results in the same css?
+                    callback(null, css);
+                }, function(e) {
+                    callback(e);
+                });
+        });
     }
 
     function testNoOptions() {

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