I'm trying to understand the benefits of the popular r.js.
It seems to...
Also, (what it makes it different from generic combine/minify tools) it seems to...
require()
modules to AMD style modulesdefine(['dependency'], function(){...}
)It does not seem to...
foo.js
into the package just because r.js finds a define(["foo"], ...)
Is this correct, or did I miss something?
js. A command line tool for running JavaScript scripts that use the Asynchronous Module Definition API (AMD) for declaring and using JavaScript modules and regular JavaScript script files. It is part of the RequireJS project, and works with the RequireJS implementation of AMD.
Several R packages allow the user to supply JavaScript code to be used as callback function or configuration object within a visualization or web application.
You are wrong, because r.js
does automatically resolve dependencies. If you have a main.js
file with this:
define(["foo"], function (foo) {
});
Then if you ask r.js
to create produce an optimized module out of main.js
, it will include the code for module foo
into the build.
Some caveats:
It is possible to tell r.js
to exclude modules. So if a module you think should be in an optimized bundle is absent, it may be that it has been excluded. (You know how you are using r.js
but if you use a bundle produced by someone else and you wonder, then this may be the answer: they specifically excluded a dependency from the build.)
r.js
does not find nested dependencies unless you tell it to. For instance:
define(function () {
require(["foo"], function (foo) {
});
});
r.js
won't find that foo
is required unless you set findNestedDepencencies
to true
in your build config.
r.js
can find only dependencies that are specified in the form of a list of string placed as a literal in the location where the require
and define
calls expect dependencies. So if you do:
define(function () {
var deps = ["foo"];
require(deps, function (foo) {
});
});
Then r.js
won't know that foo
is a dependency, because in require(deps, ...
your dependencies appear as a symbol reference, not as a list of strings. You would have to specify foo
as a dependency manually in the build configuration. There's no flag to turn on to make r.js
find these cases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With