From 863d8ef44be3fc644d0e4af6d6caf424220efd7b Mon Sep 17 00:00:00 2001 From: karl wiggisser Date: Thu, 9 May 2019 13:49:30 +0200 Subject: [PATCH 1/5] accept a function as options.name --- index.js | 4 ++++ test/test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/index.js b/index.js index 8280222..a6c29bf 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ const vinylContents = require('vinyl-contents'); 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,6 +18,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'); vinylContents(file, function onContents(err, contents) { diff --git a/test/test.js b/test/test.js index f490de2..9a24553 100644 --- a/test/test.js +++ b/test/test.js @@ -153,6 +153,32 @@ describe('test', function () { ); }); + it('should name the compiled JS function using name option', function (done) { + function name(file) { + if (!file || !file.name) { + return 'template'; + } + return '__' + path.basename(file.path, '.pug') + '__'; + } + + const templateFilename = 'helloworld.pug'; + + function assert(files) { + expect(files.length).toEqual(1); + const newFileContent = files[0].contents.toString(); + expect(newFileContent).toContain("function " + name(templateFilename) + "("); + } + + pipe( + [ + from.obj([getFixture(templateFilename)]), + task({ client: true, name }), + concat(assert), + ], + done + ); + }); + it('should always return contents as buf with client = true', function (done) { function assert(files) { expect(files.length).toEqual(1); From 36cc6f258fe661684b613f855419030d08dd44aa Mon Sep 17 00:00:00 2001 From: karl wiggisser Date: Thu, 9 May 2019 16:07:08 +0200 Subject: [PATCH 2/5] adapt to PR comments --- index.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index a6c29bf..092be51 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,6 @@ const vinylContents = require('vinyl-contents'); 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 || {}); @@ -18,9 +17,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'); vinylContents(file, function onContents(err, contents) { @@ -41,6 +37,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); From 527b6c997a631dd1b62fd4e8e6be105972dedf02 Mon Sep 17 00:00:00 2001 From: karl wiggisser Date: Thu, 9 May 2019 17:27:54 +0200 Subject: [PATCH 3/5] avoid potential undefined options object --- index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 092be51..e8e1995 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ const vinylContents = require('vinyl-contents'); 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 || {}); @@ -37,8 +38,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 { From f1bc5f3f6e4601f1d5f1d6ce348a6b846dfdd102 Mon Sep 17 00:00:00 2001 From: Vladyslav Zagrevskiy Date: Mon, 12 Jun 2023 15:37:48 +0300 Subject: [PATCH 4/5] Modify type definitions for name option --- index.d.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/index.d.ts b/index.d.ts index 3527a5e..1f04b30 100644 --- a/index.d.ts +++ b/index.d.ts @@ -27,6 +27,12 @@ declare namespace GulpPug { */ client?: boolean; + /** + * If passed as a string, used as the name of your client side template function. If passed as a function, + * it is called with a pug template file as an argument, to obtain a name of client side template function. + */ + name?: string | ((file: VinylFile) => string); + /** * A custom instance of Pug for `gulp-pug` to use. */ From 7aa8caa46fa2795afa2f80d07683e580da9d4034 Mon Sep 17 00:00:00 2001 From: Vladyslav Zagrevskiy Date: Mon, 12 Jun 2023 15:44:48 +0300 Subject: [PATCH 5/5] Document name argument in README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6050f13..6c893c1 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ exports.views = () => { - `opts.locals` (`Object`): Locals to compile the Pug with. You can also provide locals through the `data` field of the file object, e.g. with [`gulp-data`][gulp-data]. They will be merged with `opts.locals`. - `opts.data` (`Object`): Same as `opts.locals`. - `opts.client` (`Boolean`): Compile Pug to JavaScript code. +- `opts.name` (`string | ((file: VinylFile) => string)`): The name of your client side template function, when `opts.client` is set to `True`. When passed as a function, takes current Pug file as an argument. - `opts.pug`: A custom instance of Pug for `gulp-pug` to use. - `opts.verbose`: display name of file from stream that is being compiled.