From c79bed22f87aab5f551f74d7f0bb0475e0648706 Mon Sep 17 00:00:00 2001 From: Kit Cambridge Date: Fri, 7 Dec 2012 19:41:11 -0800 Subject: [PATCH] Ensure that passing `settings=...` does not clobber the default `moduleId`. Precompiling a template with `settings` previously generated a snippet resembling `define(["undefined"], function(lodash) { ... })` if the `settings` object did not contain a `moduleId` property. You can now pass `settings` and `moduleId` options simultaneously; the builder will always use the `moduleId` property in `settings` if it is provided, and default to `moduleId` otherwise. Former-commit-id: 711b4f167dc08ce3d42029e9001ebdb2d8d60a56 --- build.js | 2 +- test/test-build.js | 39 +++++++++++++++++++++++++-------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/build.js b/build.js index 6d3aa3367..940379029 100755 --- a/build.js +++ b/build.js @@ -1050,7 +1050,7 @@ var templateSettings = options.reduce(function(result, value) { var match = value.match(/settings=(.+)$/); return match - ? Function('return {' + match[1].replace(/^{|}$/g, '') + '}')() + ? Function('assign, result', 'return assign(result, {' + match[1].replace(/^{|}$/g, '') + '})')(_.assign, result) : result; }, _.assign(_.clone(_.templateSettings), { 'moduleId': moduleId diff --git a/test/test-build.js b/test/test-build.js index 18a475a38..3421536a2 100644 --- a/test/test-build.js +++ b/test/test-build.js @@ -525,25 +525,36 @@ start(); }); }); - }); - asyncTest('`lodash settings=...`', function() { - var start = _.after(2, _.once(QUnit.start)); + asyncTest('`lodash settings=...`' + (command ? ' ' + command : ''), function() { + var start = _.after(2, _.once(QUnit.start)); - build(['-s', 'template=' + templatePath + '/*.tpl', 'settings={interpolate:/\\{\\{([\\s\\S]+?)\\}\\}/}'], function(source, filePath) { - var basename = path.basename(filePath, '.js'), - context = createContext(); + build(['-s', 'template=' + templatePath + '/*.tpl', 'settings={interpolate:/\\{\\{([\\s\\S]+?)\\}\\}/}'].concat(command || []), function(source, filePath) { + var moduleId, templates, + basename = path.basename(filePath, '.js'), + context = createContext(); - var data = { - 'd': { 'name': 'Mustache' } - }; + var data = { + 'd': { 'name': 'Mustache' } + }; - context._ = _; - vm.runInContext(source, context); - var templates = context._.templates; + (context.define = function(requires, factory) { + factory(_); + templates = _.templates; + moduleId = requires + ''; + }) + .amd = {}; - equal(templates.d(data.d), 'Hello Mustache!', basename); - start(); + context._ = _; + vm.runInContext(source, context); + + var templates = context._.templates; + + equal(moduleId, command ? 'underscore' : 'lodash'); + equal(templates.d(data.d), 'Hello Mustache!', basename); + delete _.templates; + start(); + }); }); }); }());