正确管理大型网站/代码串联的 JavaScript

发布于 2024-10-10 08:09:13 字数 513 浏览 4 评论 0原文

我正在开发的网站有大量在各种功能之间共享的 javascript,以及同样大量的特定于功能的 javascript。我读过所有关于使用一个整体 javascript 文件与许多较小的文件的内容。

就我的目的而言,大文件方法不仅会导致脚本难以维护,而且还会包含大量不需要的 JavaScript。同时,分离 javascript 以便仅包含所需的代码会导致文件/HTTP 请求数量过多。除了给用户带来额外的文件大小开销之外,即使包含适量不需要的代码的想法似乎也与正确软件设计的概念相悖。

我找到了 Apache 的 mod_concat 模块,它似乎可以完全解决我的问题 - 我可以将我的 javascript 分成我想要的任意多个文件,只包含那些必要的文件,并且几乎不会影响性能。

事实真的是这样吗?唯一的潜在缺点是需要管理许多文件吗?我知道 mod_concat 并没有永远存在,所以我也在寻找一些背景知识:a)以前是如何处理的,b)即使使用代码串联,包括适量不需要的 javascript 也被认为是可以接受的(甚至是最佳实践)。

谢谢,布莱恩

The site I am developing has a large amount of javascript that is shared across various functionality, and an equally large amount of feature-specific javascript. I've read all about using one monolithic javascript file vs. many smaller ones.

For my purposes, the huge-file approach would not only result in a script difficult to maintain, but contain a lot of unneeded javascript as well. At the same time, separating the javascript so that only the required code is included would result in an excessive number of files / HTTP requests. The idea of including even a moderate amount of unneeded code seems contrary to the concepts of proper software design, besides the additional file size overhead for the user.

I have found the mod_concat module for Apache which seems like it would solve my problem entirely - I could separate my javascripts into as many files as I want, include only those necessary, and take almost no hit on performance.

Is this actually the case? Is the only potential drawback the need to manage many files? I know mod_concat has not been around forever, so I'm also looking for a bit of background on a) how this was handled before, and b) if, even with code concatenation, including a moderate amount of unneeded javascript is considered acceptable (or even a best practice).

Thanks, Brian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭⌒浅淡时光〆 2024-10-17 08:09:13

我认为你不需要 apache 模块。为生产创建一个缩小的 JS 文件应该是最好的方法,因为它仅加载一次,然后由浏览器缓存。当然,对于开发而言,将应用程序拆分为单独的文件是有意义的。

我个人最喜欢的 JavaScript 模块管理和压缩是 Steal JS,它是伟大的 < a href="http://javascriptmvc.com/" rel="nofollow">JavaScript MVC 框架(对于较大的 JS 应用程序来说通常会很有趣)。它可以在开发过程中动态加载模块文件,对于生产,您可以创建一个压缩的 JavaScript 文件(它也可以执行 CSS)。

另一种选择是 RequireJS 但我只快速浏览了一下。

I don't think you need an apache module for that. Creating one minified JS file for production should be the best way to go, because it is only loaded once and then cached by the browser. Although for development of course, it makes sense to have your application split into separate files.

My personal favorite for JavaScript module management and compression is Steal JS which is part of the great JavaScript MVC framework (could be generally interesting for larger JS applications). It can load module files dynamically during development and for production you can create one compressed JavaScript file (it can do CSS, too).

Another alternative is RequireJS but I only had a quick look at it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文