Skip to content

Commit

Permalink
Allow define(string, function) where function should be scanned for d…
Browse files Browse the repository at this point in the history
…ependencies. Test it by trying out some flavors of universal module boilerplate.
  • Loading branch information
jrburke committed Oct 7, 2011
1 parent a9eec19 commit 5d28b60
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 21 deletions.
6 changes: 3 additions & 3 deletions require.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** vim: et:ts=4:sw=4:sts=4
* @license RequireJS 0.27.0 Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
* @license RequireJS 0.27.0+ Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/requirejs for details
*/
Expand All @@ -11,7 +11,7 @@
var requirejs, require, define;
(function () {
//Change this version number for each release.
var version = "0.27.0",
var version = "0.27.0+",
commentRegExp = /(\/\*([\s\S]*?)\*\/|\/\/(.*)$)/mg,
cjsRequireRegExp = /require\(\s*["']([^'"\s]+)["']\s*\)/g,
currDirRegExp = /^\.\//,
Expand Down Expand Up @@ -1565,7 +1565,7 @@ var requirejs, require, define;

//If no name, and callback is a function, then figure out if it a
//CommonJS thing with dependencies.
if (!name && !deps.length && isFunction(callback)) {
if (!deps.length && isFunction(callback)) {
//Remove comments from the callback string,
//look for require calls, and pull them into the dependencies,
//but only if there are function args.
Expand Down
5 changes: 3 additions & 2 deletions tasks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Release Notes
Next release
--------------

- Work on module adapater format, modify optimizer to see it: https://gist.github.com/1262861
- test rflorence changes to things like bonzo: https://github.com/ded/bonzo/issues/32

- Update uglifyjs for the release!

- Doc: add info about overriding requirejs.onError.
Expand All @@ -14,8 +17,6 @@ Next release

- almond release: look at splice change

- test rflorence changes to things like bonzo: https://github.com/ded/bonzo/issues/32

- Overriding modules used in r.js? https://github.com/jrburke/r.js/issues/24

- onModuleLoad, useful?
Expand Down
2 changes: 2 additions & 0 deletions tests/all-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ load("anon/anon-tests.js");
load("packages/packages-tests.js");
load("plugins/sync-tests.js");
load("plugins/fromText/fromText-tests.js");
load("universal/universal-tests.js");
load("defineError/defineError-tests.js");


//Print out the final report
doh.run();
4 changes: 4 additions & 0 deletions tests/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ doh.registerUrl("layers", "../layers/layers.html", 10000);

doh.registerUrl("afterload", "../afterload.html", 10000);

doh.registerUrl("universal", "../universal/universal.html");
doh.registerUrl("universalBuilt", "../universal/universal-built.html");


doh.registerUrl("nestedDefine", "../nestedDefine/nestedDefine.html");
doh.registerUrl("defineError", "../defineError/defineError.html");

Expand Down
34 changes: 18 additions & 16 deletions tests/anon/anon-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,24 @@ require({
//Also try a require call after initial
//load that uses already loaded modules,
//to be sure the require callback is called.
setTimeout(function () {
require(["blue", "red", "magenta"], function (blue, red) {
doh.register(
"anonSimpleCached",
[
function colorsCached(t){
t.is("red", red.name);
t.is("blue", blue.name);
t.is("redblue", magenta.name);
t.is("hello world", magenta.message);
}
]
);
doh.run();
});
}, 300);
if (require.isBrowser) {
setTimeout(function () {
require(["blue", "red", "magenta"], function (blue, red) {
doh.register(
"anonSimpleCached",
[
function colorsCached(t){
t.is("red", red.name);
t.is("blue", blue.name);
t.is("redblue", magenta.name);
t.is("hello world", magenta.message);
}
]
);
doh.run();
});
}, 300);
}
}
]
);
Expand Down
9 changes: 9 additions & 0 deletions tests/universal/eye.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
!function (name, definition) {
if (typeof module != 'undefined') module.exports = definition()
else if (typeof define == 'function' && typeof define.amd == 'object') define(name, definition)
else this[name] = definition()
}('eye', function() {
return {
name: 'eye'
}
})
32 changes: 32 additions & 0 deletions tests/universal/newt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*jslint strict: false */
/*global define: false, module: false, require: false, window: false */

(function (define) {
//The 'id' is optional, but recommended if this is
//a popular web library that is used mostly in
//non-AMD/Node environments.
define(function (require) {
//If have dependencies, get them here
var tail = require('tail'),
eye = require('eye');

//Return the module definition.
return {
name: 'newt',
eyeName: eye.name,
tailName: tail.name
};
});
}(typeof define === 'function' && define.amd ? define : function (id, factory) {
if (typeof module !== 'undefined' && module.exports) {
//Node
module.exports = factory(require);
} else {
//Create a global function. Only works if
//the code does not have dependencies, or
//dependencies fit the call pattern below.
window.myGlobal = factory(function (value) {
return window[value];
});
}
}));
32 changes: 32 additions & 0 deletions tests/universal/spell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*jslint strict: false */
/*global define: false, module: false, require: false, window: false */

(function (define) {
//The 'id' is optional, but recommended if this is
//a popular web library that is used mostly in
//non-AMD/Node environments.
define('spell', function (require) {
//If have dependencies, get them here
var newt = require('newt');

//Return the module definition.
return {
name: 'spell',
newtName: newt.name,
tailName: newt.tailName,
eyeName: newt.eyeName
};
});
}(typeof define === 'function' && define.amd ? define : function (id, factory) {
if (typeof module !== 'undefined' && module.exports) {
//Node
module.exports = factory(require);
} else {
//Create a global function. Only works if
//the code does not have dependencies, or
//dependencies fit the call pattern below.
window.myGlobal = factory(function (value) {
return window[value];
});
}
}));
9 changes: 9 additions & 0 deletions tests/universal/tail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
!function (name, definition) {
if (typeof module != 'undefined') module.exports = definition()
else if (typeof define == 'function' && typeof define.amd == 'object') define('tail', definition)
else this[name] = definition()
}('tail', function() {
return {
name: 'tail'
}
})
13 changes: 13 additions & 0 deletions tests/universal/universal-built.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>require.js: Universal Module Wrapper: Built Tests</title>
<script type="text/javascript" src="../doh/runner.js"></script>
<script type="text/javascript" src="../doh/_browserRunner.js"></script>
<script type="text/javascript" data-main="universal-tests-built" src="../../require.js"></script>
</head>
<body>
<h1>require.js: Universal Module Wrapper: Built Tests</h1>
<p>Check console for messages</p>
</body>
</html>
108 changes: 108 additions & 0 deletions tests/universal/universal-tests-built-expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@

!function (name, definition) {
if (typeof module != 'undefined') module.exports = definition()
else if (typeof define == 'function' && typeof define.amd == 'object') define('tail', definition)
else this[name] = definition()
}('tail', function() {
return {
name: 'tail'
}
})
;
define("tail", function(){});

!function (name, definition) {
if (typeof module != 'undefined') module.exports = definition()
else if (typeof define == 'function' && typeof define.amd == 'object') define(name, definition)
else this[name] = definition()
}('eye', function() {
return {
name: 'eye'
}
})
;
define("eye", function(){});

/*jslint strict: false */
/*global define: false, module: false, require: false, window: false */

(function (define) {
//The 'id' is optional, but recommended if this is
//a popular web library that is used mostly in
//non-AMD/Node environments.
define('newt',['require','tail','eye'],function (require) {
//If have dependencies, get them here
var tail = require('tail'),
eye = require('eye');

//Return the module definition.
return {
name: 'newt',
eyeName: eye.name,
tailName: tail.name
};
});
}(typeof define === 'function' && define.amd ? define : function (id, factory) {
if (typeof module !== 'undefined' && module.exports) {
//Node
module.exports = factory(require);
} else {
//Create a global function. Only works if
//the code does not have dependencies, or
//dependencies fit the call pattern below.
window.myGlobal = factory(function (value) {
return window[value];
});
}
}));
;
/*jslint strict: false */
/*global define: false, module: false, require: false, window: false */

(function (define) {
//The 'id' is optional, but recommended if this is
//a popular web library that is used mostly in
//non-AMD/Node environments.
define('spell',['require','newt'], function (require) {
//If have dependencies, get them here
var newt = require('newt');

//Return the module definition.
return {
name: 'spell',
newtName: newt.name,
tailName: newt.tailName,
eyeName: newt.eyeName
};
});
}(typeof define === 'function' && define.amd ? define : function (id, factory) {
if (typeof module !== 'undefined' && module.exports) {
//Node
module.exports = factory(require);
} else {
//Create a global function. Only works if
//the code does not have dependencies, or
//dependencies fit the call pattern below.
window.myGlobal = factory(function (value) {
return window[value];
});
}
}));
;
require({baseUrl: require.isBrowser ? "./" : "./universal/"}, ["spell"], function(spell) {
doh.register(
"universal",
[
function universal(t){
t.is('spell', spell.name);
t.is('newt', spell.newtName);
t.is('tail', spell.tailName);
t.is('eye', spell.eyeName);
}
]
);

doh.run();
});
;
define("universal-tests", function(){});
15 changes: 15 additions & 0 deletions tests/universal/universal-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require({baseUrl: require.isBrowser ? "./" : "./universal/"}, ["spell"], function(spell) {
doh.register(
"universal",
[
function universal(t){
t.is('spell', spell.name);
t.is('newt', spell.newtName);
t.is('tail', spell.tailName);
t.is('eye', spell.eyeName);
}
]
);

doh.run();
});
13 changes: 13 additions & 0 deletions tests/universal/universal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>require.js: Universal Module Wrapper Tests</title>
<script type="text/javascript" src="../doh/runner.js"></script>
<script type="text/javascript" src="../doh/_browserRunner.js"></script>
<script type="text/javascript" data-main="universal-tests" src="../../require.js"></script>
</head>
<body>
<h1>require.js: Universal Module Wrapper Tests</h1>
<p>Check console for messages</p>
</body>
</html>

0 comments on commit 5d28b60

Please sign in to comment.