UNPKG

asset-rack

Version:

Static Web Framework for Nodejs

118 lines (103 loc) 3.71 kB
// Generated by CoffeeScript 1.6.3 (function() { var Asset, ensureLessError, fs, less, pathutil, 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; }; less = require('less'); fs = require('fs'); pathutil = require('path'); Asset = require('../.').Asset; urlRegex = /url\s*\(\s*(['"])((?:(?!\1).)+)\1\s*\)/; urlRegexGlobal = /url\s*\(\s*(['"])((?:(?!\1).)+)\1\s*\)/g; exports.LessAsset = (function(_super) { __extends(LessAsset, _super); function LessAsset() { _ref = LessAsset.__super__.constructor.apply(this, arguments); return _ref; } LessAsset.prototype.mimetype = 'text/css'; LessAsset.prototype.create = function(options) { var error, fileContents, parser, _this = this; if (options.filename) { this.filename = pathutil.resolve(options.filename); fileContents = fs.readFileSync(this.filename, 'utf8'); } else { if (fileContents == null) { fileContents = options.contents; } } this.paths = options.paths; if (this.paths == null) { this.paths = []; } this.paths.push(pathutil.dirname(options.filename)); this.toWatch = pathutil.dirname(this.filename); this.compress = options.compress; if (this.compress == null) { this.compress = false; } try { parser = new less.Parser({ filename: this.filename, paths: this.paths }); return parser.parse(fileContents, function(error, tree) { var match, quote, raw, result, results, specificUrl, url, _i, _len; if (error != null) { return _this.emit('error', ensureLessError(error)); } raw = tree.toCSS({ compress: _this.compress }); if (_this.rack != null) { results = raw.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) { raw = raw.replace(result, "url(" + quote + specificUrl + quote + ")"); } } } } return _this.emit('created', { contents: raw }); }); } catch (_error) { error = _error; error = ensureLessError(error); return this.emit('error', error); } }; return LessAsset; })(Asset); ensureLessError = function(error) { var extract, line, msg, _i, _len, _ref1; if (!(error instanceof Error)) { if (!error.filename) { error.filename = "[provided asset content]"; } msg = "Less error: " + error.message + "\n\tfilename: " + error.filename + "\n\tline " + error.line + " column " + error.column; line = error.line; msg += "\n\t..."; if (error.extract != null) { _ref1 = error.extract; for (_i = 0, _len = _ref1.length; _i < _len; _i++) { extract = _ref1[_i]; if (extract) { msg += "\n\t " + (line++) + ": " + extract; } } } msg += "\n\t..."; error = new Error(msg); } return error; }; }).call(this);