Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requireJS - a few questions

sorry for beeing a little lazy and not trying it all out myself, but I thought a nice answer on Stackoverflow might help some other guys too. I'm pondering whether or not to use requireJS to load my modules. Currently I'm doing that on my very own, so I have some questions about requireJS.

  • How does requireJS deal with multiple references (does it cache files/modules) ?

More precisely, if you have calls like require(["some/module", "a.js", "b.js"], function...}); and you again reference a.js or b.js in later .require or .define calls, how does requireJS handle those? My guess is, it will entirely ignore those additional references, is that correct? If so, is it possible to force requireJS to reload a script ?

  • Does requireJS always transfer files over the wire or you can load modules statically ?

What I normally do is, to concatenate all of my js files (modules included), except for those which need to get loaded dependent on run-time conditions. As far as I read the requireJS doc, you can define own names for modules. So my question is, can you load a module which is already present in the script, without transferring it over the wire ? As far as I understood the doc, names are created automatically for modules, based on their path location and filename, so this would make no sense for my requirement here.

like image 826
jAndy Avatar asked Apr 17 '12 16:04

jAndy


People also ask

Is RequireJS still relevant?

RequireJS has been a hugely influential and important tool in the JavaScript world. It's still used in many solid, well-written projects today.

What is RequireJS used for?

RequireJS is a JavaScript library and file loader which manages the dependencies between JavaScript files and in modular programming. It also helps to improve the speed and quality of the code.

What is define in RequireJS?

The define() function can be used to load the modules (module can be an object, function, class or a code which is executed after loading a module). You can load different versions of the same module in the same page.

What is the difference between RequireJS CommonJS and AMD loaders?

The main difference between AMD and CommonJS lies in its support for asynchronous module loading. "The main difference between AMD and CommonJS lies in its support for asynchronous module loading."


2 Answers

requirejs.undef() should do the trick

like image 65
Yacine Zalouani Avatar answered Oct 06 '22 11:10

Yacine Zalouani


Normally, a module will only be loaded once by require.js. require.js will always resolve dependencies and load the modules in the right order so that you don't have to care about that. Subsequent calls to require for the same module will yield it immediately.

It is not possible to reload a module. If you really have a need for loading the same module more than once (which would unfortunately indicate that there is something wrong with your module's design) you can have a look at the Multiversion support.

I am not sure I understand what you mean by "load modules statically". But if I am guessing right you want to load several modules as one and use them seperately. This is possible: Typically in your modules you will be doing something like:

define(['moduleA', 'moduleB', 'moduleC'], function (a, b, c) {
    ...
    return exports;
});

where exports can be more or less anything, a function, an object, whatever. So you can also do something like:

define(['moduleA', 'moduleB', 'moduleC'], function (a, b, c) {
    ...
    return {moduleA: a, moduleB: b, moduleC: c};
});

exporting them all together.

Note however that you should really have a look at the optimization tool. It can group related modules together.

Finally, the automatic naming is a misunderstanding, you can be explicit on the names of your modules.

like image 30
ggozad Avatar answered Oct 06 '22 11:10

ggozad