From 7b9f20a981c8111fe2008bc208940aae19fc57b8 Mon Sep 17 00:00:00 2001 From: Gabriele Genta Date: Thu, 29 Sep 2016 23:25:48 +0200 Subject: [PATCH] make sure the right engine is available as `_` when withImports is true --- index.js | 2 +- package.json | 2 + test/loaderTest.js | 51 ++++++++++++++++++- test/templates/output-engine-name.html | 1 + test/templates/output/simple-lodash.txt | 4 +- test/templates/output/simple-with-imports.txt | 4 +- 6 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 test/templates/output-engine-name.html diff --git a/index.js b/index.js index d624d60..ecfcb83 100644 --- a/index.js +++ b/index.js @@ -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'; } diff --git a/package.json b/package.json index c39f04d..d5c7a78 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/loaderTest.js b/test/loaderTest.js index 7766780..9a164f9 100644 --- a/test/loaderTest.js +++ b/test/loaderTest.js @@ -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'); @@ -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', { diff --git a/test/templates/output-engine-name.html b/test/templates/output-engine-name.html new file mode 100644 index 0000000..71f944a --- /dev/null +++ b/test/templates/output-engine-name.html @@ -0,0 +1 @@ +<%-_.ENGINE_NAME%> \ No newline at end of file diff --git a/test/templates/output/simple-lodash.txt b/test/templates/output/simple-lodash.txt index 1a471a7..14e0749 100644 --- a/test/templates/output/simple-lodash.txt +++ b/test/templates/output/simple-lodash.txt @@ -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+='

'+ @@ -13,4 +13,4 @@ __p+='\n

'+ __p+='\n'; } return __p; -}.toString()).apply(undefined, _.values(_.templateSettings.imports)); +}.toString()).apply(undefined, [_].concat(_.values(_.templateSettings.imports))); diff --git a/test/templates/output/simple-with-imports.txt b/test/templates/output/simple-with-imports.txt index 25a15b8..f394f3a 100644 --- a/test/templates/output/simple-with-imports.txt +++ b/test/templates/output/simple-with-imports.txt @@ -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+='

'+ @@ -12,4 +12,4 @@ __p+='\n

'+ __p+='\n'; } return __p; -}.toString()).apply(undefined, _.values(_.templateSettings.imports)); +}.toString()).apply(undefined, [_].concat(_.values(_.templateSettings.imports)));