views:

31

answers:

2

I am developing a site which includes several different javascript files and libraries. For optimization purposes I have implemented YUI Compressor for .Net

This will minimize and combine my javascript files into one single file.

Now I have put this up in a MSBuild script that automatically does the compression and minimization and outputs it to a file of my choosing. However, I still wish to keep the original javascript files in my development environment. My question is simply:

Is there a good way to depending on the Debug setting for example choose which javascript to use? This to not have to change the MasterPage by hand each time I release the build.

Allow me to illustrate.

If I am running in Debug="true" I wish my MasterPage to include the following javascripts:

<script type="text/javascript" src="first.js"></script>
<script type="text/javascript" src="second.js"></script>
<script type="text/javascript" src="third.js"></script>
<script type="text/javascript" src="fourth.js"></script>

If I am running in Debug="false" I wish this to be outputted in the MasterPage:

<script type="text/javascript" src="compressedAndMinimized.js"></script>

Is there an elegant solution to this that I am missing here?

A: 

Can you not have a different MSBuild script for you development machines that doesn't run the compressor?

Paul Manzotti
Indeed we do, but that does not solve the problem as I still want to be able to xcopy the files directly from the development environment to the release environment without having to change the javascript references in the masterpage.
Robban
+1  A: 

You can use preprocessors:

<% #if(DEBUG) %>
    <script type="text/javascript" src="first.js"></script>
    <script type="text/javascript" src="second.js"></script>
    <script type="text/javascript" src="third.js"></script>
    <script type="text/javascript" src="fourth.js"></script>
<% #else %>
    <script type="text/javascript" src="compressedAndMinimized.js"></script>
<% #endif %>

In my project, I define the platform (e.g. "local", "development", "beta", and "production") in an environment variable and access it via a Config class, but I suppose checking for debug/release mode works too.

EDIT

According to http://stackoverflow.com/questions/1801007/preprocessor-statements-in-aspx, the DEBUG variable used in ASPX files is pulled from Web.config, not your project's current build configuration. This might still be usable to you, but your deployment script would need to change your Web.config when deploying to production.

MikeWyatt
Thanks Mike, this was basically exactly what I was looking for.
Robban