何时使用Requirejs以及何时使用捆绑的javascript? [英] When to use Requirejs and when to use bundled javascript?

查看:105
本文介绍了何时使用Requirejs以及何时使用捆绑的javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于网络人来说,这可能是一个愚蠢的问题。但我对此感到有些困惑。现在,我有一个应用程序,我使用几个Javascript文件来执行不同的任务。现在,我使用Javascript bundler 来组合和缩小所有文件。因此,在运行时只有一个app.min.js文件。现在, Requirejs 用于在运行时加载模块或文件。所以,问题是如果我已将所有内容都放在一个文件中,那么我是否需要requirejs?或者我可以使用requirejs和/或bundler的用例场景是什么?

This may be a dumb question for web guys. But I am a little confused over this. Now, I have an application where I am using a couple of Javascript files to perform different tasks. Now, I am using Javascript bundler to combine and minify all the files. So, at runtime there will be only one app.min.js file. Now, Requirejs is used to load modules or files at runtime. So, the question is if I already have all things in one file, then do I need requirejs? Or what is a use case scenario where I can use requirejs and/or bundler?

如果需要进一步的详细信息,请告诉我。

Please let me know if any further details are needed.

推荐答案

这是许多熟练的JavaScript开发人员中一个备受争议的问题。许多其他语言都有一个编译阶段,整个程序捆绑在一起进行部署(JBoss的.WAR文件会浮现在脑海中)。来自更传统背景的程序员通常喜欢这种方法。

This is a hotly contested issue among many proficient javascript developers. Many other languages have a "compilation" phase where the entire program is bundled up for deployment (JBoss's .WAR files come to mind). Programmers that come from more traditional backgrounds often favor this approach.

Javascript近年来已经出现这样的增长,很难确定最佳实践,但是那些欣赏Javascript的更多功能性通常更喜欢模块加载方法(如require.js使用)。

Javascript has seen such growth in recent years that it is difficult to chart exact best practices, but those that appreciate the more functional nature of Javascript often prefer the module loading approach (like require.js uses).

我写了 Frame.js 的工作原理与require.js非常相似,所以我偏向于模块加载器方法。

I wrote Frame.js which works much like require.js, so my bias is towards the module loader approach.

直接回答你的问题,是的,它是一个或另一个。

To answer your question directly, yes, it is one or the other.

大多数认为将你的脚本打包成一个文件的人认为它可以实现更多压缩,因此更高效。我相信在大多数情况下,包装的效率优势可以忽略不计,因为:(1)模块加载时间分布在整个会话中,(2)单个模块可以压缩到几乎相同的百分比,(3)单个模块可以缓存单独的服务器和路由器,以及(4)仅在需要时加载脚本最终允许您为一些用户加载更少的代码和更多的代码。

Most that argue for packing your scripts into a single file believe it enables more compression and is thus more efficient. I believe the efficiency advantages of packaging are negligible in most cases because: (1) module load times are distributed over the entire session, (2) individual modules can be compressed to nearly the same percentage, (3) individual modules can be cached by the server and routers separately, and (4) loading scripts only when they are needed ultimately allows you load less code for some users and more code overall.

从长远来看,如果你能看到动态脚本加载的优势,请使用它。如果没有,请将脚本捆绑到一个文件中。

In the long run, if you can see an advantage to dynamic script loading use it. If not, bundle your scripts into a single file.

这篇关于何时使用Requirejs以及何时使用捆绑的javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆