asset-rack
Version:
Static Web Framework for Nodejs
142 lines (126 loc) • 4.83 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var Asset, async, fs, jade, pathutil, uglify, _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');
uglify = require('uglify-js');
async = require('async');
jade = require('jade');
Asset = require('../index').Asset;
exports.JadeAsset = (function(_super) {
__extends(JadeAsset, _super);
function JadeAsset() {
_ref = JadeAsset.__super__.constructor.apply(this, arguments);
return _ref;
}
JadeAsset.prototype.mimetype = 'text/javascript';
JadeAsset.prototype.create = function(options) {
var asset, assets, _i, _len, _ref1;
this.dirname = pathutil.resolve(options.dirname);
this.separator = options.separator || '/';
this.compress = options.compress || false;
this.toWatch = this.dirname;
this.clientVariable = options.clientVariable || 'Templates';
this.beforeCompile = options.beforeCompile || null;
this.fileObjects = this.getFileobjects(this.dirname);
if (this.rack != null) {
assets = {};
_ref1 = this.rack.assets;
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
asset = _ref1[_i];
assets[asset.url] = asset.specificUrl;
}
this.assetsMap = "var assets = { \n assets: " + (JSON.stringify(assets)) + ",\n url: " + (function(url) {
return this.assets[url];
}) + "\n};";
}
return this.createContents();
};
JadeAsset.prototype.createContents = function() {
var fileObject, _i, _len, _ref1;
this.contents = fs.readFileSync(require.resolve('jade').replace('index.js', 'runtime.js'));
if (this.assetsMap != null) {
this.contents += '(function(){ \n';
}
if (this.assetsMap != null) {
this.contents += this.assetsMap;
}
this.contents += "window." + this.clientVariable + " = {\n";
this.fileContents = "";
_ref1 = this.fileObjects;
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
fileObject = _ref1[_i];
if (this.fileContents.length > 0) {
this.fileContents += ",";
}
if (this.assetsMap != null) {
this.fileContents += "'" + fileObject.funcName + "': function(locals) {\n locals = locals || {};\n locals['assets'] = assets;\n return (" + fileObject.compiled + ")(locals)\n}";
} else {
this.fileContents += "'" + fileObject.funcName + "': " + fileObject.compiled;
}
}
this.contents += this.fileContents;
this.contents += '};';
if (this.assetsMap != null) {
this.contents += '})();';
}
if (this.compress) {
this.contents = uglify.minify(this.contents, {
fromString: true
}).code;
}
if (!this.hasError) {
return this.emit('created');
}
};
JadeAsset.prototype.getFileobjects = function(dirname, prefix) {
var compiled, error, fileContents, filename, filenames, funcName, newPrefix, path, paths, stats, _i, _len;
if (prefix == null) {
prefix = '';
}
filenames = fs.readdirSync(dirname);
paths = [];
for (_i = 0, _len = filenames.length; _i < _len; _i++) {
filename = filenames[_i];
if (filename.slice(0, 1) === '.') {
continue;
}
path = pathutil.join(dirname, filename);
stats = fs.statSync(path);
if (stats.isDirectory()) {
newPrefix = "" + prefix + (pathutil.basename(path)) + this.separator;
paths = paths.concat(this.getFileobjects(path, newPrefix));
} else {
if (filename.indexOf('.jade') === -1) {
continue;
}
funcName = "" + prefix + (pathutil.basename(path, '.jade'));
fileContents = fs.readFileSync(path, 'utf8');
if (this.beforeCompile != null) {
fileContents = this.beforeCompile(fileContents);
}
try {
compiled = jade.compile(fileContents, {
client: true,
compileDebug: false,
filename: path
});
paths.push({
path: path,
funcName: funcName,
compiled: compiled
});
} catch (_error) {
error = _error;
this.hasError = true;
this.emit('error', error);
}
}
}
return paths;
};
return JadeAsset;
})(Asset);
}).call(this);