JavaScript minifier (e.g. YUI) integrated to ASP.N

2019-07-25 12:11发布

问题:

I have a javascript file that I minify using the Yahoo YUI. When I 'Publish' the web application project, I want it to only copy the .min.js file and not the original one too. I can use a pre-build event to create the minified version, but how do I prevent 'Publish' from copying the non minify file?

I suppose I can 'exclude' the original non minified file from the project, but that would not be helpful because I want to edit the script file through VS 2010, but when I publish I want the minifier to run first and then publish only that.

Any pointers appreciated

回答1:

If it's a web application (and not a web site), just change the Build Action to None in the properties window. This will prevent the original file from being copied when the app is published.



回答2:

You can use MSBuild Community Task project and the Exec task to do this. Here is an example.

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <yuiCompressor>java -jar Libraries\yuicompressor-2.3.6.jar</yuiCompressor>
  </PropertyGroup>
  <PropertyGroup>
    <Major>1</Major>
    <Minor>0</Minor>
    <Build>0</Build>
    <Revision>1</Revision>
    <BindMinor>0</BindMinor>
    <BindBuild>0</BindBuild>
    <BindRevision>0</BindRevision>
  </PropertyGroup>
  <ItemGroup>
    <CssFiles Include="css\site.css" />
    <CssFiles Include="css\gray.css" />
    <JsFiles Include="scripts\base.js" />
    <JsFiles Include="scripts\lib.js" />
    <JsFiles Include="scripts\project.js" />
  </ItemGroup>
  <Target Name="Minimize" DependsOnTargets="Version">
    <!-- CSS Merge and Minimize -->    
    <Merge Mode="TextLine"
      SourceFiles="@(CssFiles)"
      DestinationFile="merged.css" />
    <Exec Command="$(yuiCompressor) --type css merged.css -o css\project-$(Revision).css" />
    <!-- js Merge and Minimize -->
    <Merge Mode="TextLine"
      SourceFiles="@(JsFiles)"
      DestinationFile="merged.js" />
    <Exec Command="$(yuiCompressor) --type js merged.js -o scripts\project-$(Revision).js" />
    <Delete Files="merged.css" />
    <Delete Files="merged.js" />
  </Target>
</Project>