In Visual Studio 2013, how do I minify Javascript and CSS in the post-build step

前端 未结 2 982
名媛妹妹
名媛妹妹 2021-02-06 02:49

In Visual Studio 2013, how do I minify Javascript and CSS in the post-build step? I\'d like to have every single css and js file compress into a .min.js, or .min.css in the sam

2条回答
  •  不知归路
    2021-02-06 03:19

    All solutions I found required using different filenames for the minimized versions, and a lot of extra work to switch between using the normal/minified versions.

    Instead, I wanted the compressed JavaScript files to have the original names so I didn't have to change the references in my HTML markup. I could use the normal Javascript files in my development environment, then minimized versions would be automatically deployed when publishing.

    I found a simple solution that does just that.

    First, install Microsoft Ajax Minifier.

    Then, in your Visual Studio project file, just before the closing tag add the following :

    
    
      
        
      
      
      
    
    
      
        CustomCollectFiles;
        $(CopyAllFilesToSingleFolderForPackageDependsOn);
      
    
    
      
        
        
          %(RecursiveDir)%(Filename).js
        
        
        
          %(RecursiveDir)%(Filename).css
        
      
    
    

    What does the above code do? When you publish in Visual Studio, this code will find every .js and .css file in your source, and create a minified copy using the extension .jsMIN and .cssMIN. It will ignore files that are already minified. Then it will copy these minified files to the deployment folder, using the original file names.

    Voilà! You just published minified JS/CSS files, while your original files stay intact on your development environment.

    Optional:
    Want Ajax Minifier to be packaged with your project? From the Ajax Minifier install folder, you can move AjaxMin.dll and AjaxMinTask.dll directly into your source directory. I put them in my App_Data folder. Once they're somewhere in your source, in Visual Studio right-click them, select Include in Project, and also change their Build Action property to None.

    Then in the code I included above, change

    to

    Done.

    A troubleshooting tip:
    My main code above executes AfterBuild and only when the configuration is Release. That's so it will only run during a publish. If your configuration is named something else, or you want it to run in other circumstances, modify the code as needed.

提交回复
热议问题