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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 = opts.name;
demurgos marked this conversation as resolved.
Show resolved Hide resolved
const pug = opts.pug || opts.jade || defaultPug;

opts.data = Object.assign(opts.data || {}, opts.locals || {});
Expand All @@ -16,6 +17,9 @@ module.exports = function gulpPug(options) {
const data = Object.assign({}, opts.data, file.data || {});

opts.filename = file.path;
if (typeof namefunc === 'function') {
opts.name = namefunc(file);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should move this into the opts.client conditional because it is only used for client compilation. We might as well avoid extra loops when it doesn't effect the output.

}
file.path = ext(file.path, opts.client ? '.js' : '.html');

if (file.isStream()) {
Expand Down
25 changes: 25 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,31 @@ 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, '.pug') + '__';
},
}))
.pipe(through.obj(function(file, enc, cb) {
t.ok(file.contents instanceof Buffer);
let expected = 'function __' + path.basename(file.path, '.js') + '__(';
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