How to minimize JavaScript files that were changed (and only they) during creation (aka duplicate elements after adding "ItemGroup" to csproj)

I added the following code to my .csproj to minimize the JS files that were changed when the project was created:

<Target Name="BeforeBuild"> <MSBuild Targets="CheckAndMinifyJS" Projects="$(MSBuildProjectFile)" /> </Target> <ItemGroup> <JS Include="$(ProjectDir)\**\*.js" /> </ItemGroup> <Target Name="CheckAndMinifyJS" Inputs="@(JS)" Outputs="@(JS->'$(ProjectDir)%(RecursiveDir)%(Filename).min.js')"> <AjaxMin JsSourceFiles="@(JS)" JsSourceExtensionPattern="\.js$" JsTargetExtension=".min.js" /> </Target> <UsingTask TaskName="AjaxMin" AssemblyFile="..\..\ThirdParty\AjaxMinTask.dll" /> 

This works fine, but it has a side effect: when you look at a project in Visual Studio (2015), all JS files look duplicated (the same path, but different assembly steps):

Duplicate items

I would like to avoid having an element with the JS build action appearing in the project. How can i do this?

Please note that several developers work with the project, so any proposed solution should be contained inside a .csproj or solution (for example: it is unacceptable to ask all developers to change their registry in order to change the default action for the assembly for JS files).

+5
source share
1 answer

To hide your ItemGroup from Visual Studio, move it to an intermediate Target . In addition to this change, the following code filters existing Content elements, rather than recursing the file system again. This ensures that you do not get extraneous .js files (for example, intermediate output files in obj\ or .min.js files created by your script but not explicitly added to the project).

  <Target Name="BeforeBuild"> <MSBuild Targets="CheckAndMinifyJS" Projects="$(MSBuildProjectFile)" /> </Target> <Target Name="GetJSItems" BeforeTargets="CheckAndMinifyJS"> <ItemGroup> <JS Include="@(Content)" Condition=" '%(Extension)' == '.js' " /> </ItemGroup> </Target> <Target Name="CheckAndMinifyJS" Inputs="@(JS)" Outputs="@(JS->'$(ProjectDir)%(RecursiveDir)%(Filename).min.js')"> <AjaxMin JsSourceFiles="@(JS)" JsSourceExtensionPattern="\.js$" JsTargetExtension=".min.js" /> </Target> <UsingTask TaskName="AjaxMin" AssemblyFile="..\..\ThirdParty\AjaxMinTask.dll" /> 
+1
source

All Articles