pp-parachute
Version:
Airdrop JS Applications
115 lines (98 loc) • 3.75 kB
JavaScript
var Bundler = require('./bundler');
var Promise = require('bluebird');
var HamlCompiler = require('./compilers/haml');
var HtmlCompiler = require('./compilers/html');
var fastbind = require('../util/fastbind');
var inherits = require('util').inherits;
var path = require('path');
var TemplateBundler = function (assetGlobs, name) {
Bundler.call(this, assetGlobs, name);
this.shouldDeclareModule = true;
this.moduleName = 'Templates';
this.templateRootPath = this.rootPath;
};
inherits(TemplateBundler, Bundler);
TemplateBundler.prototype.declareModule = function (shouldDeclare) {
this.shouldDeclareModule = shouldDeclare;
return this;
};
TemplateBundler.prototype.setModuleName = function (moduleName) {
this.moduleName = moduleName;
return this;
};
TemplateBundler.prototype.setTemplateRoot = function(root) {
this.templateRootPath = root;
return this;
};
TemplateBundler.prototype.bundle = function(filePaths) {
return this.compile(filePaths).then(fastbind(function (compiledFiles) {
return this.processFiles(compiledFiles);
}, this));
};
TemplateBundler.prototype.processFiles = function (compiledFiles) {
var templates = [];
for(var i = 0; i < compiledFiles.length; i++) {
var promise = compiledFiles[i];
if (promise.isFulfilled()) {
templates.push(this.templatize(promise.value()));
} else {
return Promise.reject(promise.reason());
}
}
return this.bundleTemplates(templates);
};
//todo make file mode implicit if possible
TemplateBundler.prototype.compile = function (filePaths) {
if (!Array.isArray(filePaths)) filePaths = [filePaths];
var promises = [];
for (var i = 0; i < filePaths.length; i++) {
var filePath = filePaths[i];
var ext = path.extname(filePath);
switch (ext) {
case '.haml':
promises.push(HamlCompiler(filePath));
break;
case '.html':
promises.push(HtmlCompiler(filePath));
break;
default:
return Promise.reject({
path: filePath,
error: 'Template bundler cant handle files with extension: `' + ext + '`'
});
}
}
return Promise.settle(promises);
};
TemplateBundler.prototype.bundleTemplates = function (templates) {
var moduleDec = this.shouldDeclareModule && ', []' || '';
var templateString = 'angular.module(\'' + this.moduleName + '\'' + moduleDec + ')';
templateString += '.run(["$templateCache", function($templateCache) { \n';
for (var i = 0; i < templates.length; i++) {
templateString += templates[i];
}
templateString += '}]);';
return templateString;
};
TemplateBundler.prototype.templatize = function (file) {
var templateName = this.createTemplatePath(file.path, this.templateRootPath);
var templateContents = JSON.stringify(file.html);
return '\t$templateCache.put(\'' + templateName + '\', ' + templateContents + ');\n';
};
TemplateBundler.prototype.createTemplatePath = function (filePath) {
var ext = path.extname(filePath);
filePath = filePath.substring(0, filePath.length - ext.length) + '.html';
if (filePath.indexOf(this.templateRootPath) === 0) {
return filePath.substring(this.templateRootPath.length + 1);
}
return filePath;
};
TemplateBundler.prototype.showBundleErrorHtml = function() {
var retn = "<pre>";
retn += "File: " + this.bundleError.path;
retn += "\nError: " + this.bundleError.error;
retn += "\nLine: " + this.bundleError.line;
retn += ", Column: " + this.bundleError.col;
return retn;
};
module.exports = TemplateBundler;