From 88c39015f07283f3aad938aee3e0ce4f35823bd3 Mon Sep 17 00:00:00 2001 From: karl wiggisser Date: Thu, 9 May 2019 13:49:30 +0200 Subject: [PATCH 1/4] accept a function as options.name --- index.js | 4 ++++ test/test.js | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/index.js b/index.js index c4b5542..7150739 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 = opts.name; const pug = opts.pug || opts.jade || defaultPug; opts.data = Object.assign(opts.data || {}, opts.locals || {}); @@ -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); + } file.path = ext(file.path, opts.client ? '.js' : '.html'); if (file.isStream()) { diff --git a/test/test.js b/test/test.js index 17158d9..19543bc 100644 --- a/test/test.js +++ b/test/test.js @@ -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({ From a60b499cb0c16834fd9bae32b4351506c594704e Mon Sep 17 00:00:00 2001 From: karl wiggisser Date: Thu, 9 May 2019 16:07:08 +0200 Subject: [PATCH 2/4] adapt to PR comments --- index.js | 7 +++---- test/test.js | 6 ++++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 7150739..05b6c12 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,6 @@ const log = require('fancy-log'); module.exports = function gulpPug(options) { const opts = Object.assign({}, options); - const namefunc = opts.name; const pug = opts.pug || opts.jade || defaultPug; opts.data = Object.assign(opts.data || {}, opts.locals || {}); @@ -17,9 +16,6 @@ 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); - } file.path = ext(file.path, opts.client ? '.js' : '.html'); if (file.isStream()) { @@ -34,6 +30,9 @@ module.exports = function gulpPug(options) { log('compiling file', file.path); } if (opts.client) { + if (typeof options.name === 'function') { + opts.name = options.name(file); + } compiled = pug.compileClient(contents, opts); } else { compiled = pug.compile(contents, opts)(data); diff --git a/test/test.js b/test/test.js index 19543bc..e2a7b3f 100644 --- a/test/test.js +++ b/test/test.js @@ -180,12 +180,14 @@ test('should replace the template name with function result', function(t) { if (!file || !file.path) { return 'template'; } - return '__' + path.basename(file.path, '.pug') + '__'; + 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, '.js') + '__('; + let expected = 'function __' + + path.basename(file.path, path.extname(file.path)) + + '__('; t.ok(String(file.contents).indexOf(expected) >= 0); if (++finishedFileCount === 2) { t.end(); From 1059c81dbd1be3d3f41f38c1d935b40cb3d7a3a1 Mon Sep 17 00:00:00 2001 From: karl wiggisser Date: Thu, 9 May 2019 16:17:51 +0200 Subject: [PATCH 3/4] remove trailing space --- test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.js b/test/test.js index e2a7b3f..6be2b68 100644 --- a/test/test.js +++ b/test/test.js @@ -186,7 +186,7 @@ test('should replace the template name with function result', function(t) { .pipe(through.obj(function(file, enc, cb) { t.ok(file.contents instanceof Buffer); let expected = 'function __' - + path.basename(file.path, path.extname(file.path)) + + path.basename(file.path, path.extname(file.path)) + '__('; t.ok(String(file.contents).indexOf(expected) >= 0); if (++finishedFileCount === 2) { From 893bb5ed24a994b8ff796e55d3d476288f81df67 Mon Sep 17 00:00:00 2001 From: karl wiggisser Date: Thu, 9 May 2019 17:27:54 +0200 Subject: [PATCH 4/4] avoid potential undefined options object --- index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 05b6c12..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,8 +31,8 @@ module.exports = function gulpPug(options) { log('compiling file', file.path); } if (opts.client) { - if (typeof options.name === 'function') { - opts.name = options.name(file); + if (typeof namefunc === 'function') { + opts.name = namefunc(file); } compiled = pug.compileClient(contents, opts); } else {