JSResolver.py


Purpose: Streamlining maintenance of JavaScript files by server-side dependency resolution of JavaScript files.

Note: A previous version of JSResolver included JavaScript compression as well. This function has been split out into a separate module (see JSCompressor).

License: Python
Download URL http://science.uwaterloo.ca/~mpalmer/software/JSResolver.txt. This is simply the Python source, disguised as a .txt file because the server doesn't seem to like .py extensions.

How to use / Javascript:

Just an example from my own hard-drive: The script "dragger.js" depends on "signal.js", which in turn requires "core.js" and "sugar.js". So, at the top of "dragger.js" it says

// requireScript signal

whereas at the top of "signal.js", it says

// requireScript core, sugar
       

Nesting can go on to any limit (as long as you stay beneath Python's recursion limit ;) ).

How to use / Python:

You instantiate class JSResolver, passing the root directory of your JavaScript files. You then can then call the following methods on your JSResolver instance to obtain the resolved dependencies in various formats:

Method Output (using scriptName='dragger.js', see above example)
asTags(scriptName) <script language='javascript' src='sugar.js'></script>
<script language='javascript' src='core.js'></script>
<script language='javascript' src='signal.js'></script>
<script language='javascript' src='dragger.js'></script>
asNames(scriptName) ['sugar.js', 'core.js', 'signal.js', 'dragger.js']
asNamesAndAges(scriptName) [('sugar.js', 1151766654), ('core.js', 1148740856), ('signal.js', 1148740856), ('dragger.js', 1148740856)]
asMerged(scriptName) This would return the actual content of all files, concatenated into a single string.

It would be up to you arrange for the output to be sent to the client - JSResolver is framework-agnostic. The most straightforward thing is to use asTags, of course, and insert its content straight into the webpage you are returning. asMerged may come in handy if you want to minimize the number of requests to your server; if the output is big, you may want to pass it through JSCompressor and cache it. The other functions are there just in case you want to roll your own one way or the other.


You might also be interested in yaptoo, a HTML templating module with configurable template syntax.

Created and maintained by Michael Palmer.