It's no simple task! Yahoo does not have an official build process.
Doug mentioned that he is working on it. I probably shouldn't go into
detail anymore than that.
I'm guessing you mean Douglas Crockford.
You work at Yahoo! but have a gmail account? I didn't know that
combination was possible.
There's an interesting article on ajaxpatterns.org regarding download
on demand javascript. It's definitely not the easiest approach; it's
along the lines of what Dojo recommends.
I think this on-demand approach encourages a lot of client-server talk
with smaller bits of JavaScript for each request. This means the
fraction of request time dedicated to making the connection is high
compared to the time for actual JavaScript transfer. This also results
in latency when the user clicks something and the JavaScript must
first be downloaded before the action can be performed. If the only
win is faster page loads, it is usually possible to minify, maybe
obfuscate, and gzip code down to the size of a single image file.
There are also implementation issues with on-demand/lazy loading since
either Ajax or script insertion techniques both have reasonably
serious downsides. Code retrieved by Ajax is evaluated with eval()
which will be inside a function (not the global scope) which leads to
code scoping issues. Script insertion works well and is controlled by
headers but it is necessary to monitor the order in which script
elements are added to the page. If two scripts are added at the same
time to the DOM you can't rely on the first script being already
evaluated when the second script is evaluated.
One idea is to use ANT, if you're a Java shop. You can build a file by
parsing out: an include file, or a page-specific js file that has
require/import function calls (like dojo.require). I was on a project
that did this and it made a huge difference.
I'm not exactly sure what you are describing. I think I've been
thinking about something like this as one option. Putting a comment
like this in the actual JavaScript files
/* @require('dependencyOne.js', 'dependencyTwo.js') */
This isn't a freebe either unfortunately. Suppose I have an event and
Ajax libraries used all over my site and then two page specific
JavaScript files foo & bar.
event.js
========
var Event = {...};
ajax.js
=======
var Ajax = {...};
foo.js
======
/* @require('Event') */
var foo = {...};
bar.js
======
/* @require('Event', 'Ajax', 'foo') */
var bar = {...};
What I want when all these files are built is two files. One file
contains all the foundational JavaScript I use on every page of my
site and one file has the page specific code. The foundation file is
cached when the first page of the site is visited. This leads to good
caching.
foundation-20070804085223.js
=============
var Event = {...};
var Ajax = {...};
page-20070804085223.js
======
var foo = {...};
var bar = {...};
Note the dependencies listed in the comments of foo.js and bar.js
don't really help create these compiled files.
The timestamps on the built files are for caching and I threw then in
above for completeness. It's another dimension of the problem.
Another idea would be to have a custom tag or include. I have never
done this, but doesn't seem any harder than using ANT to do it.
<js:resources>
<js:resource src="com.example.ArrayExtras.js"/>
</js:resources>
would generate:
<script src="everythingCombined.js"></script>
everythingCombined would combine your framework.js file and all the
other js:resources.
Personally, I can't wait 'till ES4 so we can have real imports.
It's going to be 8-10 years before we can reasonably rely on ES4 in
the browser. I can't wait either which is why I'm looking for a
solution
It just makes more sense to bind the behavior in the behavior layer.
Pros, cons? What are your thoughts?
I going to think more about how make files work. Yes makefiles take
time to maintain and the dependencies are separate from the source
code but they work well for a very wide set of requirements. This
completely separates the ideas of source and compiled files. This
allows for each source file to have different build rules (something
I'm really interested in lately).
It is important to me that during development that each file request
amounts to running "make" automatically before serving the requested
file. That way I don't have to run make manually after each edit.
During the site's build stage make would run and create the
timestamped files which involves some other less nasty issues then the
file dependency issues.
Definitely (maybe) something along the lines of a makefile. That seems
reasonable and very flexible for so many things.
The sad thing is I have to write this build process in
Perl...Perl...Ugg........ugg.
Peter