jade-angularjs-brunch
Version:
Adds Jade support to brunch with angularjs template cache modules.
199 lines (177 loc) • 7.03 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var JadeAngularJsCompiler, fileWriter, fs, jade, mkdirp, sysPath, _;
jade = require('jade');
sysPath = require('path');
mkdirp = require('mkdirp');
fs = require('fs');
_ = require('lodash');
fileWriter = function(newFilePath) {
return function(err, content) {
var dirname;
if (err != null) {
throw err;
}
if (content == null) {
return;
}
dirname = sysPath.dirname(newFilePath);
return mkdirp(dirname, '0775', function(err) {
if (err != null) {
throw err;
}
return fs.writeFile(newFilePath, content, function(err) {
if (err != null) {
throw err;
}
});
});
};
};
module.exports = JadeAngularJsCompiler = (function() {
JadeAngularJsCompiler.prototype.brunchPlugin = true;
JadeAngularJsCompiler.prototype.type = 'template';
JadeAngularJsCompiler.prototype.extension = 'jade';
function JadeAngularJsCompiler(config) {
var _ref, _ref1, _ref10, _ref11, _ref12, _ref13, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9;
this["public"] = ((_ref = config.paths) != null ? _ref["public"] : void 0) || "_public";
this.pretty = !!((_ref1 = config.plugins) != null ? (_ref2 = _ref1.jade) != null ? _ref2.pretty : void 0 : void 0);
this.doctype = ((_ref3 = config.plugins) != null ? (_ref4 = _ref3.jade) != null ? _ref4.doctype : void 0 : void 0) || "5";
this.locals = ((_ref5 = config.plugins) != null ? (_ref6 = _ref5.jade_angular) != null ? _ref6.locals : void 0 : void 0) || {};
this.staticMask = ((_ref7 = config.plugins) != null ? (_ref8 = _ref7.jade_angular) != null ? _ref8.static_mask : void 0 : void 0) || /index.jade/;
this.compileTrigger = sysPath.normalize(this["public"] + sysPath.sep + (((_ref9 = config.paths) != null ? _ref9.jadeCompileTrigger : void 0) || 'js/dontUseMe'));
this.singleFile = !!(config != null ? (_ref10 = config.plugins) != null ? (_ref11 = _ref10.jade_angular) != null ? _ref11.single_file : void 0 : void 0 : void 0);
this.singleFileName = sysPath.join(this["public"], (config != null ? (_ref12 = config.plugins) != null ? (_ref13 = _ref12.jade_angular) != null ? _ref13.single_file_name : void 0 : void 0 : void 0) || "js/angular_templates.js");
}
JadeAngularJsCompiler.prototype.compile = function(data, path, callback) {
var content, err, error;
try {
content = jade.compile(data, {
compileDebug: false,
client: false,
filename: path,
doctype: this.doctype,
pretty: this.pretty
});
return content(this.locals);
} catch (_error) {
err = _error;
return error = err;
} finally {
callback(error, "");
}
};
JadeAngularJsCompiler.prototype.preparePairStatic = function(pair) {
pair.path.push(pair.path.pop().slice(0, -this.extension.length) + 'html');
return pair.path.splice(0, 1, this["public"]);
};
JadeAngularJsCompiler.prototype.writeStatic = function(pairs) {
var _this = this;
return _.each(pairs, function(pair) {
var writer;
_this.preparePairStatic(pair);
writer = fileWriter(sysPath.join.apply(_this, pair.path));
return writer(null, pair.result);
});
};
JadeAngularJsCompiler.prototype.attachModuleNameToTemplate = function(pair) {
return pair.module = pair.path.slice(0, -1).join('.');
};
JadeAngularJsCompiler.prototype.generateModuleFileName = function(module) {
return module.filename = sysPath.join.apply(this, [this["public"], 'js', module.name + ".js"]);
};
JadeAngularJsCompiler.prototype.writeModules = function(modules) {
var buildModule, content, writer;
buildModule = function(module) {
var addEndOfModule, content, moduleHeader, templateRecord;
moduleHeader = function(name) {
return "angular.module('" + name + "', [])";
};
templateRecord = function(result, path) {
var parseStringToJSArray;
parseStringToJSArray = function(str) {
var stringArray;
stringArray = '[';
str.split('\n').map(function(e, i) {
return stringArray += "\n'" + e.replace(/'/g, "\\'") + "',";
});
return stringArray += "''" + '].join("\\n")';
};
return "\n.run(['$templateCache', function($templateCache) {\n return $templateCache.put('" + path + "', " + (parseStringToJSArray(result)) + ");\n}])";
};
addEndOfModule = function() {
return ";\n";
};
content = moduleHeader(module.name);
_.each(module.templates, function(template) {
return content += templateRecord(template.result, template.path);
});
return content += addEndOfModule();
};
content = "";
_.each(modules, function(module) {
var moduleContent, writer;
moduleContent = buildModule(module);
if (this.singleFile) {
return content += "\n" + moduleContent;
} else {
writer = fileWriter(module.filename);
return writer(null, moduleContent);
}
});
if (this.singleFile) {
writer = fileWriter(this.singleFileName);
return writer(null, content);
}
};
JadeAngularJsCompiler.prototype.prepareResult = function(compiled) {
var pathes,
_this = this;
pathes = _.find(compiled, function(v) {
return v.path === _this.compileTrigger;
});
if (pathes === void 0) {
return [];
}
return pathes.sourceFiles.map(function(e, i) {
var content, data, result;
data = fs.readFileSync(e.path, 'utf8');
content = jade.compile(data, {
compileDebug: false,
client: false,
filename: e.path,
doctype: _this.doctype,
pretty: _this.pretty
});
return result = {
path: e.path.split(sysPath.sep),
result: content(_this.locals)
};
});
};
JadeAngularJsCompiler.prototype.onCompile = function(compiled) {
var assets, preResult,
_this = this;
preResult = this.prepareResult(compiled);
assets = _.filter(preResult, function(v) {
return _this.staticMask.test(v.path);
});
this.writeStatic(assets);
return this.writeModules(_.chain(preResult).difference(assets).each(function(v) {
return _this.attachModuleNameToTemplate(v);
}).each(function(v) {
return v.path = v.path.join('/');
}).groupBy(function(v) {
return v.module;
}).map(function(v, k) {
return {
name: k,
templates: v
};
}).each(function(v) {
return _this.generateModuleFileName(v);
}).value());
};
return JadeAngularJsCompiler;
})();
}).call(this);