Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ module.exports = function(content) {

// Build the module export, optionally with template imports
if (withImports) {
source = 'module.exports = Function(_.keys(_.templateSettings.imports), \'return \' + ' + source + '.toString()).apply(undefined, _.values(_.templateSettings.imports));\n';
source = 'module.exports = Function([\'_\'].concat(_.keys(_.templateSettings.imports)), \'return \' + ' + source + '.toString()).apply(undefined, [_].concat(_.values(_.templateSettings.imports)));\n';
} else {
source = 'module.exports = ' + source + ';\n';
}
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
"chai": "^3.2.0",
"chai-string": "^1.1.2",
"istanbul": "^0.3.18",
"lodash": "^4.16.2",
"mocha": "^2.2.5",
"require-from-string": "^1.2.0",
"underscore": "^1.8.3"
},
"scripts": {
Expand Down
51 changes: 50 additions & 1 deletion test/loaderTest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
var underscore = require('underscore');
var lodash = require('lodash');
var requireFromString = require('require-from-string');
var fs = require('fs');
var path = require('path');
var chai = require('chai');
Expand Down Expand Up @@ -167,7 +170,53 @@ describe('loader', function () {
done();
});
});


it('should include the required engine as `_` when withImports is true', function (done) {
underscore.ENGINE_NAME = 'UNDERSCORE';
lodash.ENGINE_NAME = 'LODASH';
_ = lodash;

testTemplate(loader, 'output-engine-name.html', {
query: {
engine: 'underscore',
withImports: true
}
}, function (output) {
var outputEngineName = requireFromString(output, 'output-engine-name');

assert.equal('UNDERSCORE', outputEngineName());

delete _;
delete underscore.ENGINE_NAME;
delete lodash.ENGINE_NAME;

done();
});
});

it('should include the required engine as `_` when withImports is false', function (done) {
underscore.ENGINE_NAME = 'UNDERSCORE';
lodash.ENGINE_NAME = 'LODASH';
_ = lodash;

testTemplate(loader, 'output-engine-name.html', {
query: {
engine: 'underscore',
withImports: false
}
}, function (output) {
var outputEngineName = requireFromString(output, 'output-engine-name');

assert.equal('UNDERSCORE', outputEngineName());

delete _;
delete underscore.ENGINE_NAME;
delete lodash.ENGINE_NAME;

done();
});
});

// FIXME: Changing the underscore tags changes it globally
it('should allow custom underscore tags', function (done) {
testTemplate(loader, 'custom-tags.html', {
Expand Down
1 change: 1 addition & 0 deletions test/templates/output-engine-name.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%-_.ENGINE_NAME%>
4 changes: 2 additions & 2 deletions test/templates/output/simple-lodash.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var _ = require('lodash');
module.exports = Function(_.keys(_.templateSettings.imports), 'return ' + function(obj){
module.exports = Function(['_'].concat(_.keys(_.templateSettings.imports)), 'return ' + function(obj){
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
with(obj||{}){
__p+='<h1>'+
Expand All @@ -13,4 +13,4 @@ __p+='\n<p>'+
__p+='\n';
}
return __p;
}.toString()).apply(undefined, _.values(_.templateSettings.imports));
}.toString()).apply(undefined, [_].concat(_.values(_.templateSettings.imports)));
4 changes: 2 additions & 2 deletions test/templates/output/simple-with-imports.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = Function(_.keys(_.templateSettings.imports), 'return ' + function(obj){
module.exports = Function(['_'].concat(_.keys(_.templateSettings.imports)), 'return ' + function(obj){
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
with(obj||{}){
__p+='<h1>'+
Expand All @@ -12,4 +12,4 @@ __p+='\n<p>'+
__p+='\n';
}
return __p;
}.toString()).apply(undefined, _.values(_.templateSettings.imports));
}.toString()).apply(undefined, [_].concat(_.values(_.templateSettings.imports)));