Skip to content

Commit 7dd0778

Browse files
committed
cleanup, remove extra closures, do not mutate options and add test for filters
1 parent 2889555 commit 7dd0778

File tree

4 files changed

+59
-19
lines changed

4 files changed

+59
-19
lines changed

index.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
'use strict';
22

3+
var jade = require('jade');
4+
var extend = require('xtend');
35
var through = require('through2');
46
var ext = require('gulp-util').replaceExtension;
57
var PluginError = require('gulp-util').PluginError;
68

9+
function handleCompile(contents, opts){
10+
if(opts.client){
11+
return opts.compileClient(contents, opts);
12+
}
713

8-
module.exports = function(options){
9-
var opts = options || {};
10-
var jade = opts.jade || require('jade');
11-
var compile = jade.compile;
12-
var compileClient = jade.compileClient;
13-
14-
function handleCompile(contents){
15-
if(opts.client){
16-
return compileClient(contents, opts);
17-
}
14+
return opts.compile(contents, opts)(opts.locals || opts.data);
15+
}
1816

19-
return compile(contents, opts)(opts.locals || opts.data);
17+
function handleExtension(filepath, opts){
18+
if(opts.client){
19+
return ext(filepath, '.js');
2020
}
21+
return ext(filepath, '.html');
22+
}
2123

22-
function handleExtension(filepath){
23-
if(opts.client){
24-
return ext(filepath, '.js');
25-
}
26-
return ext(filepath, '.html');
27-
}
24+
module.exports = function(options){
25+
options = options || {};
26+
var opts = extend(options, {
27+
compile: (options.jade || jade).compile,
28+
compileClient: (options.jade || jade).compileClient
29+
});
2830

2931
function CompileJade(file, enc, cb){
3032
opts.filename = file.path;
@@ -33,7 +35,7 @@ module.exports = function(options){
3335
opts.data = file.data;
3436
}
3537

36-
file.path = handleExtension(file.path);
38+
file.path = handleExtension(file.path, opts);
3739

3840
if(file.isStream()){
3941
return cb(new PluginError('gulp-jade', 'Streaming not supported'));

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"dependencies": {
1414
"gulp-util": "~2.2",
1515
"jade": "1.1 - 1.7",
16-
"through2": "^0.5.1"
16+
"through2": "^0.5.1",
17+
"xtend": "^4.0.0"
1718
},
1819
"main": "index.js",
1920
"engines": {

test/filters.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
var test = require('tap').test;
4+
5+
var task = require('../');
6+
var path = require('path');
7+
var fs = require('fs');
8+
var gutil = require('gulp-util');
9+
var jade = require('jade');
10+
11+
jade.filters.shout = function(str){
12+
return str.toUpperCase() + '!!!!';
13+
};
14+
15+
var filePath = path.join(__dirname, 'fixtures', 'filters.jade');
16+
var base = path.join(__dirname, 'fixtures');
17+
var cwd = __dirname;
18+
19+
var file = new gutil.File({
20+
path: filePath,
21+
base: base,
22+
cwd: cwd,
23+
contents: fs.readFileSync(filePath)
24+
});
25+
26+
test('should compile a jade template with a custom jade instance with filters', function(t){
27+
var stream = task();
28+
stream.on('data', function(newFile){
29+
t.ok(newFile);
30+
t.ok(newFile.contents);
31+
t.equal(newFile.contents.toString(), 'HELLO, TESTER!!!!');
32+
t.end();
33+
});
34+
stream.write(file);
35+
});

test/fixtures/filters.jade

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:shout
2+
hello, tester

0 commit comments

Comments
 (0)