In order for static analysis to work properly, you'll need to follow a few simple rules when using the require function:
In the wrapper, the first argument MUST be named
"require".
// Wrong!
define(function(req) {
// ...
});
// Right!
define(function(require) {
// ...
});
Don't rename the require function, or assign any other value
to the require variable in any scope.
// Wrong - Renaming "require"!
var req = require, mod = req("./mod");
// Wrong - Redefining "require"!
require = function() {};
// Wrong - Redefining "require" as a function parameter!
function F(require) {}
// Wrong - Redefining "require" in a nested scope!
function F() {
var require = function() {};
}
The argument to require MUST be a single string
literal.
// Wrong!
require(myModule);
// Wrong!
require("my-" + "module");
// Wrong!
require("MY-MODULE".toLowerCase());
// Right!
require("my-module");
I assure you, you can create large, complex applications while following
these rules. Think of require not as a function, but as a
language-level statement, and you'll be just fine.
Occasionally developers have felt the need to use require
conditionally:
if (todayIsWeekend)
require("play");
else
require("work");
Keep in mind that from the point of view of static analysis, this module is dependant on both the "play" and "work" modules, and the loader will probably attempt to fetch both.
In this case, you can use require.async to load conditional
modules asynchronously.
This page is forked from FlyScript's Documentation. Thanks very much to Kevin H. Smith.