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
This commit is contained in:
Kit Cambridge
2012-12-07 19:41:11 -08:00
parent cdeb50132d
commit c79bed22f8
2 changed files with 26 additions and 15 deletions

View File

@@ -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

View File

@@ -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();
});
});
});
}());