Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const log = require('fancy-log');

module.exports = function gulpPug(options) {
const opts = Object.assign({}, options);
const namefunc = typeof opts.name === 'function' ? opts.name : undefined;
const pug = opts.pug || opts.jade || defaultPug;

opts.data = Object.assign(opts.data || {}, opts.locals || {});
Expand All @@ -30,6 +31,9 @@ module.exports = function gulpPug(options) {
log('compiling file', file.path);
}
if (opts.client) {
if (typeof namefunc === 'function') {
opts.name = namefunc(file);
}
compiled = pug.compileClient(contents, opts);
} else {
compiled = pug.compile(contents, opts)(data);
Expand Down
27 changes: 27 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,33 @@ test('should compile my pug files into JS', function(t) {
}));
});

test('should replace the template name with function result', function(t) {
const filename2 = path.join(__dirname, './fixtures/helloworld2.pug');
let finishedFileCount = 0;
gulp.src([filename, filename2])
.pipe(task({
client: true,
name: function(file) {
if (!file || !file.path) {
return 'template';
}
return '__' + path.basename(file.path, path.extname(file.path)) + '__';
},
}))
.pipe(through.obj(function(file, enc, cb) {
t.ok(file.contents instanceof Buffer);
let expected = 'function __'
+ path.basename(file.path, path.extname(file.path))
+ '__(';
t.ok(String(file.contents).indexOf(expected) >= 0);
if (++finishedFileCount === 2) {
t.end();
}
cb();
}));
});


test('should always return contents as buffer with client = true', function(t) {
gulp.src(filename)
.pipe(task({
Expand Down