热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

捆绑,RequireJS和CDN-他们互相取消了吗?-Bundling,RequireJS,andCDNs-Dotheycanceleachotherout?

Iknowwhatyoureallthinking,canthejustuseall3?Yes,Icould,butmyquestionisthis:do

I know what you're all thinking, can't he just use all 3? Yes, I could, but my question is this: do they not cancel each other out?

我知道你们都在想什么,他不能只使用全部3个吗?是的,我可以,但我的问题是:他们不会互相取消吗?

tl;dr

  1. Does bundling make RequireJS obsolete?
  2. 捆绑使RequireJS过时吗?

  3. Does bundling make CDNs obsolete?
  4. 捆绑是否会使CDN过时?

  5. If I have to make a choice between bundling or RequireJS+CDN, how do I determine which to use? Is it best practice to CDN common files, and bundle uncommon? What about SPA files?
  6. 如果我必须在捆绑或RequireJS + CDN之间做出选择,我该如何确定使用哪个?是CDN常用文件的最佳实践,并且捆绑不常见? SPA文件怎么样?

The long version

长版

For example, we bundle files for fewer HTTP requests. Whereas RequireJS is used so that files can be loaded dynamically and asynchronously, as they're needed.

例如,我们捆绑文件以减少HTTP请求。而使用RequireJS,以便可以根据需要动态和异步地加载文件。

So if we bundle all our JS files into 1, is RequireJS even needed?

因此,如果我们将所有JS文件捆绑为1,甚至需要RequireJS吗?

Also, we use a CDN so the browser can access a cached version, so if the user visits 5 websites, all of which use a popular jQuery CDN - i.e. http://cdn.com/jquery.min.js - the browser actually needs to make no HTTP requests for this file on the last 4 sites, which is much faster than that file getting bundled. If those sites were to instead each access a unique bundled file from the CDN - specific to their needs - the odds of a cache "hit" become much lower. And, since the odds of a "hit" are lower, the file is less likely to be cached for even the people using that site.

此外,我们使用CDN,因此浏览器可以访问缓存版本,因此如果用户访问5个网站,所有这些网站都使用流行的jQuery CDN - 即http://cdn.com/jquery.min.js - 浏览器实际上需要在最后4个站点上不对此文件发出HTTP请求,这比捆绑的文件快得多。如果这些网站每个都从CDN访问一个独特的捆绑文件 - 特定于他们的需求 - 缓存“命中”的几率变得低得多。而且,由于“点击”的几率较低,即使使用该网站的人也不太可能缓存该文件。

So does it really help for a bundled file to even be on a CDN?

那么捆绑文件甚至可以在CDN上真的有帮助吗?

2 个解决方案

#1


Whereas RequireJS is used so that files can be loaded dynamically and asynchronously, as they're needed.

而使用RequireJS,以便可以根据需要动态和异步地加载文件。

Many developers use RequireJS to have a clean way to separate concerns without the need for loading modules dynamically. I have a few applications and libraries developed with RequireJS. One of them is a library that does not use the dynamic loading capabilities at all and would not benefit from using them. In almost all use-case scenarios for this library, it should be loaded as a single bundle, and not as a collection of modules, one-by-one.

许多开发人员使用RequireJS来分离关注点,而无需动态加载模块。我有一些使用RequireJS开发的应用程序和库。其中之一是一个根本不使用动态加载功能的库,并且不会从使用它们中受益。在这个库的几乎所有用例场景中,它应该作为单个包加载,而不是作为模块的集合一个接一个地加载。

This brings me to another thing: the decision is not between bundling or RequireJS. You can use r.js (RequireJS' optimizer) to bundle your RequireJS modules into a single file for production. This could include 3rd party modules if you want. Or you could leave them out to be served from a CDN. An application can be optimized to a single bundle, or a series of bundles. As any other optimization question, what should be done depends on the architecture of the specific application being considered.

这让我想到了另一件事:决定不是在捆绑或RequireJS之间。您可以使用r.js(RequireJS'优化器)将RequireJS模块捆绑到单个文件中进行生产。如果您愿意,这可能包括第三方模块。或者你可以把它们留在CDN服务。应用程序可以针对单个捆绑包或一系列捆绑包进行优化。与任何其他优化问题一样,应该做什么取决于所考虑的特定应用程序的体系结构。

The rest of your question is wholly opinion-based. I've seen detailed arguments supported by statistics that you should use a CDN, that you should not use a CDN, and everything in between. It is an optimization question, which, again, depends on the specifics of the application being considered.

你的其余部分完全基于意见。我已经看到统计数据支持的详细论据,你应该使用CDN,你不应该使用CDN,以及介于两者之间的所有内容。这是一个优化问题,它同样取决于所考虑的应用程序的细节。

#2


So if we bundle all our JS files into 1, is RequireJS even needed?

因此,如果我们将所有JS文件捆绑为1,甚至需要RequireJS吗?

Not all of it. You might be interested in almond.

不是全部。您可能对杏仁感兴趣。

A replacement AMD loader for RequireJS. It provides a minimal AMD API footprint that includes loader plugin support. Only useful for built/bundled AMD modules, does not do dynamic loading.

RequireJS的替代AMD加载程序。它提供了最小的AMD API占用空间,包括加载程序插件支持。仅对构建/捆绑的AMD模块有用,不进行动态加载。


推荐阅读
author-avatar
李慧颖yynd_613
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有