From 121d9d9ed089fee19c031ba26668fd4fc470f8ca Mon Sep 17 00:00:00 2001 From: Magnus Petersen-Paaske Date: Tue, 2 May 2017 03:18:43 +0200 Subject: [PATCH 1/5] Added a process name and namespace option, allowing the templates to be named (inspired by how grunt-contrib-pug handles it) --- index.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/index.js b/index.js index 8fbd5cd..575c669 100644 --- a/index.js +++ b/index.js @@ -7,12 +7,45 @@ var ext = require('gulp-util').replaceExtension; var PluginError = require('gulp-util').PluginError; var log = require('gulp-util').log; +// A function to make the namespace client templates are compiled to +const getNamespaceDeclaration = (ns) => { + const output = []; + let curPath = 'this'; + + if (ns !== 'this') { + var nsParts = ns.split('.'); + nsParts.forEach((curPart) => { + if (curPart !== 'this') { + curPath += '[' + JSON.stringify(curPart) + ']'; + output.push(curPath + ' = ' + curPath + ' || {};'); + } + }) + } + + return { + namespace: curPath, + declaration: output.join('\n') + }; +} + module.exports = function gulpPug(options) { var opts = objectAssign({}, options); var pug = opts.pug || opts.jade || defaultPug; opts.data = objectAssign(opts.data || {}, opts.locals || {}); + // filename conversion for templates + var defaultProcessName = function(name) { + return name.replace('.pug', ''); + }; + + var processName = options.processName || defaultProcessName; + + let nsInfo; + if (options.namespace) { + nsInfo = getNamespaceDeclaration(options.namespace) + } + return through.obj(function compilePug(file, enc, cb) { var data = objectAssign({}, opts.data, file.data || {}); @@ -27,11 +60,19 @@ module.exports = function gulpPug(options) { try { var compiled; var contents = String(file.contents); + + let localFilePath = file.path.replace(process.cwd() + '/', ''); + let filename = processName(localFilePath); + if (opts.verbose === true) { log('compiling file', file.path); } if (opts.client) { compiled = pug.compileClient(contents, opts); + + if (options.namespace) { + compiled = nsInfo.namespace + '['+JSON.stringify(filename)+'] = '+ compiled + ']'; + } } else { compiled = pug.compile(contents, opts)(data); } From a6ad84cf5ab6e7a1ece07f7e6cc37d179cebc0af Mon Sep 17 00:00:00 2001 From: Magnus Petersen-Paaske Date: Tue, 2 May 2017 03:29:15 +0200 Subject: [PATCH 2/5] Updated readme with info on processname and namespace options --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9f5f841..0abac50 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,9 @@ gulp.task('views', function buildHTML() { - `opts.client` (`Boolean`): Compile Pug to JavaScript code. - `opts.pug`: A custom instance of Pug for `gulp-pug` to use. - `opts.verbose`: display name of file from stream that is being compiled. - + - `opts.namespace`: sets a namespace that's used for accessing the templates on client + - `opts.processname`: a function that takes the filename for the template and creates a unique function name to access the template when compiling to the client (the template function will be on the namespace defined with `opts.namespace`) + To change `opts.filename` use [`gulp-rename`][gulp-rename] before `gulp-pug`. Returns a stream that compiles Vinyl files as Pug. From d23fd50ed06912a3e500e4aab54b19f16b2a29e8 Mon Sep 17 00:00:00 2001 From: Magnus Petersen-Paaske Date: Tue, 2 May 2017 03:43:43 +0200 Subject: [PATCH 3/5] Slight update to make it pass tests --- index.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 575c669..09958c4 100644 --- a/index.js +++ b/index.js @@ -8,18 +8,18 @@ var PluginError = require('gulp-util').PluginError; var log = require('gulp-util').log; // A function to make the namespace client templates are compiled to -const getNamespaceDeclaration = (ns) => { +function getNamespaceDeclaration(ns) { const output = []; let curPath = 'this'; if (ns !== 'this') { var nsParts = ns.split('.'); - nsParts.forEach((curPart) => { + nsParts.forEach(function(curPart) { if (curPart !== 'this') { curPath += '[' + JSON.stringify(curPart) + ']'; output.push(curPath + ' = ' + curPath + ' || {};'); } - }) + }); } return { @@ -39,11 +39,11 @@ module.exports = function gulpPug(options) { return name.replace('.pug', ''); }; - var processName = options.processName || defaultProcessName; + var processName = opts.processName || defaultProcessName; let nsInfo; - if (options.namespace) { - nsInfo = getNamespaceDeclaration(options.namespace) + if (opts.namespace) { + nsInfo = getNamespaceDeclaration(opts.namespace); } return through.obj(function compilePug(file, enc, cb) { @@ -70,8 +70,9 @@ module.exports = function gulpPug(options) { if (opts.client) { compiled = pug.compileClient(contents, opts); - if (options.namespace) { - compiled = nsInfo.namespace + '['+JSON.stringify(filename)+'] = '+ compiled + ']'; + if (opts.namespace) { + compiled = nsInfo.namespace + '[' + JSON.stringify(filename) + + '] = ' + compiled + ']'; } } else { compiled = pug.compile(contents, opts)(data); From 7053819f71e3805f857394db0e9d5cc1f4035f03 Mon Sep 17 00:00:00 2001 From: Magnus Petersen-Paaske Date: Thu, 4 May 2017 02:05:05 +0200 Subject: [PATCH 4/5] Fixed a typo --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 09958c4..42908c3 100644 --- a/index.js +++ b/index.js @@ -72,7 +72,7 @@ module.exports = function gulpPug(options) { if (opts.namespace) { compiled = nsInfo.namespace + '[' + JSON.stringify(filename) + - '] = ' + compiled + ']'; + '] = ' + compiled; } } else { compiled = pug.compile(contents, opts)(data); From 7832fec4cf6149d055d8ea330dc84dca6da173f7 Mon Sep 17 00:00:00 2001 From: Magnus Petersen-Paaske Date: Tue, 16 May 2017 11:07:22 +0200 Subject: [PATCH 5/5] Replaced 'const' and 'let' with 'var' to make package pre ES6 compatible --- index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 42908c3..7276b22 100644 --- a/index.js +++ b/index.js @@ -9,8 +9,8 @@ var log = require('gulp-util').log; // A function to make the namespace client templates are compiled to function getNamespaceDeclaration(ns) { - const output = []; - let curPath = 'this'; + var output = []; + var curPath = 'this'; if (ns !== 'this') { var nsParts = ns.split('.'); @@ -41,7 +41,7 @@ module.exports = function gulpPug(options) { var processName = opts.processName || defaultProcessName; - let nsInfo; + var nsInfo; if (opts.namespace) { nsInfo = getNamespaceDeclaration(opts.namespace); } @@ -61,8 +61,8 @@ module.exports = function gulpPug(options) { var compiled; var contents = String(file.contents); - let localFilePath = file.path.replace(process.cwd() + '/', ''); - let filename = processName(localFilePath); + var localFilePath = file.path.replace(process.cwd() + '/', ''); + var filename = processName(localFilePath); if (opts.verbose === true) { log('compiling file', file.path);