Skip to content

Commit 230dc35

Browse files
committed
Fixes #470, allow anon wrapped umd pattern with interior modules to work as dependency built into an optimized bundle for another project.
1 parent 908d3ac commit 230dc35

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
build/jslib/uglifyjs2/temp
55
build/tests/builds
66
build/tests/lib/amdefine/built.js
7+
build/tests/lib/anonUmdInteriorModules/main-built.js
78
build/tests/lib/appDirSrcOverwrite/www-built
89
build/tests/lib/cjsTranslate/www-built
910
build/tests/lib/comments/built.js

build/jslib/parse.js

+40-1
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,15 @@ define(['./esprimaAdapter', 'lang'], function (esprima, lang) {
522522
node.left.property && node.left.property.name === 'amd';
523523
};
524524

525+
//define.amd reference, as in: if (define.amd)
526+
parse.refsDefineAmd = function (node) {
527+
return node && node.type === 'MemberExpression' &&
528+
node.object && node.object.name === 'define' &&
529+
node.object.type === 'Identifier' &&
530+
node.property && node.property.name === 'amd' &&
531+
node.property.type === 'Identifier';
532+
};
533+
525534
//require(), requirejs(), require.config() and requirejs.config()
526535
parse.hasRequire = function (node) {
527536
var callName,
@@ -699,7 +708,7 @@ define(['./esprimaAdapter', 'lang'], function (esprima, lang) {
699708
* Otherwise null.
700709
*/
701710
parse.parseNode = function (node, onMatch) {
702-
var name, deps, cjsDeps, arg, factory,
711+
var name, deps, cjsDeps, arg, factory, exp, refsDefine, bodyNode,
703712
args = node && node[argPropName],
704713
callName = parse.hasRequire(node);
705714

@@ -772,6 +781,36 @@ define(['./esprimaAdapter', 'lang'], function (esprima, lang) {
772781
}
773782

774783
return onMatch("define", null, name, deps, node);
784+
} else if (node.type === 'CallExpression' && node.callee &&
785+
node.callee.type === 'FunctionExpression' &&
786+
node.callee.body && node.callee.body.body &&
787+
node.callee.body.body.length === 1 &&
788+
node.callee.body.body[0].type === 'IfStatement') {
789+
bodyNode = node.callee.body.body[0];
790+
//Look for a define(Identifier) case, but only if inside an
791+
//if that has a define.amd test
792+
if (bodyNode.consequent && bodyNode.consequent.body) {
793+
exp = bodyNode.consequent.body[0];
794+
if (exp.type === 'ExpressionStatement' && exp.expression &&
795+
parse.hasDefine(exp.expression) &&
796+
exp.expression.arguments &&
797+
exp.expression.arguments.length === 1 &&
798+
exp.expression.arguments[0].type === 'Identifier') {
799+
800+
//Calls define(Identifier) as first statement in body.
801+
//Confirm the if test references define.amd
802+
traverse(bodyNode.test, function (node) {
803+
if (parse.refsDefineAmd(node)) {
804+
refsDefine = true;
805+
return false;
806+
}
807+
});
808+
809+
if (refsDefine) {
810+
return onMatch("define", null, null, null, exp.expression);
811+
}
812+
}
813+
}
775814
}
776815
};
777816

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
(function (root, factory) {
2+
if (typeof define === 'function' && define.amd) {
3+
define(factory);
4+
} else {
5+
root.Bam = factory();
6+
}
7+
}(this, function () {
8+
9+
//Pretend almond and other stuff in here
10+
//The build parsing should not dive into here
11+
12+
define('cs',{load: function(id){throw new Error("Dynamic load not allowed: " + id);}});
13+
14+
if ( typeof define === "function" && define.amd && define.amd.jQuery ) {
15+
define( "jquery", [], function () { return jQuery; } );
16+
}
17+
18+
define('cs!src/view',['backbone', 'jquery', 'underscore'], function(Backbone, $, _) {});
19+
20+
return require('cs!src/main');
21+
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
baseUrl: '.',
3+
name: 'main',
4+
out: 'main-built.js',
5+
optimize: 'none'
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
define(['bam'], function (bam) {
2+
3+
});

0 commit comments

Comments
 (0)