diff --git a/Gruntfile.js b/Gruntfile.js index 85c58c2..f909a9b 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -257,6 +257,36 @@ module.exports = function(grunt) { dest: 'tmp/process_all_jade.js' }, + process_pug: { + src: ['test/fixtures/process_pug.pug'], + dest: 'tmp/process_pug.js' + }, + + process_pug_custom: { + options: { + pug: {doctype: 'html'} + }, + src: ['test/fixtures/process_pug_custom.pug'], + dest: 'tmp/process_pug_custom.js' + }, + + process_pug_with_include: { + options: { + pug: {} + }, + src: ['test/fixtures/process_pug_with_include.pug'], + dest: 'tmp/process_pug_with_include.js' + }, + + process_all_pug: { + options: { + pug: {}, + watch: true + }, + src: ['test/fixtures/*.pug'], + dest: 'tmp/process_all_pug.js' + }, + single_module: { options: { singleModule: true diff --git a/package.json b/package.json index 036db91..067eb8c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "grunt-html2js", "description": "Compiles AngularJS templates to JavaScript", - "version": "0.3.6", + "version": "1.0.0", "homepage": "https://github.com/karlgoldstein/grunt-html2js", "author": { "name": "Karl Goldstein", @@ -28,10 +28,10 @@ "test": "grunt test" }, "devDependencies": { - "grunt-contrib-jshint": "~0.1.1", + "grunt": "~0.4.0", "grunt-contrib-clean": "~0.4.0", + "grunt-contrib-jshint": "~0.1.1", "grunt-contrib-nodeunit": "^0.3.3", - "grunt": "~0.4.0", "grunt-conventional-changelog": "~1.0.0", "load-grunt-tasks": "~0.2.0" }, @@ -39,7 +39,8 @@ "grunt": ">=0.4.0" }, "optionalDependencies": { - "jade": "^1.3.1" + "jade": "^1.3.1", + "pug": "^2.0.0-beta6" }, "keywords": [ "gruntplugin" diff --git a/tasks/html2js.js b/tasks/html2js.js index b60b477..1856b31 100644 --- a/tasks/html2js.js +++ b/tasks/html2js.js @@ -42,6 +42,11 @@ module.exports = function(grunt) { var jadeExtension = /\.jade$/; return jadeExtension.test(filepath); } + + function isPugTemplate(filepath) { + var pugExtension = /\.pug$/; + return pugExtension.test(filepath); + } // return template content var getContent = function(filepath, options) { @@ -50,6 +55,10 @@ module.exports = function(grunt) { var jade = require('jade'); options.jade.filename = filepath; content = jade.render(content, options.jade); + } else if (isPugTemplate(filepath)) { + var pug = require('pug'); + options.pug.filename = filepath; + content = pug.render(content, options.pug); } // Process files as templates if requested. @@ -139,6 +148,7 @@ module.exports = function(grunt) { htmlmin: {}, process: false, jade: { pretty: true }, + pug: { pretty: true }, singleModule: false, existingModule: false, watch: false, diff --git a/test/expected/process_all_pug.js b/test/expected/process_all_pug.js new file mode 100644 index 0000000..3fc6e77 --- /dev/null +++ b/test/expected/process_all_pug.js @@ -0,0 +1,21 @@ +angular.module('templates-process_all_pug', ['../test/fixtures/process_pug.pug', '../test/fixtures/process_pug_custom.pug', '../test/fixtures/process_pug_with_include.pug', '../test/fixtures/pug_include.pug']); + +angular.module("../test/fixtures/process_pug.pug", []).run(["$templateCache", function($templateCache) { + $templateCache.put("../test/fixtures/process_pug.pug", + "

Hello World!

Nice
test
"); +}]); + +angular.module("../test/fixtures/process_pug_custom.pug", []).run(["$templateCache", function($templateCache) { + $templateCache.put("../test/fixtures/process_pug_custom.pug", + "Great"); +}]); + +angular.module("../test/fixtures/process_pug_with_include.pug", []).run(["$templateCache", function($templateCache) { + $templateCache.put("../test/fixtures/process_pug_with_include.pug", + "

I'm an include!

"); +}]); + +angular.module("../test/fixtures/pug_include.pug", []).run(["$templateCache", function($templateCache) { + $templateCache.put("../test/fixtures/pug_include.pug", + "

I'm an include!

"); +}]); diff --git a/test/expected/process_all_pug_after_change.js b/test/expected/process_all_pug_after_change.js new file mode 100644 index 0000000..3fc6e77 --- /dev/null +++ b/test/expected/process_all_pug_after_change.js @@ -0,0 +1,21 @@ +angular.module('templates-process_all_pug', ['../test/fixtures/process_pug.pug', '../test/fixtures/process_pug_custom.pug', '../test/fixtures/process_pug_with_include.pug', '../test/fixtures/pug_include.pug']); + +angular.module("../test/fixtures/process_pug.pug", []).run(["$templateCache", function($templateCache) { + $templateCache.put("../test/fixtures/process_pug.pug", + "

Hello World!

Nice
test
"); +}]); + +angular.module("../test/fixtures/process_pug_custom.pug", []).run(["$templateCache", function($templateCache) { + $templateCache.put("../test/fixtures/process_pug_custom.pug", + "Great"); +}]); + +angular.module("../test/fixtures/process_pug_with_include.pug", []).run(["$templateCache", function($templateCache) { + $templateCache.put("../test/fixtures/process_pug_with_include.pug", + "

I'm an include!

"); +}]); + +angular.module("../test/fixtures/pug_include.pug", []).run(["$templateCache", function($templateCache) { + $templateCache.put("../test/fixtures/pug_include.pug", + "

I'm an include!

"); +}]); diff --git a/test/expected/process_pug.js b/test/expected/process_pug.js new file mode 100644 index 0000000..b766fe9 --- /dev/null +++ b/test/expected/process_pug.js @@ -0,0 +1,7 @@ +angular.module('templates-process_pug', ['../test/fixtures/process_pug.pug']); + +angular.module("../test/fixtures/process_pug.pug", []).run(["$templateCache", function($templateCache) { + $templateCache.put("../test/fixtures/process_pug.pug", + "

Hello World!

\n" + + "
Nice
"); +}]); diff --git a/test/expected/process_pug_custom.js b/test/expected/process_pug_custom.js new file mode 100644 index 0000000..5bb28ae --- /dev/null +++ b/test/expected/process_pug_custom.js @@ -0,0 +1,6 @@ +angular.module('templates-process_pug_custom', ['../test/fixtures/process_pug_custom.pug']); + +angular.module("../test/fixtures/process_pug_custom.pug", []).run(["$templateCache", function($templateCache) { + $templateCache.put("../test/fixtures/process_pug_custom.pug", + "Great"); +}]); diff --git a/test/expected/process_pug_with_include.js b/test/expected/process_pug_with_include.js new file mode 100644 index 0000000..303be55 --- /dev/null +++ b/test/expected/process_pug_with_include.js @@ -0,0 +1,6 @@ +angular.module('templates-process_pug_with_include', ['../test/fixtures/process_pug_with_include.pug']); + +angular.module("../test/fixtures/process_pug_with_include.pug", []).run(["$templateCache", function($templateCache) { + $templateCache.put("../test/fixtures/process_pug_with_include.pug", + "

I'm an include!

"); +}]); diff --git a/test/fixtures/process_pug.pug b/test/fixtures/process_pug.pug new file mode 100644 index 0000000..0d11df6 --- /dev/null +++ b/test/fixtures/process_pug.pug @@ -0,0 +1,2 @@ +p.example Hello World! +#greeting Nice diff --git a/test/fixtures/process_pug_custom.pug b/test/fixtures/process_pug_custom.pug new file mode 100644 index 0000000..064e768 --- /dev/null +++ b/test/fixtures/process_pug_custom.pug @@ -0,0 +1 @@ +a(href) Great diff --git a/test/fixtures/process_pug_with_include.pug b/test/fixtures/process_pug_with_include.pug new file mode 100644 index 0000000..51e68aa --- /dev/null +++ b/test/fixtures/process_pug_with_include.pug @@ -0,0 +1 @@ +include ./pug_include diff --git a/test/fixtures/pug_include.pug b/test/fixtures/pug_include.pug new file mode 100644 index 0000000..ccfc1f3 --- /dev/null +++ b/test/fixtures/pug_include.pug @@ -0,0 +1 @@ +h1 I'm an include! diff --git a/test/html2js_test.js b/test/html2js_test.js index 2d3e661..7970a36 100644 --- a/test/html2js_test.js +++ b/test/html2js_test.js @@ -328,6 +328,57 @@ exports.html2js = { test.done(); }, 1000); + }, + process_pug: function (test) { + test.expect(1); + + assertFileContentsEqual(test, 'tmp/process_pug.js', + 'test/expected/process_pug.js', + 'expected pug template to be processed'); + + test.done(); + }, + process_pug_with_custom_options: function (test) { + test.expect(1); + + assertFileContentsEqual(test, 'tmp/process_pug_custom.js', + 'test/expected/process_pug_custom.js', + 'expected pug template to be processed with custom options'); + + test.done(); + }, + process_pug_with_include: function (test) { + test.expect(1); + assertFileContentsEqual(test, 'tmp/process_pug_with_include.js', + 'test/expected/process_pug_with_include.js', + 'expected pug template to be processed with custom options'); + + test.done(); + }, + process_all_pug: function (test) { + test.expect(1); + + // This test is run with options.watch on + // We need to edit a fixture file to make sure it is watched and re-compiled + var file2Change = 'test/fixtures/process_pug.pug'; + var contents = grunt.file.read(file2Change); + var newContents = contents + "\n#watch test"; + + // Write edited fixture file + grunt.file.write(file2Change, grunt.util.normalizelf(newContents)); + + // wait for the watch-change to process + setTimeout(function () { + // Check re-compiled with changes were added + assertFileContentsEqual(test, 'tmp/process_all_pug.js', + 'test/expected/process_all_pug_after_change.js', + 'expected pug template to be processed with custom options'); + + //reset fixture file to original contents + grunt.file.write(file2Change, grunt.util.normalizelf(contents)); + test.done(); + }, 1500); + }, single_module: function (test) { test.expect(1);