Общо показвания

юли 22, 2011

On script loaders

There is a lot of discussion going on on JavaScript loaders recently.

It is a bit unclear to me how the authors and supporters of this or that loader claim theirs is best, while none of them is doing something more than loading files that are previously 'required'.

Here is what I am talking about:

Usually a loader library/script is used like this:

  • Describe my dependencies 
  • Require my dependencies
  • Run a script when my dependencies are loaded
This is exactly the same as just stating your dependencies in JSON format and load them one after another if you know your stuff (i.e. you know which file depends on which, this is usually the case by the way, because you need to know this to describe the dependencies in the first place). All those fancy loaders come down to simple iteration over list of files and loading them. Some of them try to be smarter and try to load "dependencies" and even check if the dependency is met when the file is loaded (RequireJS for example). Still, this is exactly the same because most developers do not separate each function in a file, instead the functionality is grouped. You end up with a big big description of global objects and its structures for 3 or 4 files. Too much work.

What I really expect to see coming is a loader, that based on actual usage loads the required module(s). This would mean:
  • load scripts only when they are used (and not at the start, in some cases they might never be used in the application if its runtime)
  • catch missing object references and postpone the execution for the time when the module is loaded, then re-run it
This is of course more complicated, but still would be really nice. Maybe some day.

Няма коментари: