在VisualStudio 2012中通过SmallSharp压缩js及修改web.config

  1. 在项目中加入一个targets文件,取名my.build.targets
  2. 在targets文件中加入内容:
    <?xml version="1.0" encoding="utf-8" ?> 
    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <ItemGroup>
        <CityIndexJS Include="js/src/cityIndex/_*.js;js/src/cityIndex/i*.js;js/src/cityIndex/page.js;js/src/cityIndex/ready.js;" />
      </ItemGroup>
    <Target Name="BuildJS"  Condition="'$(Configuration)' == 'Release'">
        <Message Text="Building javascript files…… haha"></Message>
        <Packer OutputFileName="jscityindex.js" Mode="JSMin" InputFiles="@(CityIndexJS)" Verbose="true" />
    </Target>
      <PropertyGroup>
        <WebConfigPath>$(SampleFilesDir)Web.config</WebConfigPath>
      </PropertyGroup>
      <Target Name="UpdateReleaseWebConfig" Condition="'$(Configuration)' == 'Release'">
        <Message Text="Updating web config…… haha"></Message>
        <UpdateWebConfig
            WebConfigPath="web.config"
            Debug="false"
            CustomErrorsMode="Remote"/>
      </Target>
    </Project>

  3. 将SmallSharp文件拷到一个目录中,比如MyLib.dllmsbuildsmallsharp   下载地址
  4. unload project , 并编辑项目文件
  5. 在项目文件的顶部加入
      <Import Project="..文档MSBuildSmallSharpMSBuild.Packer.Targets" />
      <Import Project="js.build.targets" />
    在项目文件底部加入
    <Target Name="AfterBuild">
        <CallTarget Targets="UpdateReleaseWebConfig" />
        <CallTarget Targets="BuildJS" />
      </Target>

  6. 使用msbuild执行,因为这里使用了Configuration=release的模式,所以需要指定参数
    msbuild my.csproj /p:Configuration=release   
    这个vs中直接编译不会执行压缩,只有通过msbuild才会执行
原文地址:https://www.cnblogs.com/dyllove98/p/3226239.html