Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept a function as options.name #199

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ module.exports = function gulpPug(options) {
log('compiling file', file.path);
}
if (opts.client) {
if (typeof options.name === 'function') {
demurgos marked this conversation as resolved.
Show resolved Hide resolved
opts.name = options.name(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