JavaScript 构建选项/工具
这些天我发现自己将越来越多的工作转移到客户端,因此我的 JS 文件往往变得越来越大。 我发现大多数 HTML 页面的标头中都有六个或更多的 JS 导入,我意识到这会影响加载时间。
我最近发现了这个 脚本,它可以让你通过一个 HTTP 请求下载多个 JS 文件。 它是用 PHP 编写的,作为 Django 粉丝,我计划用 Python 重写它。 我计划使用 HTTP 重定向到预先缩小并连接的文件,并且想知道 301 的成本是多少。 如果这是一个愚蠢的想法,请告诉我。
另一方面,我不太担心将脚本逻辑引入到静态文件的服务中,我想知道是否有一个可行的构建替代方案,例如,一个 ant 任务,它连接和缩小 JS 文件并替换 HTML 中的多个 JS 下载标题有一个大标题,就像脚本一样。
These days I find myself shifting out more and more work to the client side and hence my JS files tend to get bigger and bigger. I have come to the point where most HTML pages have half a dozen or more JS imports in the header and I realised that this is hurting loading times.
I have recently discovered this script which lets you download several JS files with one HTTP request. It is written in PHP and being a Django fan I'm planning to rewrite it in Python. I'm planning to use a HTTP redirect to the pre-minified and concatenated file and was wondering what the cost of a 301 would be. Please let me know if that is a stupid idea.
On the other hand, am little worried about introducing scripting logic into the serving of static files and I was wondering if there is a viable build alternative like, say, an ant task that concatenates and minifies JS files and replaces multiple JS downloads in a HTML header with one big one, like the script does.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 PHP,我当然喜欢动态地执行它,因为如果引入构建步骤,您就会失去使用 PHP 的主要好处之一。 事实上,冒着自我推销的风险,我已经编写了 Supercharging Javascript in PHP 关于这个问题。
当然其他技术可能会有所不同。
同样,它是 PHP,但它不仅仅是供您使用的一堆代码(尽管您可以直接跳到 第 6 部分(如果您只是想要一些完整工作的代码),并且可能对您确定问题、以正确的方式做事以及为什么这样做很有价值。
我喜欢拥有一捆 Javascript 文件(可能整个应用程序只有一个),然后每个页面只需通过标准方式激活它所需的行为,但所有代码体都位于较大的缓存和缩小的 JS 文件中。 这种方法效果最快,是一种很好的方法。
如果您确实希望将其作为构建过程的一部分(如果您无论如何都有构建过程,那么这是一个合理的解决方案),那么我建议您缩小代码。 有很多工具可以做到这一点。 看看 YUI 压缩器。
如果您对 JS 文件进行静态组合,则上面提到的其他内容(例如 gzipping 和相关问题)仍然相关。
For PHP I certainly favour doing it dynamically just because if you introduce a build step you're losing one of the main benefits of using PHP. In fact, at the risk of self-promotion I've written Supercharging Javascript in PHP about this very issue.
Of course other technologies may vary.
Again it is PHP but it's not just a lump of code for you to use (although you can jump straight to Part 6 if you just want some fully working code) and may have value to you in terms of identifying the issues and doing things the right way and why you do them that way.
I favour having bundles of Javascript files (maybe only one for the entire application) and then each page simply activates the behaviour it needs through standard means but all the code bodies are in the larger cached and minified JS file. It works out fastest this way and is a good way to go.
If you do want it as part of a build process, which is a reasonable solution if you have a build process anyway, then I suggest you minify your code. There are lots of tools to do this. Have a look at YUI Compressor.
If you do a static combine of JS files, the other stuff mentioned above such as gzipping and associated issues is still relevant.
YUI压缩器是一个不错的选择。 如果您想了解如何设置基于 Ant 的构建过程,请查看此教程:http://www.javascriptr.com/2009/07/21/setting-up-a-javascript-build-process/
作为 Ruby-基于替代方案,我建议 Sprockets
YUI compressor is a good choice. If you want to learn how to set up an Ant-based build process, have a look at this Tutorial: http://www.javascriptr.com/2009/07/21/setting-up-a-javascript-build-process/
As a Ruby-based alternative, I would recommend Sprockets