捆绑和缩小(BundleConfig.RegisterBundles) 第六章 : LESS, CoffeeScript, SCSS, Sass Bundling 和注意事项

??

较少,CoffeeScript、 SCSS,敢亵渎捆绑销售。

捆绑和缩小框架提供一种机制来处理中间语言如SCSS、上海社会科学院、较少或Coffeescript,并适用于产生捆绑的转换,如缩小。例如,要将.less文件添加到您的 MVC 4 项目:

  1. 创建一个文件夹为您较少的内容。下面的示例使用ContentMyLess文件夹。
  2. 添加.less NuGet 包拉丁文到您的项目。
    捆绑和缩小(BundleConfig.RegisterBundles) 第六章 : LESS, CoffeeScript, SCSS, Sass Bundling 和注意事项
  3. 添加一个类实现IBundleTransform接口。.Less 变换,将下面的代码添加到项目中。
  4. 创建与和CssMinify变换较少的文件包。将下面的代码添加到App_StartBundleConfig.cs文件中的方法。
  5. 将下面的代码添加到引用的少捆绑的任何视图。

捆绑软件注意事项

创建包时遵循良好公约 》 是包括”捆绑”作为软件包名称中的前缀。这将防止路由冲突的可能.

一旦您更新包中的一个文件,为捆绑查询字符串参数生成一个新的令牌和充分的捆绑必须下载一个客户端请求的页面包含包的下一次。在传统标记中的每个资产所单独列,仅更改后的文件将被下载。频繁更改的资产捆绑可能不到很好的候选。

捆绑和缩小主要改善的第一页请求加载时间。一旦请求了一个网页,浏览器会缓存 (JavaScript、 CSS 和图像) 的资产所以捆绑和缩小在请求同一页面时,不会提供任何性能提升或页面上相同站点请求同一资产。如果不设置过期标头正确地对您的资产,您不使用捆绑和缩小,浏览器新鲜度试探法将标志着资产陈旧几天后和浏览器的每个资产的需要验证请求。在这种情况下,捆绑和缩小后的第一个页面请求提供性能提高。有关详细信息,请参阅博客使用 Cdn 和过期的改进 Web 站点的性能.

通过使用CDN可以降低浏览器限制的每个主机名每六个同时连接。因为 CDN 将比您承载的网站有一个不同的主机名,从 CDN 资产请求不会计算针对六个同时连接数限制到您的宿主环境。CDN 也可以提供共同包缓存和缓存优势的边缘。

束应由页需要它们进行分区。例如,默认的 internet 应用程序的 ASP.NET MVC 模板创建 jQuery 验证包单独从 jQuery。因为创建的默认视图有没有输入,并且不发布值,它们不包含验证包。

命名空间是在 System.Web.Optimization.DLL 中实现的。它利用了 WebGrease 库 (WebGrease.dll) 对于缩小功能,它反过来使用 Antlr3.Runtime.dll。

原文:

LESS, CoffeeScript, SCSS, Sass Bundling.

The bundling and minification framework provides a mechanism to process intermediate languages such as SCSS, Sass, LESS or Coffeescript, and apply transforms such as minification to the resulting bundle. For example, to add .less files to your MVC 4 project:

  1. Create a folder for your LESS content. The following example uses the ContentMyLess folder.
  2. Add the .less NuGet package dotless to your project.
    捆绑和缩小(BundleConfig.RegisterBundles) 第六章 : LESS, CoffeeScript, SCSS, Sass Bundling 和注意事项
  3. Add a class that implements the IBundleTransform interface. For the .less transform, add the following code to your project.
  4. Create a bundle of LESS files with the and the CssMinify transform. Add the following code to the method in the App_StartBundleConfig.cs file.
  5. Add the following code to any views which references the LESS bundle.

Bundle Considerations

A good convention to follow when creating bundles is to include “bundle” as a prefix in the bundle name. This will prevent a possible routing conflict.

Once you update one file in a bundle, a new token is generated for the bundle query string parameter and the full bundle must be downloaded the next time a client requests a page containing the bundle. In traditional markup where each asset is listed individually, only the changed file would be downloaded. Assets that change frequently may not be good candidates for bundling.

Bundling and minification primarily improve the first page request load time. Once a webpage has been requested, the browser caches the assets (JavaScript, CSS and images) so bundling and minification won’t provide any performance boost when requesting the same page, or pages on the same site requesting the same assets. If you don’t set the expires header correctly on your assets, and you don’t use bundling and minification, the browsers freshness heuristics will mark the assets stale after a few days and the browser will require a validation request for each asset. In this case, bundling and minification provide a performance increase after the first page request. For details, see the blog Using CDNs and Expires to Improve Web Site Performance.

The browser limitation of six simultaneous connections per each hostname can be mitigated by using a CDN. Because the CDN will have a different hostname than your hosting site, asset requests from the CDN will not count against the six simultaneous connections limit to your hosting environment. A CDN can also provide common package caching and edge caching advantages.

Bundles should be partitioned by pages that need them. For example, the default ASP.NET MVC template for an internet application creates a jQuery Validation bundle separate from jQuery. Because the default views created have no input and do not post values, they don’t include the validation bundle.

The namespace is implemented in System.Web.Optimization.DLL. It leverages the WebGrease library (WebGrease.dll) for minification capabilities, which in turn uses Antlr3.Runtime.dll.

来源:zhou44129879

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2013年10月16日
下一篇 2013年10月17日

相关推荐