From 69a04ce0926fab0a28c3ba5f5f7d7ff5c68ba098 Mon Sep 17 00:00:00 2001 From: Richard Quadling Date: Tue, 18 Jul 2017 17:51:01 +0100 Subject: [PATCH] Tag 0.4.1 Add support to embed a comment containing the path to the template. --- Gruntfile.js | 7 +++++++ README.md | 8 ++++++++ package.json | 2 +- tasks/html2js.js | 12 +++++++----- test/expected/template_path_in_comment.js | 9 +++++++++ test/html2js_test.js | 9 +++++++++ 6 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 test/expected/template_path_in_comment.js diff --git a/Gruntfile.js b/Gruntfile.js index ee31b20..c956ffb 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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' } }, diff --git a/README.md b/README.md index 261be61..0824817 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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) diff --git a/package.json b/package.json index b58e7c7..085c394 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tasks/html2js.js b/tasks/html2js.js index 973becd..ad4b45f 100644 --- a/tasks/html2js.js +++ b/tasks/html2js.js @@ -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 ? '\n' : '') + content; var bsRegexp = new RegExp('\\\\', 'g'); var quoteRegexp = new RegExp('\\' + quoteChar, 'g'); var nlReplace = '\\n' + quoteChar + ' +\n' + indentString + indentString + quoteChar; @@ -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); @@ -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 @@ -156,7 +157,8 @@ module.exports = function (grunt) { watch: false, amd: false, amdPrefixString: "define(['angular'], function(angular){", - amdSuffixString: "});" + amdSuffixString: "});", + templatePathInComment: false }); var counter = 0; @@ -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 + "."); }); diff --git a/test/expected/template_path_in_comment.js b/test/expected/template_path_in_comment.js new file mode 100644 index 0000000..1791367 --- /dev/null +++ b/test/expected/template_path_in_comment.js @@ -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", + "\n" + + "Multiple\n" + + "Lines\n" + + ""); +}]); diff --git a/test/html2js_test.js b/test/html2js_test.js index c342fc3..308569f 100644 --- a/test/html2js_test.js +++ b/test/html2js_test.js @@ -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(); } };