<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Calyptus Life &#187; Server Side</title>
	<atom:link href="http://blog.calyptus.eu/topics/server-side/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.calyptus.eu</link>
	<description>Advanced web application development tactics - from scalability to UI design</description>
	<lastBuildDate>Wed, 28 Dec 2011 10:31:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Client Side Dependency Strategy</title>
		<link>http://blog.calyptus.eu/seb/2009/05/client-side-dependency-strateg/</link>
		<comments>http://blog.calyptus.eu/seb/2009/05/client-side-dependency-strateg/#comments</comments>
		<pubDate>Sun, 17 May 2009 13:06:00 +0000</pubDate>
		<dc:creator>Sebastian Markbåge</dc:creator>
				<category><![CDATA[Client Side]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Server Side]]></category>
		<category><![CDATA[Web Resource Manager]]></category>

		<guid isPermaLink="false">http://blog.calyptus.eu/?p=97</guid>
		<description><![CDATA[This post is in response to an off-site discussion about modular dependency strategies. But I figured I&#8217;d post it here for future reference.
The Calyptus Web Resource Manager is a project that can on compile-time or on runtime handle your JavaScript, CSS, and other client-side dependencies. You can keep source code as separate files on the [...]]]></description>
			<content:encoded><![CDATA[<p><em>This post is in response to an off-site discussion about modular dependency strategies. But I figured I&#8217;d post it here for future reference.</em></p>
<p>The <a href="http://blog.calyptus.eu/source-code/">Calyptus Web Resource Manager</a> is a project that can on compile-time or on runtime handle your JavaScript, CSS, and other client-side dependencies. You can keep source code as separate files on the server or pre-compile packages (such as .ZIP, .DLL or .JAR). Currently source is available only on the .NET/Mono platforms but the concept is valid for all platforms. </p>
<p><strong>Syntax</strong></p>
<p>The syntax is largely inspired to be compatible with <a href="http://www.scriptdoc.org/">ScriptDoc</a> and <a href="http://www.ecmascript.org/es4/spec/overview.pdf">ECMAScript 4 Draft</a> import statement. In the top of your file you add the dependencies that your file relies on:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p9718"><td class="code" id="p97code18"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
@import [package, ]filename
@include [package, ]filename
@build [package, ]filename
@compress [always|release|never]
*/</span></pre></td></tr></table></div>

<p><em>Don&#8217;t worry, we&#8217;re not going to ruin your precious open-source project with inline docs. Read on.</em></p>
<p><strong>@import</strong> &#8211; Indicates that this file has a dependency on the referenced file and that it needs to be included in the final document (implicitly before this one). The other file may be a JavaScript file, CSS, image, Flash or something else. The project is fully extensible.</p>
<p><strong>@include</strong> &#8211; Same as @import but also indicates that the referenced file should be merged into this one on compile or runtime.</p>
<p><strong>@build</strong> &#8211; Same as @include but also merges any nested @import statements. Allowing you to create a single packaged file.</p>
<p><strong>@compress</strong> &#8211; Indicate whether the document should use a compression tool (such as YUI compressor) or not. Defaults to &#8220;release&#8221;, which means that it won&#8217;t compress during the debug stage.</p>
<p>You can reference a file by either filename/namespace or package + filename/namespace. You may include wild cards to reference an entire path or namespace. If you&#8217;re referencing another file in the same package, you can exclude the package name.</p>
<p>If you are running ASP.NET you can exclude the package if you&#8217;re referencing an assembly that is already referenced in your Web.config.</p>
<p>If you&#8217;re in a .js file, the filename will automatically look for files ending in .js.</p>
<p>This allows you to do a namespace like syntax on prepackaged files:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p9719"><td class="code" id="p97code19"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
@import MooTools.Core.*
@import MooTools.More.URI
*/</span></pre></td></tr></table></div>

<p>If you want to use the runtime view generating tools the syntax depends on what View Engine you&#8217;re running. For ASP.NET WebForms you can use the following controls:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p9720"><td class="code" id="p97code20"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;c:Import</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;filename&quot;</span> <span style="color: #000066;">runat</span>=<span style="color: #ff0000;">&quot;server&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;c:Import</span> <span style="color: #000066;">assembly</span>=<span style="color: #ff0000;">&quot;package&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;namespace/filename&quot;</span> <span style="color: #000066;">runat</span>=<span style="color: #ff0000;">&quot;server&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;c:Include</span> ... <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;c:Build</span> ... <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p>In the future this will be integrated into the ASP.NET ScriptManager as well. For other view engines the syntax would be much prettier.</p>
<p><strong>Example</strong></p>
<p><em>MyBaseStyle.css</em></p>

<div class="wp_codebox"><table width="100%" ><tr id="p9721"><td class="code" id="p97code21"><pre class="css" style="font-family:monospace;">div<span style="color: #6666ff;">.BaseClassItem</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">MyBaseImage.png</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p><em>MyBaseClass.js</em></p>

<div class="wp_codebox"><table width="100%" ><tr id="p9722"><td class="code" id="p97code22"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// @import MyTheme.css</span>
<span style="color: #003366; font-weight: bold;">var</span> MyBaseClass <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #003366; font-weight: bold;">Class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  initialize<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">element</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Element<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> className<span style="color: #339933;">:</span> <span style="color: #3366CC;">'BaseClassItem'</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><em>MyChildClass.js</em></p>

<div class="wp_codebox"><table width="100%" ><tr id="p9723"><td class="code" id="p97code23"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// @import MyBaseClass.js</span>
<span style="color: #003366; font-weight: bold;">var</span> MyChildClass <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #003366; font-weight: bold;">Class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">Extends</span><span style="color: #339933;">:</span> MyBaseClass<span style="color: #339933;">,</span>
  ...
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><em>MyView.aspx</em></p>

<div class="wp_codebox"><table width="100%" ><tr id="p9724"><td class="code" id="p97code24"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;c:Include</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;MyChildClass.js&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p><em>OUTPUT:</em></p>

<div class="wp_codebox"><table width="100%" ><tr id="p9725"><td class="code" id="p97code25"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;MyBaseStyle.css&quot;</span> <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;MyBaseClass.js&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
var MyChildClass=new Class({Extends:MyBaseClass,...});
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Since I used the included command the file is included in the output document. All it&#8217;s dependencies are automatically added to the document through links.</p>
<p>Any referenced file is only added once to the output. So it&#8217;s no problem adding multiple references to the same resource in partial views or by indirect dependencies.</p>
<p><em>MyOtherView.aspx</em></p>

<div class="wp_codebox"><table width="100%" ><tr id="p9726"><td class="code" id="p97code26"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;c:Import</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;MyChildClass.js&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;c:Import</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;MyBaseClass.js&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p><em>OUTPUT:</em></p>

<div class="wp_codebox"><table width="100%" ><tr id="p9727"><td class="code" id="p97code27"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;MyBaseStyle.css&quot;</span> <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;MyBaseClass.js&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;MyChildClass.js&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>In the sample above, I import the base class after the child class. Since the child is dependent on the base, it will be included first. Therefore the second reference to MyBaseClass.js is excluded.</p>
<p><strong>Typical Work Flow &#8211; Late Optimization</strong></p>
<p>Typically you would only use the @import statement in all your resources. You should only reference any direct resources that your code or style sheet uses. Indirect files are referenced by the referenced resources so that if a dependency changes, you don&#8217;t have to update all your reliers. Your views will only reference the direct resources that it is using by import statements as well.</p>
<p>This will generate a lot of &lt;script&gt; and &lt;link&gt; tags in your documents. This is not good for production where you want to minimize the overhead of multiple requests. That&#8217;s when you start building clusters.</p>
<p><em>Common.css</em></p>

<div class="wp_codebox"><table width="100%" ><tr id="p9728"><td class="code" id="p97code28"><pre class="css" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*
@build Headers.css
@build Footers.css
@build MyBaseStyle.css
*/</span></pre></td></tr></table></div>

<p><em>Common.js</em></p>

<div class="wp_codebox"><table width="100%" ><tr id="p9729"><td class="code" id="p97code29"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
@build MooTools.Core.Fx.Tween
@build MyChildClass.js
*/</span></pre></td></tr></table></div>

<p>Now I can include the cluster Common.js in my view:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p9730"><td class="code" id="p97code30"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;c:Import</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;Common.css&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;c:Import</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;Common.js&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;c:Include</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;MyChildClass.js&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p><em>OUTPUT:</em></p>

<div class="wp_codebox"><table width="100%" ><tr id="p9731"><td class="code" id="p97code31"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;Common.css&quot;</span> <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;Common.js&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The MyChildClass.js reference and all it&#8217;s dependencies are ignored since those file has already been included in the document by Common.css and Common.js. You can for example add these clusters to your Master view to automatically optimize all your partial views. If you remove a reference from your cluster it won&#8217;t break any of your code, since those files are individually added by your partial views to your document.</p>
<p>This pattern will allow you to do late optimization of your load-time by grouping only the files that are commonly used in to clusters. Leaving edge-case files into the outer branches of your site. To accomplish this I recommend that you use a modular framework such as <a href="http://www.mootools.net/">MooTools</a>.</p>
<p>Your clusters should be named and composed in relevant packages for your site, not in packages of JavaScript frameworks. For example, DON&#8217;T create a MooTools.js cluster that includes all MooTools files.</p>
<p><em>By default, @include and @build commands are evaluated as @import during the debug stage. That makes it easy to find the references to your source code with debugging tools such as FireBug.</em></p>
<p><strong>Messing Up Your Beautiful Source? Use Place Holders</strong></p>
<p>If you&#8217;re working with a consultant project you can just put all your references in the source file. That makes it very easy to work with. But if you have an open-source project you may not want to mess up the source with dependency references. Instead, use place holder files that @include the original source and references the dependency place holders using @import.</p>
<p><em>Fx.js</em></p>

<div class="wp_codebox"><table width="100%" ><tr id="p9732"><td class="code" id="p97code32"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
@import Class.Extras.js
@include Real/Source/Fx/Fx.js
*/</span></pre></td></tr></table></div>

<p><em>Fx.CSS.js</em></p>

<div class="wp_codebox"><table width="100%" ><tr id="p9733"><td class="code" id="p97code33"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
@import Fx.js
@import Element.Style.js
@include Real/Source/Fx/Fx.CSS.js
*/</span></pre></td></tr></table></div>

<p>Now you can reference your place holders to get dependencies instead of the original source files.</p>
<p><strong>What about my CDN?</strong></p>
<p>You can use a CDN to store your clusters. Just reference the full URIs in your import statements. There is a pre-built class that does this with MooTools on Google. Just @import GoogleAPIs.MooTools.</p>
<p>I will add an <strong>@embedded</strong> syntax to reference other files that have already been included. That way you could write your own like this:</p>
<p><em>MooTools-Cluster-Google.js</em></p>

<div class="wp_codebox"><table width="100%" ><tr id="p9734"><td class="code" id="p97code34"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
@import http://ajax.googleapis.com/ajax/libs/mootools/1.2.2/mootools-yui-compressed.js
@embedded MooTools.Core.*
*/</span></pre></td></tr></table></div>

<p>If you reference this cluster in your view, all references to your local MooTools files will be ignored since it they are already included in the Google cluster.</p>
<p><strong>@include on Images</strong></p>
<p>If @include filename.png is used in a style-sheet, every instance of url(filename.png) will automatically be replaced with base64 embedded data at runtime. This is only used on the runtime version since this content can&#8217;t be sent to IE browsers. IE browsers will get the url(filename.png) reference intact.</p>
<p>This also works with view/document Include commands. In that case an &lt;img&gt; tag is rendered with a link or embedded content depending on the browser capabilities.</p>
<p>This pattern allows you to do late load time optimization of image dependencies.</p>
<p><strong>Getting Started</strong></p>
<p>As always, begin by <a href="http://blog.calyptus.eu/source-code/">checking out the source</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.calyptus.eu/seb/2009/05/client-side-dependency-strateg/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>

