From 5325a2e4b4b8f847ead6abbc6a5ebedc7cfdeb26 Mon Sep 17 00:00:00 2001 From: Daniel Stockman Date: Fri, 11 Jan 2013 15:18:54 -0800 Subject: [PATCH] Provide fallback 'mime' object that covers 80% case of data-uri usage. --- lib/less/functions.js | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/less/functions.js b/lib/less/functions.js index 8c3af9a4..62f564f9 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -387,7 +387,13 @@ tree.functions = { // detect the mimetype if not given if (arguments.length < 2) { - var mime = require('mime'); + var mime; + try { + mime = require('mime'); + } catch (ex) { + mime = tree._mime; + } + path = mimetype; mimetype = mime.lookup(path); @@ -410,6 +416,34 @@ tree.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' : ''; + } + } +}; + var mathFunctions = [{name:"ceil"}, {name:"floor"}, {name: "sqrt"}, {name:"abs"}, {name:"tan", unit: ""}, {name:"sin", unit: ""}, {name:"cos", unit: ""}, {name:"atan", unit: "rad"}, {name:"asin", unit: "rad"}, {name:"acos", unit: "rad"}],