Skip to content

Commit

Permalink
Allow to set the file size limit of image inlining to false to inline…
Browse files Browse the repository at this point in the history
… all files. Also fix some var-redeclarations.
  • Loading branch information
cpojer committed Aug 13, 2012
1 parent 42d89a5 commit 7c7047d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
12 changes: 6 additions & 6 deletions docs/functions.url.md
Expand Up @@ -12,21 +12,21 @@ The `.define(name, callback)` method assigned a JavaScript function that can be
.set('filename', __dirname + '/css/test.styl')
.define('url', stylus.url())
.render(function(err, css){

});

For example, imagine our images live in `./public/images`. We want to use `url(images/tobi.png)`. We could pass `paths` our public directory, so that it becomes part of the lookup process.
For example, imagine our images live in `./public/images`. We want to use `url(images/tobi.png)`. We could pass `paths` our public directory, so that it becomes part of the lookup process.

Likewise, if instead we wanted `url(tobi.png)`, we could pass `paths: [__dirname + '/public/images']`.

stylus(str)
.set('filename', __dirname + '/css/test.styl')
.define('url', stylus.url({ paths: [__dirname + '/public'] }))
.render(function(err, css){

});

### Options

- `limit` bytesize limit defaulting to 30Kb (30000)
- `paths` image resolution path(s)
- `limit` bytesize limit defaulting to 30Kb (30000), use `false` to disable the limit
- `paths` image resolution path(s)
22 changes: 10 additions & 12 deletions lib/functions/url.js
Expand Up @@ -51,24 +51,22 @@ var mimes = {
module.exports = function(options) {
options = options || {};

var sizeLimit = options.limit || 30000
, _paths = options.paths || [];
var _paths = options.paths || [];

function url(url){
function fn(url){
// Compile the url
var compiler = new Compiler(url);
compiler.isURL = true;
var url = url.nodes.map(function(node){
url = url.nodes.map(function(node){
return compiler.visit(node);
}).join('');

// Parse literal
var url = parse(url)
, ext = extname(url.pathname)
// Parse literal
url = parse(url)
var ext = extname(url.pathname)
, mime = mimes[ext]
, literal = new nodes.Literal('url("' + url.href + '")')
, paths = _paths.concat(this.paths)
, founds
, buf;

// Not supported
Expand All @@ -87,12 +85,12 @@ module.exports = function(options) {
buf = fs.readFileSync(found);

// To large
if (buf.length > sizeLimit) return literal;
if ('number' == typeof options.limit && buf.length > options.limit) return literal;

// Encode
return new nodes.Literal('url("data:' + mime + ';base64,' + buf.toString('base64') + '")');
};

url.raw = true;
return url;
};
fn.raw = true;
return fn;
};

0 comments on commit 7c7047d

Please sign in to comment.