[Pkg-javascript-commits] [less.js] 25/285: continue moving node functionality into the environment interface. function names need making consistent etc.

Jonas Smedegaard dr at jones.dk
Mon Oct 26 23:23:34 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 ddf1a45e5eb572d72255acfe36cd8b03ec0d3141
Author: Luke Page <luke.a.page at gmail.com>
Date:   Mon Feb 24 21:51:38 2014 +0000

    continue moving node functionality into the environment interface. function names need making consistent etc.
---
 lib/less/environments/api.js  |  2 ++
 lib/less/environments/node.js | 18 ++++++++++++++++
 lib/less/functions.js         | 50 ++++++-------------------------------------
 lib/less/parser.js            | 10 ++++-----
 4 files changed, 31 insertions(+), 49 deletions(-)

diff --git a/lib/less/environments/api.js b/lib/less/environments/api.js
index 483895f..74f0dcc 100644
--- a/lib/less/environments/api.js
+++ b/lib/less/environments/api.js
@@ -36,5 +36,7 @@ module.exports = {
       * @returns {Boolean}
       */
     loadFile: function(env, filename, currentDirectory, callback) {
+    },
+    supportsDataURI: function(env) {
     }
 };
\ No newline at end of file
diff --git a/lib/less/environments/node.js b/lib/less/environments/node.js
index a4cae9b..ea4f240 100644
--- a/lib/less/environments/node.js
+++ b/lib/less/environments/node.js
@@ -11,6 +11,21 @@ module.exports = {
     encodeBase64: function encodeBase64(env, str) {
         return new Buffer(str).toString('base64');
     },
+    supportsDataURI: function(env) {
+        return true;
+    },
+    mimeLookup: function (env, filename) {
+        return require('mime').lookup(filename);
+    },
+    charsetLookup: function (env, mime) {
+        return require('mime').charsets.lookup(mime);
+    },
+    readFileSync: function (filename) {
+        return require("fs").readFileSync(filename);
+    },
+    getCleanCSS: function() {
+        return require('clean-css');
+    },
     getPath: function (env, filename) {
         var j = filename.lastIndexOf('/');
         if (j < 0) {
@@ -24,6 +39,9 @@ module.exports = {
     isPathAbsolute: function(env, filename) {
         return (/^(?:[a-z-]+:|\/|\\)/).test(filename);
     },
+    getAbsolutePath: function getAbsolutePath(env, filename) {
+        return require('path').resolve(filename);
+    },
     alwaysMakePathsAbsolute: function alwaysMakePathsAbsolute() {
         return false;
     },
diff --git a/lib/less/functions.js b/lib/less/functions.js
index 47cf330..d0cf835 100644
--- a/lib/less/functions.js
+++ b/lib/less/functions.js
@@ -397,16 +397,14 @@ var functions = {
 
     "data-uri": function(mimetypeNode, filePathNode) {
 
-        if (typeof window !== 'undefined') {
+        if (!less.environment.supportsDataURI(this.env)) {
             return new tree.URL(filePathNode || mimetypeNode, this.currentFileInfo).eval(this.env);
         }
 
         var mimetype = mimetypeNode.value;
         var filePath = (filePathNode && filePathNode.value);
 
-        var fs = require('fs'),
-            path = require('path'),
-            useBase64 = false;
+        var useBase64 = false;
 
         if (arguments.length < 2) {
             filePath = mimetype;
@@ -414,25 +412,19 @@ var functions = {
 
         if (this.env.isPathRelative(filePath)) {
             if (this.currentFileInfo.relativeUrls) {
-                filePath = path.join(this.currentFileInfo.currentDirectory, filePath);
+                filePath = less.environment.join(this.currentFileInfo.currentDirectory, filePath);
             } else {
-                filePath = path.join(this.currentFileInfo.entryPath, filePath);
+                filePath = less.environment.join(this.currentFileInfo.entryPath, filePath);
             }
         }
 
         // detect the mimetype if not given
         if (arguments.length < 2) {
-            var mime;
-            try {
-                mime = require('mime');
-            } catch (ex) {
-                mime = tree._mime;
-            }
 
-            mimetype = mime.lookup(filePath);
+            mimetype = less.environment.mimeLookup(this.env, filePath);
 
             // use base 64 unless it's an ASCII or UTF-8 format
-            var charset = mime.charsets.lookup(mimetype);
+            var charset = less.environment.charsetLookup(this.env, mimetype);
             useBase64 = ['US-ASCII', 'UTF-8'].indexOf(charset) < 0;
             if (useBase64) { mimetype += ';base64'; }
         }
@@ -440,7 +432,7 @@ var functions = {
             useBase64 = /;base64$/.test(mimetype);
         }
 
-        var buf = fs.readFileSync(filePath);
+        var buf = less.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.
@@ -541,34 +533,6 @@ var functions = {
     }
 };
 
-// these static methods are used as a fallback when the optional 'mime' dependency is missing
-tree._mime = {
-    // this map is intentionally incomplete
-    // if you want more, install 'mime' dep
-    _types: {
-        '.htm' : 'text/html',
-        '.html': 'text/html',
-        '.gif' : 'image/gif',
-        '.jpg' : 'image/jpeg',
-        '.jpeg': 'image/jpeg',
-        '.png' : 'image/png'
-    },
-    lookup: function (filepath) {
-        var ext = require('path').extname(filepath),
-            type = tree._mime._types[ext];
-        if (type === undefined) {
-            throw new Error('Optional dependency "mime" is required for ' + ext);
-        }
-        return type;
-    },
-    charsets: {
-        lookup: function (type) {
-            // assumes all text types are UTF-8
-            return type && (/^text\//).test(type) ? 'UTF-8' : '';
-        }
-    }
-};
-
 // Math
 
 var mathFunctions = {
diff --git a/lib/less/parser.js b/lib/less/parser.js
index af23116..ee56599 100644
--- a/lib/less/parser.js
+++ b/lib/less/parser.js
@@ -312,9 +312,7 @@ var Parser = function Parser(env) {
 
     function getDebugInfo(index, inputStream, env) {
         var filename = env.currentFileInfo.filename;
-        if(less.mode !== 'browser' && less.mode !== 'rhino') {
-            filename = require('path').resolve(filename);
-        }
+        filename = less.environment.getAbsolutePath(env, filename);
 
         return {
             lineNumber: getLocation(index, inputStream).line + 1,
@@ -614,9 +612,9 @@ var Parser = function Parser(env) {
                         throw new(LessError)(e, env);
                     }
 
-                    if (options.cleancss && less.mode === 'node') {
-                        var CleanCSS = require('clean-css'),
-                            cleancssOptions = options.cleancssOptions || {};
+                    var CleanCSS = less.environment.getCleanCSS();
+                    if (options.cleancss && CleanCSS) {
+                        var cleancssOptions = options.cleancssOptions || {};
 
                         if (cleancssOptions.keepSpecialComments === undefined) {
                             cleancssOptions.keepSpecialComments = "*";

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