简体   繁体   English

缩小和组合.net中的文件

[英]Minifying and combining files in .net

I am looking at implementing some performance optimization around my javascript/css. 我正在寻找围绕我的javascript / css实现一些性能优化。 In particular looking to achieve the minification and combining of such. 特别是寻求实现这种缩小和组合。 I am developing in .net/c# web applications. 我正在使用.net / c #Web应用程序开发。

I have a couple of options and looking for feedback on each: 我有几个选项,并寻找每个方面的反馈:

First one is this clever tool I came across Chirpy which via visual studio combines, minifies etc -> http://chirpy.codeplex.com/ This is a visual studio add in but as I am in a team environment, this tool isnt ideal. 第一个是我遇到的这个聪明的工具Chirpy通过视觉工作室结合,缩小等 - > http://chirpy.codeplex.com/这是一个视觉工作室添加但是因为我在团队环境中,这个工具不理想。

My next option is to use an Msbuild task ( http://yuicompressor.codeplex.com/ ) to minify the files and also combine them (maybe read from an xml file what needs to be combined). 我的下一个选择是使用Msbuild任务( http://yuicompressor.codeplex.com/ )来缩小文件并将它们组合起来(可能从xml文件中读取需要组合的内容)。 While this works for minifying fine, the concern I have is that I will have to maintain what must be combined which could be a headache. 虽然这对于缩小罚款很有用,但我担心的是我必须保持必须结合的东西,这可能是一件令人头疼的问题。

3rd option is to use msbuild task just for the minifying and at runtime using some helper classes, combine the files on a per page basis. 第三个选项是使用msbuild任务仅用于缩小,在运行时使用一些帮助程序类,在每页的基础上组合文件。 This would combine the files, give it a name and add a version to it. 这将合并文件,为其命名并为其添加版本。

Any other options I could consider? 我可以考虑其他任何选择吗? My concern with the last option is that it may have performance issues as I would have to open the file from the local drive, read its contents and then combine the files. 我对最后一个选项的关注是它可能有性能问题,因为我必须从本地驱动器打开文件,读取其内容然后合并文件。 This is alot of processing at run time. 这在运行时很多处理。 I was looking at something like Squishit - https://github.com/jetheredge/SquishIt/downloads This minifies the files at run time but I would look at doing this at compile time. 我正在寻找类似Squishit的东西 - https://github.com/jetheredge/SquishIt/downloads这会在运行时缩小文件,但我会在编译时看看这样做。

So any feedback on my approaches would be great? 那么对我的方法的任何反馈都会很棒吗? If the 3rd option would not cause performance issues, I am leading towards it. 如果第三种选择不会导致性能问题,我就会引领它。

We have done something similar with several ASP.NET web applications. 我们已经使用几个ASP.NET Web应用程序做了类似的事情。 Specifically, we use the Yahoo Yui compressor , which has a .NET library version which you can reference in your applications. 具体来说,我们使用Yahoo Yui压缩器 ,它具有.NET库版本,您可以在应用程序中引用它。

The approach we took was to generate the necessary merged/minified files at runtime. 我们采用的方法是在运行时生成必要的合并/缩小文件。 We wrapped all this logic up into an ASP.NET control, but that isn't necessary depending on your project. 我们将所有这些逻辑包装到ASP.NET控件中,但根据您的项目,这不是必需的。

  • The first time a request is made for a page, we process through the list of included JS and CSS files. 第一次对页面发出请求时,我们会处理包含的JS和CSS文件列表。 In a separate thread (so the original request returns without delay) we then merged the included files together (1 for JS, 1 for CSS), and then apply the Yui compressor. 在一个单独的线程中(所以原始请求会毫不拖延地返回)然后我们将包含的文件合并在一起(1代表JS,1代表CSS),然后应用Yui压缩器。
  • The result is then written to disk for fast reference in the future 然后将结果写入磁盘以便将来快速参考
  • On subsequent requests, the page first looks for the minified versions. 在后续请求中,页面首先查找缩小版本。 If found, it just serves those up. 如果找到,它只是为那些服务。 If not, it goes through the process again. 如果没有,它会再次经历这个过程。

As some icing to the cake: 作为一些锦上添花:

  • For debug purposes, if the query string ?debug=true is present, the merged/minified resources are ignored and the original individual files are served instead (since it can be hard to debug optimized JS) 出于调试目的,如果存在查询字符串?debug = true,则忽略合并/缩小的资源,而是提供原始的单个文件(因为它可能很难调试优化的JS)

We have found this process to work exceptionally well. 我们发现这个过程非常好用。 We built it into a library so all our ASP.NET sites can take advantage. 我们将它构建到一个库中,这样我们所有的ASP.NET站点都可以利用它们。 The post-build scripts can get complicated if each page has different dependencies, but the run-time can determine this quite easily. 如果每个页面具有不同的依赖关系,那么构建后的脚本会变得复杂,但运行时可以很容易地确定这一点。 And, if someone needs to make a quick fix to a CSS file, they can do so, delete the merged versions of the file, and the process will automatically start over without need to do post-build processing with MSBuild or NAnt. 并且,如果有人需要快速修复CSS文件,他们可以这样做,删除文件的合并版本,并且该过程将自动重新开始,而无需使用MSBuild或NAnt进行后期构建处理。

RequestReduce provides a really nice solution for combining and minifying javascript and css at run time. RequestReduce为在运行时组合和缩小javascript和css提供了一个非常好的解决方案。 It will also attempt to sprite your background images. 它还会尝试精灵您的背景图像。 It caches the processed files and serves them using custom ETags and far future headers. 它缓存已处理的文件,并使用自定义ETag和远期标头为它们提供服务。 RequestReduce uses a response filter to transform the content so no code or configuration is needed for basic functionality. RequestReduce使用响应过滤器来转换内容,因此基本功能不需要代码或配置。 It can be configured to work in a web farm environment and sync content accross several servers and can be configured to point to a CDN. 它可以配置为在Web场环境中工作,并跨多个服务器同步内容,并且可以配置为指向CDN。 It can be downloaded at http://www.RequestReduce.com or from Visual Studio via Nuget. 它可以从http://www.RequestReduce.com下载,也可以通过Nuget从Visual Studio下载。 The source is available at https://github.com/mwrock/RequestReduce . 该源代码位于https://github.com/mwrock/RequestReduce

have you heard of Combres ? 你听说过Combres吗? go to : http://combres.codeplex.com and check it out 请访问: http//combres.codeplex.com并查看它

it minifies your CSS and JS files at Runtime meaning you can change any file and upload it and each request the client does it minifies it. 它会在运行时缩小您的CSS和JS文件,这意味着您可以更改任何文件并上传它,客户端做的每个请求都会缩小它。 all you gotta do is add the files u wanna compress to a list in the combres XML file and just call the list from your page / masterpage. 你要做的就是添加你想要压缩到combres XML文件中的列表的文件,然后从你的页面/母版页调用列表。

if you are using VS2010 you can easily install it on your project using NuGet here's the Combres NuGet link: http://combres.codeplex.com/wikipage?title=5-Minute%20Quick%20Start 如果您使用的是VS2010,您可以使用NuGet在Combres NuGet链接上轻松安装它: http ://combres.codeplex.com/wikipage?title = 5 -Minute%20Quick%20Start

I did a really nice solution to this a couple of years back but I don't have the source left. 几年前我做了一个非常好的解决方案,但我没有留下来源。 The solution was for webforms but it should work fine to port it to MVC. 解决方案是针对webforms,但它应该可以正常工作将其移植到MVC。 I'll give it a try to explain what I did in some simple step. 我会尝试用一些简单的步骤来解释我的所作所为。 First we need to register the scripts and we wrote a special controller that did just that. 首先,我们需要注册脚本,然后我们编写了一个特殊的控制器。 When the controller was rendered it did three things: 当控制器被渲染时,它做了三件事:

  1. Minimize all the files, I think we used the YUI compression 最小化所有文件,我认为我们使用了YUI压缩
  2. Combine all the files and store as string 合并所有文件并存储为字符串
  3. Calculate a hash for the string of the combined files and use that as a virtual filename. 计算组合文件字符串的哈希值,并将其用作虚拟文件名。 You store the string of combined files in a cached dictionary on the server with the hash value as key, the html that is rendered needs to point to a special folder where the "scripts" are located. 您将组合文件的字符串存储在服务器上的缓存字典中,并将哈希值作为键,呈现的html需要指向“脚本”所在的特殊文件夹。

The next step is to implement a special HttpHandler that handles request for files in the special folder. 下一步是实现一个特殊的HttpHandler来处理特殊文件夹中的文件请求。 When a request is made to that special folder you make a lookup in the cached dictionary and returns the string bascially. 当对该特殊文件夹发出请求时,您在缓存的字典中进行查找并返回字符串。

One really nice feature of this is that the returned script is always valid so the user will never have to ask you for an update of the script. 一个非常好的功能是返回的脚本始终有效,因此用户永远不必要求您更新脚本。 The reason for that is when you make a change to any of the script files the hash value will change and the client will ask for a new script. 原因是当您对任何脚本文件进行更改时,哈希值将更改,并且客户端将要求新脚本。

You can use this for css-files as well with no problems. 你可以将它用于css文件,没有任何问题。 I remebered making it configurable so you could turn off combine files, minimize files, or just exclude one file from the process if you wanted to do some debugging. 我重新编程使其可配置,以便您可以关闭合并文件,最小化文件,或者如果您想进行一些调试,只需从过程中排除一个文件。

I might have missed some details, but it wasn't that hard to implement and it turned out very well. 我可能已经错过了一些细节,但实施起来并不难,结果非常好。

Update: I've implemented a solution for MVC and released it on nuget and have the source up on github . 更新:我已经为MVC实现了一个解决方案,并在nuget上发布了它,并在github上提供了源代码。

Microsoft's Ajax minifier is suprisingly good as a minification tool. 微软的Ajax缩小器作为缩小工具是令人惊讶的好。 I wrote a blog post on combining files and using their minifier in a javascript and stylesheet handler: 我写了一篇关于组合文件和在javascript和样式表处理程序中使用缩小器的博客文章:

http://www.markistaylor.com/javascript-concatenating-and-minifying/ http://www.markistaylor.com/javascript-concatenating-and-minifying/

I needed a solution for combining/minifying CSS/JS on a .NET 2.0 web app and SquishIt and other tools I found weren't .NET 2.0-compatible, I created my own solution that uses a syntax similar to SquishIt but is compatible with .NET 2.0. 我需要一个在.NET 2.0 Web应用程序上结合/缩小CSS / JS的解决方案,而SquishIt和我发现的其他工具不兼容.NET 2.0,我创建了自己的解决方案,使用类似于SquishIt的语法但是兼容.NET 2.0。 Since I thought other people might find it useful I put it up on Github. 因为我认为其他人可能会发现它很有用,我把它放在Github上。 You can find it here: https://github.com/AlliterativeAlice/simpleyui 你可以在这里找到它: https//github.com/AlliterativeAlice/simpleyui

It's worthwhile combining the files at run time to avoid having to synchronise new versions. 在运行时组合文件是值得的,以避免同步新版本。 However, once they are programmatically combined, cache them to disk. 但是,一旦以编程方式组合它们,就将它们缓存到磁盘。 Then the code which runs each time the files are fetched need only check that the files haven't changed before serving the cached version. 然后,每次获取文件时运行的代码只需要在提供缓存版本之前检查文件是否未更改。

If they have changed, then the compression code can run as a one-off. 如果它们已更改,则压缩代码可以作为一次性运行。 Whilst there will be a slight performance cost, you will also receive a performance benefit from fewer file requests. 虽然会有轻微的性能成本,但您也可以从较少的文件请求中获得性能优势。

This is the approach that the Minify tool uses to compress JS/CSS, which has worked really well for me. 这是Minify工具用于压缩JS / CSS的方法,这对我来说非常有用。 It's Linux/PHP only, but you might get some more ideas there too. 它只是Linux / PHP,但你也可能会得到更多的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM