简体   繁体   English

要求JS(AMD)在后台将文件最小化为单个文件

[英]Require JS(AMD) working under the hood on minification of files in to a single file

Require.js looks like a perfect solution for having a module based code.As it is mentioned in its website it loads the dependencies on demand(So its faster).But when we use their r.js plugin to minify the code base in to single file , it loads the entire combined file. Require.js看起来像是一个基于模块的代码的完美解决方案,正如其网站上所提到的那样,它可以按需加载依赖项(因此速度更快),但是当我们使用其r.js插件来缩小代码库时,单个文件,它将加载整个组合文件。

So does that mean it is a normal async load and not on demand load? 那么这是否意味着这是正常的异步负载而不是按需负载? or will r.js has any optimization technique internally to do this efficiently? 还是r.js内部有任何优化技术可以有效地做到这一点?

Loading individual JS modules on demand is a bit of a red herring because it's often a better idea to put all your code into a single minified bundle even if most users will only use a subset of it. 按需加载单个JS模块有点麻烦,因为将大多数代码放在一个最小的包中通常是一个更好的主意,即使大多数用户只会使用它的一个子集。 GZipping your resources will reduce size of your scripts by further 33%-50% (at least) so you may end up with a total that's smaller than a single JPEG file (depending on how big your project is, of course). GZipping您的资源将使脚本的大小进一步减少33%-50%(至少),因此最终您的总大小可能会比单个JPEG文件小(当然,这取决于项目的大小)。

When loading individual files suddenly the network itself becomes the biggest bottleneck - the browser limits the number of parallel downloads (so you end up loading files sequentially) and there's the connection negotiation overhead for each resource. 当突然加载单个文件时,网络本身成为最大的瓶颈-浏览器限制了并行下载的数量(因此您最终会依次加载文件),并且每种资源都有连接协商的开销。 Together with the fact that it complicates r.js configuration I'd say on-demand loading should be considered only when the modules/libraries are really big and are only used by a tiny fraction of users. 再加上它使r.js配置变得复杂的事实,我想说只有在模块/库确实很大并且仅一小部分用户使用模块时,才应考虑按需加载。

The real benefit of AMD modules isn't actually asynchronous loading but code organisation - with neatly organised modules with well-defined dependencies it's much harder to write bad or untestable code. AMD模块的真正好处不是实际上是异步加载,而是代码的组织-带有定义良好的依存关系的组织整齐的模块,很难编写糟糕或无法测试的代码。

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

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