Minify CSS and JS in Asp.Net when Publish a WebApplication

GibboK picture GibboK · Oct 19, 2011 · Viewed 7.5k times · Source

I use Asp.net 4 C# and MicrosoftAjax Minifier.

Please look at my code here; Using <Target Name="AfterBuild"> I'm able to minify all files .js and .css. The problem is that this code minify even the original files in my project solution, so would be almost impossible to edit theme once again.

I need instead minify all .js and .css on my solution after publishing it to a local folder.

In this way I can keep my original files in my project folder intact and have instead the site compiled and minified into another local folder.

I change my scripting using <Target Name="Publish">, I do not receive any error but It does not work.

Could you tell me what I'm missing here, and if there is a better approach to solve this problem? Thanks

<!-- Minify all JavaScript files that were embedded as resources -->
<Import Project="$(MSBuildExtensionsPath)\Microsoft\MicrosoftAjax\ajaxmin.tasks" />
<Target Name="Publish">
    <ItemGroup>
        <JS Include="**\*.js" Exclude="**\*.min.js;Scripts\*.js" />
    </ItemGroup>
    <ItemGroup>
        <CSS Include="**\*.css" Exclude="**\*.min.css" />
    </ItemGroup>
    <AjaxMin
        JsSourceFiles="@(JS)"  JsSourceExtensionPattern="\.js$" JsTargetExtension=".js"
        CssSourceFiles="@(CSS)" CssSourceExtensionPattern="\.css$" CssTargetExtension=".css"  />
</Target>

Answer

Will picture Will · Oct 20, 2011

(copied from questions)

To deal with losing the original js/css files, ensure the target filenames include .min, eg. JsTargetExtension=".min.js" and CssTargetExtension=".min.css". See article on using MsAjax minifier: http://www.asp.net/ajaxlibrary/ajaxminquickstart.ashx

To then be able to publish the minified files, see this article: Using Microsoft AJAX Minifier with Visual Studio 2010 1-click publish

At my workplace, we are solving the minifying issue quite differently by using Chirpy addin for Visual Studio. It automatically minifies files to a .min version upon saving. See http://chirpy.codeplex.com/