In Visual Studio 2013, how do I minify Javascript and CSS in the post-build step

13,393

Solution 1

All solutions I found required using different filenames for the minimized versions, and a lot of extra work to switch between using the normal/minified versions.

Instead, I wanted the compressed JavaScript files to have the original names so I didn't have to change the references in my HTML markup. I could use the normal Javascript files in my development environment, then minimized versions would be automatically deployed when publishing.

I found a simple solution that does just that.

First, install Microsoft Ajax Minifier.

Then, in your Visual Studio project file, just before the closing </Project> tag add the following :

<Import Project="$(MSBuildExtensionsPath)\Microsoft\Microsoft Ajax Minifier\ajaxmin.tasks" />
<Target Name="AfterBuild" Condition="'$(ConfigurationName)'=='Release'">
  <ItemGroup>
    <JS Include="**\*.js" Exclude="**\*.min.js;obj\**\*.*" />
  <CSS Include="**\*.css" Exclude="**\*.min.css;obj\**\*.*" />
  </ItemGroup>
  <AjaxMin
    JsSourceFiles="@(JS)" JsSourceExtensionPattern="\.js$" JsTargetExtension=".jsMIN"
    CssSourceFiles="@(CSS)" CssSourceExtensionPattern="\.css$" CssTargetExtension=".cssMIN" />
</Target>
<PropertyGroup>
  <CopyAllFilesToSingleFolderForPackageDependsOn>
    CustomCollectFiles;
    $(CopyAllFilesToSingleFolderForPackageDependsOn);
  </CopyAllFilesToSingleFolderForPackageDependsOn>
</PropertyGroup>
<Target Name="CustomCollectFiles">
  <ItemGroup>
    <MinJS Include="**\*.jsMIN" />
    <FilesForPackagingFromProject Include="%(MinJS.Identity)">
      <DestinationRelativePath>%(RecursiveDir)%(Filename).js</DestinationRelativePath>
    </FilesForPackagingFromProject>
    <MinCSS Include="**\*.cssMIN" />
    <FilesForPackagingFromProject Include="%(MinCSS.Identity)">
      <DestinationRelativePath>%(RecursiveDir)%(Filename).css</DestinationRelativePath>
    </FilesForPackagingFromProject>
  </ItemGroup>
</Target>

What does the above code do? When you publish in Visual Studio, this code will find every .js and .css file in your source, and create a minified copy using the extension .jsMIN and .cssMIN. It will ignore files that are already minified. Then it will copy these minified files to the deployment folder, using the original file names.

Voilà! You just published minified JS/CSS files, while your original files stay intact on your development environment.

Optional:
Want Ajax Minifier to be packaged with your project? From the Ajax Minifier install folder, you can move AjaxMin.dll and AjaxMinTask.dll directly into your source directory. I put them in my App_Data folder. Once they're somewhere in your source, in Visual Studio right-click them, select Include in Project, and also change their Build Action property to None.

Then in the code I included above, change
<Import Project="$(MSBuildExtensionsPath)\Microsoft\Microsoft Ajax Minifier\ajaxmin.tasks" />
to
<UsingTask TaskName="AjaxMin" AssemblyFile="$(MSBuildProjectDirectory)\App_Data\AjaxMinTask.dll" />
Done.

A troubleshooting tip:
My main code above executes AfterBuild and only when the configuration is Release. That's so it will only run during a publish. If your configuration is named something else, or you want it to run in other circumstances, modify the code as needed.

Solution 2

With Microsoft Ajax Minifier you could create powershell script to create minified versions of all files in given folder and add it to Post-build events.

Example for js files(it will be similar for css):

Get-ChildItem *.js -Exclude *.min.js |
    Foreach-Object{
    $file = [io.fileinfo]$_.Name
    ajaxmin $file.Name -out "$($file.Name).min$($file.Extension)"
}

Check also a page with full list of command line switches e.g.: -map creates source map file.

Share:
13,393
Admin
Author by

Admin

Updated on July 26, 2022

Comments

  • Admin
    Admin almost 2 years

    In Visual Studio 2013, how do I minify Javascript and CSS in the post-build step? I'd like to have every single css and js file compress into a .min.js, or .min.css in the same folder.

    I don't want to check in the minified files, but rather just have them generated post-build.

  • Artjom B.
    Artjom B. over 8 years
    Please don't post the exact same answer to multiple questions (1, 2, 3, 4, 5). If the questions are indeed duplicates then you should post your single answer to the best question and flag/vote to close the others as duplicate. If those are not duplicates, then you need to adjust every single answer to the specific question.
  • Doug S
    Doug S over 8 years
    @ArtjomB. The answer is applicable and correct for each of the questions. The questions are similar enough that no customization of the answer is necessary. If you think the questions are dupes, feel free to take action. I would never post the same answer twice, however, this question has been asked in several different variations, with no complete simple answer. This answer accomplishes that, so now each OP has the answer, along with anyone searching for the answer who stumbles across one of the question variants.
  • Artjom B.
    Artjom B. over 8 years
    I did vote to close as duplicate almost all of the questions you answered. One of them was closed.
  • TheLegendaryCopyCoder
    TheLegendaryCopyCoder over 3 years
    Hey @DougS , question; are you still using this solution today for new projects ?
  • Doug S
    Doug S over 3 years
    @TheLegendaryCopyCoder. Yep. It's great!