Skip to content

Instantly share code, notes, and snippets.

@stefanprodan
Last active January 13, 2018 22:06
Show Gist options
  • Save stefanprodan/11248241 to your computer and use it in GitHub Desktop.
Save stefanprodan/11248241 to your computer and use it in GitHub Desktop.
MSBuild Task that minifies all js files in Script folder using YUI and RequireJsNet.Compressor
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="JavaScriptCompressorTask" AssemblyFile="$(MSBuildProjectDirectory)\$(OutputPath)RequireJsNet.Compressor.dll" />
<!--Folder deploy-->
<Target Name="MinifyForBuild" AfterTargets="CopyAllFilesToSingleFolderForPackage" Condition="'$(Configuration)'=='Release'">
<MsBuild Projects="$(MSBuildProjectFile)" Targets="Minify" Properties="RootDir=$(_PackageTempDir)" />
<Message Text="Minifying for build" Importance="high"/>
</Target>
<Target Name="Minify" Condition="'$(Configuration)'=='Release'">
<ItemGroup>
<!-- Get a list of all the files you want to minify -->
<OriginalFiles Include="$(RootDir)\Scripts\**\*.js" />
</ItemGroup>
<Message Text="Start Minify" Importance="high" />
<!-- Make a copy of each file in your temp folder so that we can obtain the folder structure for our workarea -->
<Copy SourceFiles="@(OriginalFiles)" DestinationFiles="@(OriginalFiles->'$(RootDir)\MinScripts\%(RecursiveDir)%(Filename)%(Extension)')" />
<!-- After we have copied the files, there is no need for them since we only want the folders. Select them for deletion. -->
<ItemGroup>
<TempJavascriptFiles Include="$(RootDir)\MinScripts\**\*.js" />
</ItemGroup>
<!-- Delete temporary files -->
<Delete Files="@(TempJavascriptFiles)" />
<!-- Do the compressing in a temporary directory since YUI throws an exception when the source files is the same as the destination -->
<JavaScriptCompressorTask
LoggingType="Debug"
EncodingType="UTF8"
SourceFiles="@(OriginalFiles)"
OutputFile="$(RootDir)\MinScripts\%(OriginalFiles.RecursiveDir)%(OriginalFiles.Filename)%(OriginalFiles.Extension)" />
<!-- Select the newly-minified files in order to copy them back-->
<ItemGroup>
<NewFiles Include="$(RootDir)\MinScripts\**\*.js" />
</ItemGroup>
<!-- Delete the old files -->
<Delete Files="@(OriginalFiles)" />
<!-- Copy the minified files over in place of the old ones -->
<Copy SourceFiles="@(NewFiles)" DestinationFiles="@(NewFiles->'$(RootDir)\Scripts\%(RecursiveDir)%(Filename)%(Extension)')" />
<!-- Delete the min folder -->
<ItemGroup>
<DirsToClean Include="$(RootDir)\MinScripts\" />
</ItemGroup>
<RemoveDir Directories="@(DirsToClean)" />
</Target>
<Target Name="StopBuild">
<Message Text="Minify failed, stopping build." />
</Target>
</Project>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="JavaScriptCompressorTask" AssemblyFile="$(MSBuildProjectDirectory)\$(OutputPath)RequireJsNet.Compressor.dll" />
<Target Name="Minify" AfterTargets="CopyAllFilesToSingleFolderForMsdeploy" Condition="'$(Configuration)'=='Release'">
<ItemGroup>
<!-- Get a list of all the files you want to minify -->
<OriginalFiles Include="$(_PackageTempDir)\Scripts\**\*.js" />
</ItemGroup>
<Message Text="Start Minify" />
<!-- Make a copy of each file in your temp folder so that we can obtain the folder structure for our workarea -->
<Copy SourceFiles="@(OriginalFiles)" DestinationFiles="@(OriginalFiles->'$(_PackageTempDir)\MinScripts\%(RecursiveDir)%(Filename)%(Extension)')" />
<!-- After we have copied the files, there is no need for them since we only want the folders. Select them for deletion. -->
<ItemGroup>
<TempJavascriptFiles Include="$(_PackageTempDir)\MinScripts\**\*.js" />
</ItemGroup>
<!-- Delete temporary files -->
<Delete Files="@(TempJavascriptFiles)" />
<!-- Do the compressing in a temporary directory since YUI throws an exception when the source files is the same as the destination -->
<JavaScriptCompressorTask
LoggingType="Debug"
EncodingType="UTF8"
SourceFiles="@(OriginalFiles)"
OutputFile="$(_PackageTempDir)\MinScripts\%(OriginalFiles.RecursiveDir)%(OriginalFiles.Filename)%(OriginalFiles.Extension)" />
<!-- Select the newly-minified files in order to copy them back-->
<ItemGroup>
<NewFiles Include="$(_PackageTempDir)\MinScripts\**\*.js" />
</ItemGroup>
<!-- Delete the old files -->
<Delete Files="@(OriginalFiles)" />
<!-- Copy the minified files over in place of the old ones -->
<Copy SourceFiles="@(NewFiles)" DestinationFiles="@(NewFiles->'$(_PackageTempDir)\Scripts\%(RecursiveDir)%(Filename)%(Extension)')" />
</Target>
<Target Name="StopBuild">
<Message Text="Minify failed, stopping build." />
</Target>
</Project>
<Import Project="$(SolutionDir)\tools\MinifyTask.proj" />
@stefanprodan
Copy link
Author

In the solution root create a new folder named tools and copy the MinifyTask.proj inside. In VS.NET unload your web app project, edit the .csporj file and add at the bottom the import directive as shown in WebApp.csproj. Create a publish profile for your site as Web Deploy Package. When you'll build on Release the minify task will replace all js files with their min version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment