[Pkg-javascript-commits] [less.js] 126/285: use a sourcemap builder for sourcemap output, seperated from the other less options

Jonas Smedegaard dr at jones.dk
Mon Oct 26 23:23:46 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 b721c4f44fa7aef7af91984156c652ac9e705e54
Author: Luke Page <luke.a.page at gmail.com>
Date:   Thu Sep 18 22:23:15 2014 +0100

    use a sourcemap builder for sourcemap output, seperated from the other less options
---
 bin/lessc                      | 14 +++++++++-----
 lib/less/index.js              |  8 ++++++--
 lib/less/parse-tree.js         | 36 ++++++++++--------------------------
 lib/less/render.js             |  4 ++--
 lib/less/source-map-builder.js | 27 +++++++++++++++++++++++++++
 test/less-test.js              |  2 ++
 6 files changed, 56 insertions(+), 35 deletions(-)

diff --git a/bin/lessc b/bin/lessc
index f0a27dd..f1931ea 100755
--- a/bin/lessc
+++ b/bin/lessc
@@ -299,7 +299,7 @@ if (options.depends) {
 
 if (!sourceMapFileInline) {
     var writeSourceMap = function(output) {
-        var filename = options.sourceMapFullFilename || options.sourceMap;
+        var filename = options.sourceMapFullFilename || options.sourceMapFilename;
         ensureDirectory(filename);
         fs.writeFileSync(filename, output, 'utf8');
     };
@@ -336,13 +336,17 @@ var parseLessFile = function (e, data) {
     }
 
     if (options.lint) {
-        options.writeSourceMap = function() {};
-    } else {
-        options.writeSourceMap = writeSourceMap;
+        options.sourceMap = false;
     }
+    options.writeSourceMap = writeSourceMap;
     options.sourceMapFilename = options.sourceMap;
-    options.sourceMap = Boolean(options.sourceMap);
     options.sourceMapRootpath = options.sourceMapRootpath || "";
+
+    if (options.sourceMap) {
+        // todo seperate options - no need to send them to less any more?
+        options.sourceMap = new less.SourceMapBuilder(options);
+    }
+
     options.plugins = pluginManager;
 
     less.render(data, options)
diff --git a/lib/less/index.js b/lib/less/index.js
index da4731f..bd259b2 100644
--- a/lib/less/index.js
+++ b/lib/less/index.js
@@ -1,4 +1,7 @@
 module.exports = function(environment) {
+    var SourceMapOutput = require('./source-map-output.js')(environment),
+        SourceMapBuilder = require('./source-map-builder.js')(SourceMapOutput);
+
     var less = {
         version: [2, 0, 0],
         data: require('./data'),
@@ -9,8 +12,9 @@ module.exports = function(environment) {
         contexts: require("./contexts"),
         environment: 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
+        ParseTree: require('./parse-tree'),
+        SourceMapOutput: SourceMapOutput,
+        SourceMapBuilder: SourceMapBuilder,
         getImportManager: require('./imports'), // TODO: change to class? add static ways of replacing file-manager?
         LessError: require('./less-error'),
         transformTree: require('./transform-tree'),
diff --git a/lib/less/parse-tree.js b/lib/less/parse-tree.js
index 7788ce0..e1aeeae 100644
--- a/lib/less/parse-tree.js
+++ b/lib/less/parse-tree.js
@@ -1,10 +1,6 @@
 var LessError = require('./less-error'),
     transformTree = require("./transform-tree");
 
-module.exports = function(environment)
-{
-var SourceMapOutput = require("./source-map-output")(environment);
-
 var ParseTree = function(root, imports) {
     this.root = root;
     this.imports = imports;
@@ -19,28 +15,17 @@ ParseTree.prototype.toCSS = function(options) {
     }
     var css;
     try {
-        if (options.sourceMap) {
-            evaldRoot = new SourceMapOutput(
-                {
-                    contentsIgnoredCharsMap: this.imports.contentsIgnoredChars,
-                    writeSourceMap: options.writeSourceMap,
-                    rootNode: evaldRoot,
-                    contentsMap: this.imports.contents,
-                    sourceMapFilename: options.sourceMapFilename,
-                    sourceMapURL: options.sourceMapURL,
-                    outputFilename: options.sourceMapOutputFilename,
-                    sourceMapBasepath: options.sourceMapBasepath,
-                    sourceMapRootpath: options.sourceMapRootpath,
-                    outputSourceFiles: options.outputSourceFiles,
-                    sourceMapGenerator: options.sourceMapGenerator
-                });
-        }
-
-        css = evaldRoot.toCSS({
+        var toCSSOptions = {
             compress: Boolean(options.compress),
             dumpLineNumbers: options.dumpLineNumbers,
             strictUnits: Boolean(options.strictUnits),
-            numPrecision: 8});
+            numPrecision: 8};
+
+        if (options.sourceMap) {
+            css = options.sourceMap.toCSS(evaldRoot, toCSSOptions, this.imports);
+        } else {
+            css = evaldRoot.toCSS(toCSSOptions);
+        }
     } catch (e) {
         throw new LessError(e, this.imports);
     }
@@ -50,10 +35,9 @@ ParseTree.prototype.toCSS = function(options) {
         for(var i = 0; i < postProcessors.length; i++) {
             // TODO - pass source maps
             // TODO - async
-            css = postProcessors[i].process(css);
+            css = postProcessors[i].process(css, options);
         }
     }
     return css;
 };
-return ParseTree;
-};
+module.exports = ParseTree;
diff --git a/lib/less/render.js b/lib/less/render.js
index dde6eb8..3048d92 100644
--- a/lib/less/render.js
+++ b/lib/less/render.js
@@ -1,10 +1,10 @@
 var PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise,
     contexts = require("./contexts"),
     getImportManager = require("./imports"),
-    Parser = require('./parser/parser');
+    Parser = require('./parser/parser'),
+    ParseTree = require("./parse-tree");
 
 module.exports = function(environment) {
-    var ParseTree = require("./parse-tree")(environment);
     var render = function (input, options, callback) {
         options = options || {};
 
diff --git a/lib/less/source-map-builder.js b/lib/less/source-map-builder.js
new file mode 100644
index 0000000..5cd6f91
--- /dev/null
+++ b/lib/less/source-map-builder.js
@@ -0,0 +1,27 @@
+module.exports = function (SourceMapOutput) {
+
+    var SourceMapBuilder = function (options) {
+        this.options = options;
+    };
+
+    SourceMapBuilder.prototype.toCSS = function(rootNode, options, imports) {
+        this.sourceMapOutput = new SourceMapOutput(
+            {
+                contentsIgnoredCharsMap: imports.contentsIgnoredChars,
+                writeSourceMap: this.options.writeSourceMap,
+                rootNode: rootNode,
+                contentsMap: imports.contents,
+                sourceMapFilename: this.options.sourceMapFilename,
+                sourceMapURL: this.options.sourceMapURL,
+                outputFilename: this.options.sourceMapOutputFilename,
+                sourceMapBasepath: this.options.sourceMapBasepath,
+                sourceMapRootpath: this.options.sourceMapRootpath,
+                outputSourceFiles: this.options.outputSourceFiles,
+                sourceMapGenerator: this.options.sourceMapGenerator
+            });
+
+        return this.sourceMapOutput.toCSS(options);
+    };
+
+    return SourceMapBuilder;
+};
diff --git a/test/less-test.js b/test/less-test.js
index 2da185c..fbb1d99 100644
--- a/test/less-test.js
+++ b/test/less-test.js
@@ -117,6 +117,8 @@ module.exports = function() {
                 options.sourceMapOutputFilename = name + ".css";
                 options.sourceMapBasepath = path.join(process.cwd(), "test/less");
                 options.sourceMapRootpath = "testweb/";
+                // TODO seperate options?
+                options.sourceMap = new less.SourceMapBuilder(options);
             }
 
             options.getVars = function(file) {

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