Skip to content

Instantly share code, notes, and snippets.

@AWolf81
Created July 12, 2015 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AWolf81/c777e919f7d50f186d56 to your computer and use it in GitHub Desktop.
Save AWolf81/c777e919f7d50f186d56 to your computer and use it in GitHub Desktop.
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