Comparing version 0.0.1 to 1.0.0
129
index.js
@@ -1,14 +0,119 @@ | ||
var through = require('through2') | ||
var combyne = require('combyne') | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var through = require('through2'); | ||
var combyne = require('combyne'); | ||
var visitCombyne = require('visit-combyne'); | ||
var extendsCache = {}; | ||
var extensions = { | ||
html: 1, | ||
cmb: 1, | ||
cmbn: 1, | ||
combyne: 1, | ||
} | ||
}; | ||
var processTemplate = function(templateSource, settings, callback) { | ||
var root = settings.root; | ||
var template = combyne(templateSource); | ||
// Find all extend. | ||
var extend = visitCombyne(template.tree.nodes, function(node) { | ||
return node.type === 'ExtendExpression'; | ||
}).map(function(node) { return node.value; }); | ||
// Find all partials. | ||
var partials = visitCombyne(template.tree.nodes, function(node) { | ||
return node.type === 'PartialExpression' && !extendsCache[node.value]; | ||
}).map(function(node) { return node.value; }); | ||
// Find all filters. | ||
var filters = visitCombyne(template.tree.nodes, function(node) { | ||
return node.filters && node.filters.length; | ||
}).map(function(node) { | ||
return node.filters.map(function(filter) { | ||
return filter.value; | ||
}).join(' '); | ||
}); | ||
// Flatten the array. | ||
if (filters.length) { | ||
filters = filters.join(' ').split(' '); | ||
} | ||
// Filters cannot be so easily inferred location-wise, so assume they are | ||
// preconfigured or exist in a filters directory. | ||
var filtersDir = settings.filtersDir || 'filters'; | ||
filters.forEach(function(filterName) { | ||
var filtersPath = path.join(root, filtersDir, filterName); | ||
var filter = require(filtersPath); | ||
// Register the exported function. | ||
template.registerFilter(filterName, filter); | ||
}); | ||
// Map all partials to functions. | ||
partials.forEach(function(name) { | ||
var source = fs.readFileSync(path.join(root, name + '.html')).toString(); | ||
// The last argument of this call is the noparse option that | ||
// specifies the virtual partial should not be loaded. | ||
processTemplate(source, settings, function(render) { | ||
template.registerPartial(name, render); | ||
}); | ||
}); | ||
// Map all extend to functions. | ||
extend.forEach(function(render) { | ||
var name = render.template; | ||
// Pre-cache this template. | ||
extendsCache[render.partial] = true; | ||
// The last argument of this call is the noparse option that | ||
// specifies the virtual partial should not be loaded. | ||
var source = fs.readFileSync(path.join(root, name + '.html')).toString(); | ||
processTemplate(source, settings, function(superTemplate) { | ||
superTemplate.registerPartial(render.partial, template); | ||
template.registerPartial(name, superTemplate); | ||
}); | ||
}); | ||
// Augment the template source to include dependencies. | ||
var lines = template.source.split('\n'); | ||
partials = Object.keys(template._partials).map(function(name) { | ||
return '"' + name + '":' + template._partials[name].source; | ||
}); | ||
filters = Object.keys(template._filters).map(function(name) { | ||
return '"' + name + '":' + String(template._filters[name]).split('\n').join(''); | ||
}); | ||
lines[1] = '_partials: {' + partials.join(',') + '},'; | ||
lines[2] = '_filters: {' + filters.join(',') + '},'; | ||
// Flatten the template to remove unnecessary `\n` whitespace. | ||
template.source = lines.join('\n'); | ||
if (this.push) { | ||
this.push('module.exports = ' + template.source); | ||
} | ||
callback.call(this, template); | ||
}; | ||
function combynify(file) { | ||
if (!extensions[file.split(".").pop()]) return through() | ||
if (!extensions[file.split('.').pop()]) return through(); | ||
var chunks = [] | ||
var settings = {}; | ||
// Mimic how the actual Combyne stores. | ||
settings._filters = {}; | ||
settings._partials = {}; | ||
settings.root = settings.root || path.join(process.cwd(), 'views'); | ||
var chunks = []; | ||
function parts(chunk, enc, callback) { | ||
@@ -18,11 +123,9 @@ chunks.push(chunk); callback(); | ||
function finish(callback) { | ||
var template = combyne(chunks.join('')) | ||
this.push('module.exports = ' + template.source) | ||
callback() | ||
} | ||
return through(parts, finish) | ||
return through(parts, function(callback) { | ||
processTemplate.call(this, chunks.join(''), settings, function(template) { | ||
callback(); | ||
}); | ||
}); | ||
} | ||
module.exports = combynify | ||
module.exports = combynify; |
{ | ||
"name": "combynify", | ||
"version": "0.0.1", | ||
"version": "1.0.0", | ||
"description": "combyne precompiler for browserify", | ||
@@ -12,4 +12,5 @@ "main": "index.js", | ||
"dependencies": { | ||
"combyne": "^0.3.5", | ||
"through2": "^0.4.2" | ||
"combyne": "^0.7.0", | ||
"through2": "^0.4.2", | ||
"visit-combyne": "^1.0.1" | ||
}, | ||
@@ -16,0 +17,0 @@ "repository": { |
@@ -5,2 +5,8 @@ # combynify | ||
# features | ||
- Optimizes templates into dependency-free isolated objects | ||
- Recursively bundles all referenced filters, partials, and template | ||
inheritance | ||
# usage | ||
@@ -7,0 +13,0 @@ |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
6573
101
2
44
3
2
1
+ Addedvisit-combyne@^1.0.1
+ Addedcombyne@0.7.1(transitive)
+ Addedvisit-combyne@1.0.3(transitive)
- Removedcombyne@0.3.9(transitive)
Updatedcombyne@^0.7.0