diff --git a/lib/less/environments/api.js b/lib/less/environments/api.js index 483895f3..74f0dcc0 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 a4cae9be..ea4f2408 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 47cf3304..d0cf8358 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 af231160..ee565994 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 = "*";