asset-rack
Version:
Static Web Framework for Nodejs
84 lines (69 loc) • 2.75 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var Asset, fs, nib, pathutil, stylus, urlRegex, urlRegexGlobal, _ref,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
fs = require('fs');
pathutil = require('path');
nib = require('nib');
stylus = require('stylus');
Asset = require('../.').Asset;
urlRegex = /url\s*\(\s*(['"])((?:(?!\1).)+)\1\s*\)/;
urlRegexGlobal = /url\s*\(\s*(['"])((?:(?!\1).)+)\1\s*\)/g;
exports.StylusAsset = (function(_super) {
__extends(StylusAsset, _super);
function StylusAsset() {
_ref = StylusAsset.__super__.constructor.apply(this, arguments);
return _ref;
}
StylusAsset.prototype.mimetype = 'text/css';
StylusAsset.prototype.create = function(options) {
var _this = this;
this.filename = pathutil.resolve(options.filename);
this.toWatch = pathutil.dirname(this.filename);
this.compress = options.compress;
if (this.compress == null) {
this.compress = process.env.NODE_ENV === 'production';
}
this.config = options.config;
if (this.config == null) {
this.config = function() {
return this.use(nib());
};
}
return fs.readFile(this.filename, 'utf8', function(error, data) {
var styl;
if (error != null) {
return _this.emit('error', error);
}
styl = stylus(data).set('compress', _this.compress).set('include css', true);
_this.config.call(styl, styl);
return styl.set('filename', _this.filename).render(function(error, css) {
var match, quote, result, results, specificUrl, url, _i, _len;
if (error != null) {
return _this.emit('error', error);
}
if (_this.rack != null) {
results = css.match(urlRegexGlobal);
if (results) {
for (_i = 0, _len = results.length; _i < _len; _i++) {
result = results[_i];
match = urlRegex.exec(result);
quote = match[1];
url = match[2];
specificUrl = _this.rack.url(url);
if (specificUrl != null) {
css = css.replace(result, "url(" + quote + specificUrl + quote + ")");
}
}
}
}
return _this.emit('created', {
contents: css
});
});
});
};
return StylusAsset;
})(Asset);
}).call(this);