Skip to content

Commit

Permalink
Tag 0.4.1
Browse files Browse the repository at this point in the history
Add support to embed a comment containing the path to the template.
  • Loading branch information
Richard Quadling authored and Richard Quadling committed Jul 18, 2017
1 parent 6c4c905 commit 69a04ce
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,13 @@ module.exports = function (grunt) {
},
src: ['test/fixtures/one.tpl.html', 'test/fixtures/two.tpl.html'],
dest: 'tmp/amd_module_custom_suffix.js'
},
template_path_in_comment: {
options: {
templatePathInComment: true
},
src: ['test/fixtures/three.tpl.html'],
dest: 'tmp/template_path_in_comment.js'
}
},

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ options: {
}
```

#### templatePathInComment:
Type: `Boolean`
Default value: `false`

If specified, adds an HTML comment containing the template file path as a comment at the start of each template.

### Usage Examples

See the `Gruntfile.js` in the project source code for various configuration examples.
Expand Down Expand Up @@ -345,3 +351,5 @@ As of 0.3.7, this package is now administered by Richard Quadling who gives a bi
0.4.0 Added ability to render pug templates. Maintains Backwards compatibility. (#83)

Marked support for jade templates as deprecated. Support will be removed in 0.5.0

0.4.1 Added ability to add an HTML comment to the templates (#9/#10)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "grunt-html2js",
"description": "Compiles AngularJS templates to JavaScript",
"version": "0.4.0",
"version": "0.4.1",
"homepage": "https://github.com/rquadling/grunt-html2js",
"author": {
"name": "Karl Goldstein",
Expand Down
12 changes: 7 additions & 5 deletions tasks/html2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ module.exports = function (grunt) {
var path = require('path');
var minify = require('html-minifier').minify;

var escapeContent = function (content, quoteChar, indentString) {
var escapeContent = function (content, quoteChar, indentString, pathToAddAsComment) {
content = (pathToAddAsComment ? '<!-- template: ' + pathToAddAsComment + ' -->\n' : '') + content;
var bsRegexp = new RegExp('\\\\', 'g');
var quoteRegexp = new RegExp('\\' + quoteChar, 'g');
var nlReplace = '\\n' + quoteChar + ' +\n' + indentString + indentString + quoteChar;
Expand Down Expand Up @@ -44,7 +45,7 @@ module.exports = function (grunt) {
var jadeExtension = /\.jade$/;
return jadeExtension.test(filepath);
}

function isPugTemplate(filepath) {
var pugExtension = /\.pug$/;
return pugExtension.test(filepath);
Expand Down Expand Up @@ -85,7 +86,7 @@ module.exports = function (grunt) {
// trim leading whitespace
content = content.replace(/(^\s*)/g, '');

return escapeContent(content, options.quoteChar, options.indentString);
return escapeContent(content, options.quoteChar, options.indentString, options.templatePathInComment ? filepath : '');
};

// compile a template to an angular module
Expand Down Expand Up @@ -156,7 +157,8 @@ module.exports = function (grunt) {
watch: false,
amd: false,
amdPrefixString: "define(['angular'], function(angular){",
amdSuffixString: "});"
amdSuffixString: "});",
templatePathInComment: false
});

var counter = 0;
Expand Down Expand Up @@ -269,7 +271,7 @@ module.exports = function (grunt) {

this.files.forEach(generateModule);

//Just have one output, so if we making thirty files it only does one line
//Just have one output, so if we are making thirty files it only does one line
grunt.log.writeln("Successfully converted " + ("" + counter).green +
" html templates to " + options.target + ".");
});
Expand Down
9 changes: 9 additions & 0 deletions test/expected/template_path_in_comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
angular.module('templates-template_path_in_comment', ['../test/fixtures/three.tpl.html']);

angular.module("../test/fixtures/three.tpl.html", []).run(["$templateCache", function($templateCache) {
$templateCache.put("../test/fixtures/three.tpl.html",
"<!-- template: test/fixtures/three.tpl.html -->\n" +
"Multiple\n" +
"Lines\n" +
"");
}]);
9 changes: 9 additions & 0 deletions test/html2js_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,15 @@ exports.html2js = {
'test/expected/amd_module_custom_suffix.js',
'expected use of amd module with custom suffix');

test.done();
},
template_path_in_comment: function(test) {
test.expect(1);

assertFileContentsEqual(test, 'tmp/template_path_in_comment.js',
'test/expected/template_path_in_comment.js',
'expected template path in comment');

test.done();
}
};

0 comments on commit 69a04ce

Please sign in to comment.