diff --git a/index.js b/index.js index c4b5542..7145c97 100644 --- a/index.js +++ b/index.js @@ -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 || {}); @@ -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); diff --git a/test/test.js b/test/test.js index 17158d9..6be2b68 100644 --- a/test/test.js +++ b/test/test.js @@ -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({