MSBuild Task Reference and Ajax Minification

image

I’m not one for dumping all scripts into a single folder and calling it a day.

The template project for ASP.NET MVC 3 does that unfortunately. So, after a bit of cleanup and adding a few more JavaScript libraries that I needed, I’m left with a reasonably organized folder structure.

I’ve added a folder for JavaScript files associated with Views, in this case, it’s located here: Scripts\Views\ViewName\…

I want to create minified versions of scripts, yet not minify scripts that have already been minified by other processes. The Microsoft Ajax Minifier (download here) includes a msbuild task, making the effort of creating minified scripts painless.

I followed these basic instructions for adding the build task:

http://www.asp.net/ajaxlibrary/AjaxMinQuickStart.ashx

I did not however, as I mentioned, want to compress all JavaScript files.

So, based on this documentation (MSBuild Items), I changed the basic Include settings to reflect my folder structure.

  <Import Project="$(MSBuildExtensionsPath)\Microsoft\MicrosoftAjax\ajaxmin.tasks" />
  <Target Name="AfterBuild">
    <ItemGroup>
      <JS Include="Scripts\Views\\**\*.js" Exclude="Scripts\Views\\**\*.min.js;" />
    </ItemGroup>
    <ItemGroup>
      <CSS Include="**\*.css" Exclude="**\*.min.css" />
    </ItemGroup>
    <AjaxMin JsSourceFiles="@(JS)" JsSourceExtensionPattern="\.js$" JsTargetExtension=".min.js" CssSourceFiles="@(CSS)" CssSourceExtensionPattern="\.css$" CssTargetExtension=".min.css" />
  </Target>

What I wanted specifically, is to only process JavaScript files found in the Scripts\Views subfolders. So, I changed the Include attribute to:

Scripts\Views\**\*.js

Roughly translated to English, the above:

  1. Recursively, find all JavaScript files under the folder Scripts\Views
  2. Skip anything that is already named *.min.js
  3. Minify.

One Comment

Comments are closed.