Skip to content
This repository was archived by the owner on May 24, 2024. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var minifyDefaults = {
collapseWhitespace: true
};

function compile(id, str) {
function compileToObject(id, str) {
var minified = minify(str, minifyDefaults);

var template = twig({
Expand All @@ -22,16 +22,21 @@ function compile(id, str) {
var tokens = JSON.stringify(template.tokens);

// the id will be the filename and path relative to the require()ing module
return 'twig({ id: __filename, path: __dirname, data:' + tokens + ', precompiled: true, allowInlineIncludes: true })';
return '{ id: __filename, path: __dirname, data:' + tokens + ', precompiled: true, allowInlineIncludes: true }';
}

function process(source) {
function process(source, opts) {
return (
'var twig = require(\'twig\').twig;\n' +
'module.exports = ' + source + ';\n'
'var transform = ' + (opts.transform || 'function(x) { return x; }') + ';\n' +
'module.exports = twig(transform(' + source + '));\n'
);
}

function compile(id, str) {
return 'twig(' + compileToObject(id, str) + ')';
}

function twigify(file, opts) {
if (!ext.test(file)) return through();
if (!opts) opts = {};
Expand All @@ -52,12 +57,12 @@ function twigify(file, opts) {
var compiledTwig;

try {
compiledTwig = compile(id, str);
compiledTwig = compileToObject(id, str);
} catch(e) {
return this.emit('error', e);
}

this.push(process(compiledTwig));
this.push(process(compiledTwig, opts));
next();
}

Expand Down