3/8/2024 0 Comments Win 32 minify js and css files![]() This is a much more elegant solution and works just as good. ASP.NET Core takes a different approach and promotes the use of post-build commands by utilizing Node modules to minify static assets. Runtime minification is not ideal because it puts additional load on the web server instead of doing the compression on the build server at an earlier stage. ![]() It is not great but does the job for the lazy programmer. The default project template in a Classic ASP.NET MVC web application offers runtime minification via the WebGrease NuGet library. The best way to prevent these type of mistakes is to automate the entire process and reduce the human error. These are typical mistakes which happen with manual tasks and every developer experiences at least once in their career. It is too easy to forget updating a minified file when making some quick changes to the original file, or I'd update the minified file, but forget to swap the file paths in the HTML source code, leaving the live website pointing to the uncompressed version. Minifying a CSS file is one of the most trivial tasks in web development and yet it is often more cumbersome than it has to be. It didn't take me very long before I had to think about minifying static content such as CSS and JavaScript files to speed up page load times for my readers. First there was nothing but my Hello World blog post and only later when I had more content I added more features over time. When I started building this blog I kept things very simple in the beginning. These PowerShell scripts can be invoked as a part of build in your CI and do the CSS and JS minification.Automating CSS and JavaScript minification in ASP.NET MVC 5 with PowerShell $files = get-childitem $folder -recurse -force -include *.js $libPathCompressor = $PSScriptRoot + "\lib\YuiCompressor\" $libPathEcma = $PSScriptRoot + "\lib\YuiCompressor\" You ca download it with NuGet package or you can download it from download section on this article page. ::WriteAllText($file.FullName,$compressedContent)įor JavaScript files, the proses is pretty much the same except we need to create an instance of class and invoke Compress method of it.Īdditionally we also need to load assembly which is required for JavaScriptCompressor.Compress method. Set-ItemProperty $file.FullName -name IsReadOnly -value $false $compressedContent = $cssCompressor.Compress($content) $files = get-childitem $folder -recurse -force -include *.css $libPath = $PSScriptRoot + "\lib\YuiCompressor\" The rest is just looping through all CSS fies in the folder and applying Compress method from loaded assembly. If you prefer not to use paths in PowerShell you can load the assemblies to GAC with gacutil and reference it with Add-Type cmdlet ![]() So first thing is to load the assembly in PowerShell with ::LoadFile. NET library and use it as if you are writing C# code. ![]() The nice past with PowerShell is that you can easily load any. It can be downloaded from the NuGet package page or download it from this article download section in the top right corner along with PoweShell scripts. It handles well both JS and CSS comments and removes all the whitespace without any issues so I decided to use it for PowerShell script as well. I used YUICompressor.NET library for minifying CSS and JS file on the runtime and did not find any issues with it. I had a similar case and that is when I used PowerShell script to minify CSS and JS files on the filesystem where web application was deployed. This is the case where you need automation to perform minification on the production file system which can be then taken to the CDN. However, the other side which is source control needs to keep the original files, so you cannot minify and check-in to your source control. The minification of CSS and JS can be easily one on the runtime with ASP.NET bundling which is build in in ASP.NET framework, but in case of distributing your resource files over CDN you cannot use the runtime minification. Minification of Cascade Style Sheet (CSS) and Java Script (JS) should be on a TODO list for every web application.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |