Skip to content

Commit

Permalink
Handle when factory args > dependent module ids.
Browse files Browse the repository at this point in the history
e.g. define(['dep1', 'dep2'], function (dep1, dep2, dep3) {});
The curl/loader/cjsm11 cram plugin does this now!
(see cujojs/curl#253)
  • Loading branch information
unscriptable committed Feb 13, 2014
1 parent 4fe1803 commit 1928485
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lib/transform/amdToSimplifiedAmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,18 @@ define(function () {
depList = ['require', 'exports', 'module'].slice(0, argList.length);
}

if (argList.length < depList.length) {
// create placeholders at end
/* scenarios:
1. factory args and module ids match (ideal case):
define(['dep1', 'dep2'], function (dep1, dep2) {});
2. more factory args than module ids (cjsm11 loader does this):
define(['dep1', 'dep2'], function (dep1, dep2, dep3) {});
3. more module ids than factory args (common user shortcut):
define(['dep1', 'dep2'], function (dep1) {});
*/

if (depList.length > argList.length) {
// there are more module ids than factory args,
// so create placeholders at the end of factory args.
argList = depList.map(function (dep, i) {
return i in argList ? argList[i] : idToVar('', i);
});
Expand All @@ -99,8 +109,12 @@ define(function () {
if (requires.length > 0) {
reqIds = requires.map(function (req) { return req.id; });
reqVars = requires.map(function (req) { return req.varName; });
// if there are more factory args than module ids, then dev
// may have intended the extra args to be undefined. we need
// to insert before these or the arguments won't match the
// module ids! also: cjsm11 loader does this to shadow `define`.
argList.splice.apply(argList, [depList.length, 0].concat(reqVars));
depList = depList.concat(reqIds);
argList = argList.concat(reqVars);
}

return 'define('
Expand Down

0 comments on commit 1928485

Please sign in to comment.