Skip to content

Commit

Permalink
rquadling#83 Add pug rendering capabilities and unit tests (major rel…
Browse files Browse the repository at this point in the history
…ease package.json update reflects the addition). Alphabetized dependency list and added pugg as an optional dependency.
  • Loading branch information
Benjamin Norwood committed Nov 9, 2016
1 parent 1add8fa commit c094ad2
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 4 deletions.
30 changes: 30 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions 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.3.6",
"version": "1.0.0",
"homepage": "https://github.com/karlgoldstein/grunt-html2js",
"author": {
"name": "Karl Goldstein",
Expand All @@ -28,18 +28,19 @@
"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"
},
"peerDependencies": {
"grunt": ">=0.4.0"
},
"optionalDependencies": {
"jade": "^1.3.1"
"jade": "^1.3.1",
"pug": "^2.0.0-beta6"
},
"keywords": [
"gruntplugin"
Expand Down
10 changes: 10 additions & 0 deletions tasks/html2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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.
Expand Down Expand Up @@ -139,6 +148,7 @@ module.exports = function(grunt) {
htmlmin: {},
process: false,
jade: { pretty: true },
pug: { pretty: true },
singleModule: false,
existingModule: false,
watch: false,
Expand Down
21 changes: 21 additions & 0 deletions test/expected/process_all_pug.js
Original file line number Diff line number Diff line change
@@ -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",
"<p class=\"example\">Hello World!</p><div id=\"greeting\">Nice</div><div id=\"watch\">test</div>");
}]);

angular.module("../test/fixtures/process_pug_custom.pug", []).run(["$templateCache", function($templateCache) {
$templateCache.put("../test/fixtures/process_pug_custom.pug",
"<a href=\"href\">Great</a>");
}]);

angular.module("../test/fixtures/process_pug_with_include.pug", []).run(["$templateCache", function($templateCache) {
$templateCache.put("../test/fixtures/process_pug_with_include.pug",
"<h1>I'm an include!</h1>");
}]);

angular.module("../test/fixtures/pug_include.pug", []).run(["$templateCache", function($templateCache) {
$templateCache.put("../test/fixtures/pug_include.pug",
"<h1>I'm an include!</h1>");
}]);
21 changes: 21 additions & 0 deletions test/expected/process_all_pug_after_change.js
Original file line number Diff line number Diff line change
@@ -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",
"<p class=\"example\">Hello World!</p><div id=\"greeting\">Nice</div><div id=\"watch\">test</div>");
}]);

angular.module("../test/fixtures/process_pug_custom.pug", []).run(["$templateCache", function($templateCache) {
$templateCache.put("../test/fixtures/process_pug_custom.pug",
"<a href=\"href\">Great</a>");
}]);

angular.module("../test/fixtures/process_pug_with_include.pug", []).run(["$templateCache", function($templateCache) {
$templateCache.put("../test/fixtures/process_pug_with_include.pug",
"<h1>I'm an include!</h1>");
}]);

angular.module("../test/fixtures/pug_include.pug", []).run(["$templateCache", function($templateCache) {
$templateCache.put("../test/fixtures/pug_include.pug",
"<h1>I'm an include!</h1>");
}]);
7 changes: 7 additions & 0 deletions test/expected/process_pug.js
Original file line number Diff line number Diff line change
@@ -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",
"<p class=\"example\">Hello World!</p>\n" +
"<div id=\"greeting\">Nice</div>");
}]);
6 changes: 6 additions & 0 deletions test/expected/process_pug_custom.js
Original file line number Diff line number Diff line change
@@ -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",
"<a href>Great</a>");
}]);
6 changes: 6 additions & 0 deletions test/expected/process_pug_with_include.js
Original file line number Diff line number Diff line change
@@ -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",
"<h1>I'm an include!</h1>");
}]);
2 changes: 2 additions & 0 deletions test/fixtures/process_pug.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
p.example Hello World!
#greeting Nice
1 change: 1 addition & 0 deletions test/fixtures/process_pug_custom.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a(href) Great
1 change: 1 addition & 0 deletions test/fixtures/process_pug_with_include.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ./pug_include
1 change: 1 addition & 0 deletions test/fixtures/pug_include.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
h1 I'm an include!
51 changes: 51 additions & 0 deletions test/html2js_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c094ad2

Please sign in to comment.