3

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>
GibboK
  • 71,848
  • 143
  • 435
  • 658
  • Just a guess, having never used MSAjax to minify, but shouldn't the target filenames include `.min` so you don't overwrite the source files? eg. `JsTargetExtension=".min.js"` and `CssTargetExtension=".min.css"` ? – Will Oct 19 '11 at 10:36
  • VS do not let me publish the file with extension .min.js, for this reason I wrote this question. – GibboK Oct 19 '11 at 10:38
  • at the moment I'm using also a version with -min.css and -min.js, this allowed be to have file automatically published, but I think it is not a clean solution plus I have to change the path for my resources. – GibboK Oct 19 '11 at 10:40
  • This article on the MS Ajax site shows how to do this: http://www.asp.net/ajaxlibrary/ajaxminquickstart.ashx – Will Oct 19 '11 at 10:43
  • Thanks Will, the script in the tutorial does the trick, but as I sad I have problem to include the .min.js file when using the PUBLISH features in VS. DO you know a way to include this file when publishing my web application? – GibboK Oct 19 '11 at 10:52
  • 1
    Does this article help? (sorry, it's big) http://stackoverflow.com/questions/5043504/using-microsoft-ajax-minifier-with-visual-studio-2010-1-click-publish – Will Oct 19 '11 at 10:58
  • thanks to point me out this article – GibboK Oct 19 '11 at 11:41

1 Answers1

3

(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/

Community
  • 1
  • 1
Will
  • 2,512
  • 14
  • 19