Skip to content

Commit

Permalink
Merge pull request #2 from ericclemmons/1-concat-multiple
Browse files Browse the repository at this point in the history
Fix Multiple Template Concatenation

Closes #1
  • Loading branch information
ericclemmons committed Jan 9, 2013
2 parents 4ea887d + 7fb6d31 commit 68c9739
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 7 deletions.
9 changes: 8 additions & 1 deletion grunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,18 @@ module.exports = function(grunt) {
globals: {}
},
ngtemplates: {
multiple: {
options: {
base: 'test/fixtures'
},
src: ['test/fixtures/multiple/**/*.html'],
dest: 'tmp/multiple.js'
},
simple: {
options: {
base: 'test/fixtures'
},
src: ['test/fixtures/**/simple.html'],
src: ['test/fixtures/simple.html'],
dest: 'tmp/simple.js'
}
}
Expand Down
8 changes: 4 additions & 4 deletions tasks/lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ module.exports.init = function(grunt) {
var concat = function(base, files, callback) {
grunt.utils.async.concatSeries(files, function(file, next) {
var id = path.relative(base, file);
var template = '\n $templateCache.put("<%= id %>", "<%= content %>"\n );\n';
var template = '\n $templateCache.put("<%= id %>",\n "<%= content %>"\n );\n';
var cleaned = grunt.file.read(file).replace(/"/g, '\\"').replace(/\r?\n/g, '" +\n "');
var cached = grunt.template.process(template, {
id: id,
content: cleaned
});

next(cached);
next(null, cached);
}, callback);
};

var compile = function(id, base, files, callback) {
var template = 'angular.module("<%= id %>", []).run(["$templateCache", function($templateCache) {\n<%= content %>\n}]);\n';

concat(base, files, function(concated) {
concat(base, files, function(err, concated) {
var compiled = grunt.template.process(template, {
id: id,
content: concated
content: concated.join('')
});

callback(false, compiled);
Expand Down
12 changes: 11 additions & 1 deletion test/angular-templates_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ exports.ngtemplates = {
simple: function(test) {
test.expect(1);

var actual = grunt.file.read('tmp/simple.js');
var actual = grunt.file.read('tmp/simple.js');
var expected = grunt.file.read('test/expected/simple.js');

test.equal(expected, actual, 'should compile template as module `simple.templates`');
test.done();
},

multiple: function(test) {
test.expect(1);

var actual = grunt.file.read('tmp/multiple.js');
var expected = grunt.file.read('test/expected/multiple.js');

test.equal(expected, actual, 'should compile multiple templates together as `multiple.templates`');
test.done();
}

};
17 changes: 17 additions & 0 deletions test/expected/multiple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
angular.module("multiple.templates", []).run(["$templateCache", function($templateCache) {

$templateCache.put("multiple/one.html",
"<h1>One</h1>" +
"" +
"<p>I am one.</p>" +
""
);

$templateCache.put("multiple/two/two.html",
"<h2>Two</h2>" +
"" +
"<p>We are two.</p>" +
""
);

}]);
3 changes: 2 additions & 1 deletion test/expected/simple.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
angular.module("simple.templates", []).run(["$templateCache", function($templateCache) {

$templateCache.put("simple.html", "Howdy there! Your name is \"{{ name }}\"." +
$templateCache.put("simple.html",
"Howdy there! Your name is \"{{ name }}\"." +
""
);

Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/multiple/one.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>One</h1>

<p>I am one.</p>
3 changes: 3 additions & 0 deletions test/fixtures/multiple/two/two.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h2>Two</h2>

<p>We are two.</p>

0 comments on commit 68c9739

Please sign in to comment.