Last active
January 13, 2018 22:06
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Import Project="$(SolutionDir)\tools\MinifyTask.proj" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the solution root create a new folder named
tools
and copy theMinifyTask.proj
inside. In VS.NET unload your web app project, edit the .csporj
file and add at the bottom the import directive as shown inWebApp.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.