I'm trying to understand the benefits of the popular r.js.
It seems to...
- concatenate a list of manually selected JavaScript files
- uglify/minimize that bined code
- do some similar stuff for CSS files (bine them)
Also, (what it makes it different from generic bine/minify tools) it seems to...
- convert Node-style
require()
modules to AMD style modules - name anonymous modules (eg.
define(['dependency'], function(){...}
) - offer some support for loader plugins, e.g. inline CSS files
It does not seem to...
- analyze and automatically resolve dependencies found in files (like, include file
foo.js
into the package just because r.js finds adefine(["foo"], ...)
Is this correct, or did I miss something?
I'm trying to understand the benefits of the popular r.js.
It seems to...
- concatenate a list of manually selected JavaScript files
- uglify/minimize that bined code
- do some similar stuff for CSS files (bine them)
Also, (what it makes it different from generic bine/minify tools) it seems to...
- convert Node-style
require()
modules to AMD style modules - name anonymous modules (eg.
define(['dependency'], function(){...}
) - offer some support for loader plugins, e.g. inline CSS files
It does not seem to...
- analyze and automatically resolve dependencies found in files (like, include file
foo.js
into the package just because r.js finds adefine(["foo"], ...)
Is this correct, or did I miss something?
Share Improve this question edited Oct 18, 2014 at 1:24 nhaa123 9,81811 gold badges45 silver badges63 bronze badges asked Oct 6, 2014 at 20:07 Udo GUdo G 13.2k16 gold badges63 silver badges92 bronze badges 6-
Uh, was my edit incorrect and you really meant that it should "include file
foo.js
into the package just because r.js finds adefine(["foo"], ...)
"? If the code is already there (foo
is defined), I don't see why you would want it to load thefoo.js
file. – Bergi Commented Oct 6, 2014 at 20:15 - RequireJS/Optimization: "[r.js] Combines related scripts together into build layers and minifies them". The dependency graph itself is still resolved at run-time, all the modules have simply been shoved together and so do not require separate resource fetches. – user2864740 Commented Oct 6, 2014 at 20:16
- @Bergi: Don't know about your edit, but yes, I want to make sure that it's correct that r.js does not automatically resolve any dependencies. I'm not saying it should do so. It just appears to me that r.js does some heavy JavaScript parsing/processing and want to make sure I understand all steps in that process. – Udo G Commented Oct 6, 2014 at 20:22
- 1 @user2864740: so, for modules that are already AMD-style, r.js does not do any special processing that can't be done by a classic biner/minifier? (no criticism intended) – Udo G Commented Oct 6, 2014 at 20:24
- 1 @UdoG I mean: Normal module-per-file dependencies will be included, pretty much unless excluded, but the object graphs/load-order won't be. – user2864740 Commented Oct 7, 2014 at 4:18
1 Answer
Reset to default 7You 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 usingr.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 thatfoo
is required unless you setfindNestedDepencencies
totrue
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 therequire
anddefine
calls expect dependencies. So if you do:define(function () { var deps = ["foo"]; require(deps, function (foo) { }); });
Then
r.js
won't know thatfoo
is a dependency, because inrequire(deps, ...
your dependencies appear as a symbol reference, not as a list of strings. You would have to specifyfoo
as a dependency manually in the build configuration. There's no flag to turn on to maker.js
find these cases.