Hi,
I'd like to add require.js to XWiki to be included in every page just before
prototype.js.
I would also like to add jQuery to our resources directory but only make it available to
scripts which "pull it in" using require().
I have come to the conclusion that jQuery is a de-facto standard and even if we don't
port
to it, we should make it available to our users. Require.js is an implementation of the
Asynchronous Module Definition standard which will allow us to use both jQuery and
prototype
relatively harmoniously.
I think it's obvious that if javascript rich webapps are going to scale, they need
modularity and I've reviewed AMD and the main competitor CommonJS and concluded that
AMD
will provide the user with better page load time by asynchronously loading modules
according
to a dependency tree while CommonJS blocks on each call to require().
I would like to see us move away from prototype but it provides more functionality than
jQuery including an OOP framework and while there are other libraries which show promise,
I don't feel right proposing a best practice without first porting some code to it to
see
what issues arise so IMO we should begin using require.js but accept that prototype still
has it's place.
So I propose:
Include require in the header vm.
Include jQuery but only if a script "require()'s" it.
Establish a best practice of using require() and define() instead of global variables but
accept that prototype.js still has it's place.
WDYT?
Caleb
miscellany:
------------------------------
require.js vital statistics:
Dual licensed BSD and MIT.
Latest Release: 2.1.2
1,993 lines before minification
24,621 lines in every .js file in the /tests/ directory.
Used by:
https://github.com/adobe/brackets/
https://github.com/ajaxorg/cloud9 (
https://c9.io/)
http://browserquest.mozilla.org/
http://www.bbc.co.uk/frameworks/barlesque/examples/global/requirejs
http://www.officejs.org/
-----------------------------
Optimization:
require.js comes with a tool for linting, compiling (using Closure Compiler),
and linking AMD modules together based on their dependencies. Complex projects
with many js files can be can be compiled into a single "statically linked" js
file.
It supports Rhino so it could be included in the maven build.
-----------------------------
My experimentation with require.js:
I use the require.js legacy shim to make jquery.sheet function as a require.js module
without making any direct changes to jquery.sheet.
I was able to move the code from XWiki to the filesystem (file:///) a total change
of the directory structure and only needed to change the main example.html file.
I was able to include the content in an iframe simply by defining a require.js plugin
see:
http://requirejs.org/docs/plugins.html for loading through an iframe and changing
the require() call to require the iframe'd sheet.
https://github.com/cjdelisle/jquery-sheet-amd