handlebars-precompiler
Advanced tools
Comparing version 1.0.2 to 1.0.4
@@ -5,10 +5,27 @@ // Modified version of https://github.com/wycats/handlebars.js/blob/master/bin/handlebars | ||
var fs = require('fs'), | ||
file = require('file'), | ||
handlebars = exports.handlebars = require('handlebars'), | ||
basename = require('path').basename, | ||
uglify = require('uglify-js'); | ||
uglify = require('uglify-js'), | ||
_ = require('lodash'); | ||
/** | ||
* Compiles all of the Handlebars templates | ||
* | ||
* @param {object} opts Options | ||
* @param {boolean} opts.min Whether or not to minify the files | ||
* @param {RegExp} opts.fileRegex File regular expression to match | ||
* @param {string[]} opts.templates Template directories to compile | ||
* @param {string} opts.output Output file name | ||
* @param {boolean} opts.amd Exports amd style (require.js) | ||
* @param {string} opts.handlebarPath Path to handlebar.js (only valid for amd-style) | ||
* @param {boolean} opts.partial Compiling a partial template | ||
* @param {string} opts.commonjs Exports CommonJS style, path to Handlebars module | ||
*/ | ||
exports.do = function(opts) { | ||
if (!opts.handlebarPath) { | ||
opts.handlebarPath = ''; | ||
} | ||
(function(opts) { | ||
var template = [0]; | ||
if (!opts.templates.length) { | ||
@@ -26,12 +43,4 @@ throw 'Must define at least one template or directory.'; | ||
}); | ||
if (opts.simple && opts.min) { | ||
throw 'Unable to minimze simple output'; | ||
} | ||
if (opts.simple && (opts._.length !== 1 || fs.statSync(opts._[0]).isDirectory())) { | ||
throw 'Unable to output multiple templates in simple mode'; | ||
} | ||
}(opts)); | ||
var template = opts.templates[0]; | ||
// Convert the known list into a hash | ||
@@ -49,5 +58,11 @@ var known = {}; | ||
var output = []; | ||
if (!opts.simple) { | ||
output.push('(function() {\n var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};\n'); | ||
if (opts.amd) { | ||
output.push('define([\'' + opts.handlebarPath + 'handlebars\'], function(Handlebars) {\n'); | ||
} else if (opts.commonjs) { | ||
output.push('var Handlebars = require("' + opts.commonjs + '");'); | ||
} else { | ||
output.push('(function() {\n'); | ||
} | ||
output.push(' var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};\n'); | ||
function processTemplate(template, root) { | ||
@@ -59,4 +74,6 @@ var path = template, | ||
var fileRegex = /\.handlebars$/; | ||
if(opts.fileRegex) fileRegex = opts.fileRegex; | ||
if (opts.fileRegex) { | ||
fileRegex = opts.fileRegex; | ||
} | ||
if (stat.isDirectory()) { | ||
@@ -66,3 +83,3 @@ fs.readdirSync(template).map(function(file) { | ||
if (fileRegex.test(path) || fs.statSync(path).isDirectory()) { | ||
if (path.slice(-1) !== '~' && (fileRegex.test(path) || fs.statSync(path).isDirectory())) { | ||
processTemplate(path, root || template); | ||
@@ -87,7 +104,6 @@ } | ||
if (opts.simple) { | ||
output.push(handlebars.precompile(data, options) + '\n'); | ||
} else { | ||
output.push('templates[\'' + template + '\'] = template(' + handlebars.precompile(data, options) + ');\n'); | ||
if(opts.amd && (opts.templates.length === 1 && !fs.statSync(opts.templates[0]).isDirectory())) { | ||
output.push('return '); | ||
} | ||
output.push('templates[\'' + template + '\'] = template(' + handlebars.precompile(data, options) + ');\n'); | ||
} | ||
@@ -101,3 +117,12 @@ } | ||
// Output the content | ||
if (!opts.simple) { | ||
if (opts.amd) { | ||
if(opts.templates.length > 1 || (opts.templates.length === 1 && fs.statSync(opts.templates[0]).isDirectory())) { | ||
if(opts.partial){ | ||
output.push('return Handlebars.partials;\n'); | ||
} else { | ||
output.push('return templates;\n'); | ||
} | ||
} | ||
output.push('});'); | ||
} else if (!opts.commonjs) { | ||
output.push('})();'); | ||
@@ -119,31 +144,91 @@ } | ||
} | ||
} | ||
}; | ||
/** | ||
* Compiles all of the Handlebars templates in the specified directory and monitors for changes. | ||
* | ||
* @deprecated This function is deprecated in favor of watch(), which allows for more options. | ||
* | ||
* @param {string} dir Directory with Handlebars templates | ||
* @param {string} outfile Output file name | ||
* @param {string[]} extensions An array of extensions (eg 'hbs') of files to compile as Handlebars templates | ||
*/ | ||
exports.watchDir = function(dir, outfile, extensions) { | ||
var fs = require('fs') | ||
, file = require('file'); | ||
exports.watch(dir, outfile, { | ||
extensions: extensions | ||
}); | ||
}; | ||
var regex = /\.handlebars$/; | ||
if(extensions) { | ||
regex = new RegExp('\.' + extensions.join('$|\.') + '$'); | ||
/** | ||
* Compiles all of the Handlebars templates in the specified directory and monitors for changes. | ||
* | ||
* @param {string} dir Directory with Handlebars templates | ||
* @param {string} outfile Output file name | ||
* @param {object} opts Options | ||
* @param {string[]} opts.extensions An array of extensions (eg 'hbs') of files to compile as Handlebars templates (takes precedence over fileRegex) | ||
* @param {RegExp} opts.fileRegex A regular expression of the files to compile as Handlebars templates (instead of using .extensions) | ||
* @param {boolean} opts.min Whether or not to minify the files (default: true) | ||
* @param {boolean} opts.silent Silence console output (default: false) | ||
* @param {boolean} opts.amd Exports amd style (require.js) (default: false) | ||
* @param {string} opts.handlebarPath Path to handlebar.js (only valid for amd-style) (default: '') | ||
* @param {boolean} opts.partial Compiling a partial template (default: false) | ||
* @param {string} opts.commonjs Exports CommonJS style, path to Handlebars module (default: false) | ||
*/ | ||
exports.watch = function(dir, outfile, opts) { | ||
// defaults to send to .do() | ||
var defaults = { | ||
extensions: null, | ||
fileRegex: /\.handlebars$/, | ||
min: true, | ||
silent: false, | ||
amd: false, | ||
handlebarPath: '', | ||
partial: false, | ||
commonjs: false | ||
}; | ||
// merge arguments with defaults into options | ||
var options = {}; | ||
_.merge(options, defaults, opts); | ||
// process passed-in arguments | ||
options.templates = [dir]; | ||
options.output = outfile; | ||
if (options.extensions) { | ||
options.fileRegex = new RegExp('\\.' + options.extensions.join('$|\\.') + '$'); | ||
} | ||
var compileOnChange = function(event, filename) { | ||
console.log('[' + event + '] detected in ' + (filename ? filename : '[filename not supported]')); | ||
console.log('[compiling] ' + outfile); | ||
exports.do({ | ||
templates: [dir], | ||
output: outfile, | ||
fileRegex: regex, | ||
min: true | ||
}); | ||
/** | ||
* Compiles all of the Handlebars templates if one of the files changes. | ||
* | ||
* @private | ||
* @param {Event} event File change event | ||
* @param {string} filename File name that changed | ||
*/ | ||
function compileOnChange(event, filename) { | ||
if (!options.silent){ | ||
console.log('[' + event + '] detected in ' + (filename ? filename : '[filename not supported]')); | ||
console.log('[compiling] ' + outfile); | ||
} | ||
// do a full compile | ||
exports.do(options); | ||
} | ||
// compile everything before we start watching | ||
exports.do(options); | ||
// find all matching files in the base directory and watch all of them for changes | ||
file.walk(dir, function(_, dirPath, dirs, files) { | ||
if(files) { | ||
for(var i = 0; i < files.length; i++) { | ||
if (files) { | ||
for (var i = 0; i < files.length; i++) { | ||
var file = files[i]; | ||
if(regex.test(file)) { | ||
if (options.fileRegex.test(file)) { | ||
// watch this file for changes | ||
fs.watch(file, compileOnChange); | ||
console.log('[watching] ' + file); | ||
if (!options.silent) { | ||
console.log('[watching] ' + file); | ||
} | ||
} | ||
@@ -153,2 +238,2 @@ } | ||
}); | ||
} | ||
}; |
{ | ||
"name": "handlebars-precompiler", | ||
"description": "Handlebars precompiler node module", | ||
"version": "1.0.2", | ||
"homepage": "http://github.com/jwietelmann/node-handlebars-precompiler", | ||
"version": "1.0.4", | ||
"homepage": "http://github.com/nicjansma/node-handlebars-precompiler", | ||
"keywords": [ | ||
@@ -11,3 +11,3 @@ "handlebars mustache template html compiler" | ||
"type": "git", | ||
"url": "git://github.com/jwietelmann/node-handlebars-precompiler.git" | ||
"url": "git://github.com/nicjansma/node-handlebars-precompiler.git" | ||
}, | ||
@@ -20,6 +20,14 @@ "engines": { | ||
"uglify-js": "~1.2", | ||
"file": "~0.2" | ||
"file": "~0.2", | ||
"lodash": ">= 0.0.0" | ||
}, | ||
"devDependencies": {}, | ||
"devDependencies": { | ||
"grunt": ">= 0.0.0", | ||
"grunt-contrib-nodeunit": ">= 0.0.0", | ||
"grunt-contrib-jshint": ">= 0.0.0" | ||
}, | ||
"scripts": { | ||
"test": "grunt travis --verbose" | ||
}, | ||
"main": "handlebars-precompiler.js" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
14074
252
110
1
4
3
2
2
+ Addedlodash@>= 0.0.0
+ Addedlodash@4.17.21(transitive)