Skip to content

Instantly share code, notes, and snippets.

@AWolf81 AWolf81/index.coffee
Created Jul 12, 2015

Embed
What would you like to do?
Workaround for angular-template-brunch with-out CommonJs
module.exports = class NgTemplatesCompiler
brunchPlugin: yes
type: 'template'
extension: 'js'
# Add to this list of markup to wrap for AngularJS
pattern: /\.(html|jade|eco|hbs|handlebars)$/
constructor: (config) ->
@module = config.plugins?.ng_templates?.module or 'appTemplates'
compile: (data, path, callback) ->
# Function.prototype.extractComment ->
# startComment = "/*!"
# endComment = "*/"
# str = this.toString();
# start = str.indexOf(startComment);
# end = str.lastIndexOf(endComment);
# str.slice(start + startComment.length, -(str.length - end))
callback null, """
(function() {
var module;
Function.prototype.extractComment = function() {
var startComment = "/*!";
var endComment = "*/";
var str = this.toString();
var start = str.indexOf(startComment);
var end = str.lastIndexOf(endComment);
return str.slice(start + startComment.length, -(str.length - end));
};
try {
// Get current templates module
module = angular.module('#{@module}');
} catch (error) {
// Or create a new one
module = angular.module('#{@module}', []);
}
module.run(function($templateCache) {
// Force CommonJS to capture from preprocessors
var define, module = { exports: true };
// Include the data from preprocessor
var tmpl = function() {/*!
#{data}
*/
}.extractComment();
// Save the template content
$templateCache.put('#{path}', tmpl);
/*if (typeof module.exports === 'function') {
$templateCache.put('#{path}', module.exports());
} else {
$templateCache.put('#{path}', module.exports);
}*/
});
})();
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.