Consider RunJS for jQuery code loading
|Reported by:||jrburke||Owned by:|
This is an alternative proposal to a $.require() function.
Code loading in the browser is a cross-library concern, and I believe it is best served by something that can be a stand-alone script, something that can be used to load even jQuery itself.
I propose RunJS as the code loader for jQuery. It is a standalone project and recently relicensed to be a tri-license like Sizzle: http://github.com/jrburke/runjs
Here are the requirements RunJS is trying to meet: http://github.com/jrburke/runjs/blob/master/docs/requirements.md
Here are specific instructions that detail how it can be used with jQuery now (links to a downloadable sample at the end): http://github.com/jrburke/runjs/blob/master/docs/jquery.md
Here is a patch to jQuery that integrates RunJS, in a similar fashion as Sizzle: http://github.com/jrburke/jquery/commit/8678f98a3a514f0a5f14a51edad970c616d46a6f
That is the bare-bones patch. I believe starting jQuery off using a stand-alone loader instead of something that is only accessible inside of jQuery (like via a $.require) will make it harder in the long term to have jquery code interoperate with other modules in the future.
Given the "no global modules" approach that is supported by RunJS, it would be possible to construct jQuery so that it would not need to create a global jQuery object. This makes it nice if multiple versions of jQuery needed to be loaded in the page with their own dependencies.
The build tools with RunJS could allow each piece of jQuery to be used on its own -- each component JS file can specify its dependencies and the RunJS build will make it easy to create custom versions of jQuery.
I am happy to make changes to RunJS if there is something that would help make it easier to integrate jQuery.
I am working with Dojo to get RunJS integrated in any future efforts for that toolkit. I can already convert the Dojo 1.4.0 code to use it, but the i18n bundles for layers is are not fully optimized yet. RunJS is capable enough to handle larger Dojo projects with many nested dependencies. I also plan on approaching other toolkits to try to get it as the baseline code loader that works well in the browser. However, with this ticket, jQuery would be the first one to integrate RunJS, if jQuery chooses to do so.
With the baseline build of RunJS integrated with today's jQuery master, the file sizes break out like so:
- no RunJS: 68,459 bytes
- w/ RunJS: 74,224 bytes
- no RunJS: 23,100 bytes
- w/ RunJS: 25,214 bytes
These numbers were generated by typing in "make" and looking in the dist directory.