continue moving node functionality into the environment interface. function names need making consistent etc.

This commit is contained in:
Luke Page
2014-02-24 21:51:38 +00:00
parent 462b0274b2
commit ddf1a45e5e
4 changed files with 31 additions and 49 deletions

View File

@@ -36,5 +36,7 @@ module.exports = {
* @returns {Boolean}
*/
loadFile: function(env, filename, currentDirectory, callback) {
},
supportsDataURI: function(env) {
}
};

View File

@@ -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;
},

View File

@@ -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 = {

View File

@@ -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 = "*";