当前位置: 动力学知识库 > 问答 > 编程问答 >

visual studio 2012 - Minifying JS/CSS for Web Deploy

问题描述:

I'm trying to Minify Javascript and CSS using AjaxMin when I deploy using a Web Deploy Publish Profile. Here is what I have in the project file:

<Import Project="$(MSBuildExtensionsPath)\Microsoft\MicrosoftAjax\AjaxMin.tasks" />

<PropertyGroup>

<ResGenDependsOn>

MinifyJavascriptAndCss;

$(ResGenDependsOn);

</ResGenDependsOn>

</PropertyGroup>

<Target Name="MinifyJavascriptAndCss"

Condition=" '$(ConfigurationName)'=='Release' ">

<ItemGroup>

<JS Include="$(_PackageTempDir)\**\*.js"

Exclude="$(_PackageTempDir)\**\*.min.js;Scripts\*.js" />

</ItemGroup>

<ItemGroup>

<CSS

Include="$(_PackageTempDir)\**\*.css"

Exclude="$(_PackageTempDir)\**\*.min.css" />

</ItemGroup>

<Message Text="Compressing JavaScript and CSS files into $(_PackageTempDir)"

Importance="high" />

<AjaxMin JsSourceFiles="@(JS)" JsSourceExtensionPattern="\.js$"

JsTargetExtension=".min.js" CssSourceFiles="@(CSS)"

CssSourceExtensionPattern="\.css$" CssTargetExtension=".min.css" />

</Target>

If I watch the output directory I can see that the files are minified as the min.* files appear, but when the package file is deployed, they are not included.

How do I force the minified files to be included in the publish package?

网友答案:

It worked for me in VS2010, but does nothing with VS2012...

<!-- Use AjaxMinifier from Libs folder in this project -->
      <UsingTask TaskName="AjaxMin" AssemblyFile="$(MSBuildProjectLocation)Libs\AjaxMinTask.dll" />
      <!-- This target will run after publish web in Release mode -->
      <Target Name="MinifyJavaScriptAndCSS" AfterTargets="CopyAllFilesToSingleFolderForPackage" Condition="'$(Configuration)'=='Release'">
        <ItemGroup>
          <!-- Every .js file (exclude *.min.js and *.vsdoc.js files) -->
          <JS Include="$(_PackageTempDir)\**\*.js" Exclude="$(_PackageTempDir)\**\*.min.js;$(_PackageTempDir)\**\*vsdoc.js" />
          <!-- Every .css file (exclude *.min.css files) -->
          <CSS Include="$(_PackageTempDir)\**\*.css" Exclude="$(_PackageTempDir)\**\*.min.css" />

        </ItemGroup>
        <!-- Log in output build window -->
        <AjaxMin JsKnownGlobalNames="jQuery,$" JsSourceFiles="@(JS)" JsSourceExtensionPattern="\.js$" JsTargetExtension=".js" CssSourceFiles="@(CSS)" CssSourceExtensionPattern="\.css$" CssTargetExtension=".css" />
        <!-- Log in output build window -->
        <Message Text="[pcv] $(MSBuildProjectName) -&gt; Minified: @(JS)" Importance="high" />
        <Message Text="[pcv] $(MSBuildProjectName) -&gt; Minified: @(CSS)" Importance="high" />

      </Target>
网友答案:

I faced the same problem and successfully resolved it. There is one easy way to do this task.

Set the Build Action of original CSS/JS files to None and set the Build Action of minify files to Content. Now when you build the project then only minified css/java script files will come.

网友答案:

I'm using VS2012, and this worked for me

<!--<Import Project="$(MSBuildExtensionsPath)\Microsoft\MicrosoftAjax\AjaxMin.targets" />-->
  <UsingTask TaskName="AjaxMin" AssemblyFile="$(MSBuildProjectDirectory)\..\packages\AjaxMin.5.14.5506.26202\tools\net40\AjaxMinTask.dll" />
  <Target Name="MinifyJsAndCss"  AfterTargets="CopyAllFilesToSingleFolderForPackage" >
    <ItemGroup>
      <JS Include="$(_PackageTempDir)\App_Scripts\**\*.js" Exclude="$(_PackageTempDir)\**\*.min.js" />
      <CSS Include="$(_PackageTempDir)\**\*.css"  Exclude="$(_PackageTempDir)\**\*.min.css" />
    </ItemGroup>
    <Message Text="Compressing JavaScript and CSS files...(to edit this feature, unload the project, right click it ->edit -> search for 'AjaxMin' bottom of the xml)" Importance="high" />
    <AjaxMin JsSourceFiles="@(JS)" JsSourceExtensionPattern="\.js$" JsTargetExtension=".js" CssSourceFiles="@(CSS)" CssSourceExtensionPattern="\.css$" CssTargetExtension=".min.css" />
  </Target>
分享给朋友:
您可能感兴趣的文章:
随机阅读: