如何为多个JavaScript文件的请求合并为一个http请求? [英] How to combine requests for multiple javascript files into one http request?

查看:150
本文介绍了如何为多个JavaScript文件的请求合并为一个http请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这概念对我来说是新的 - 我碰到它第一次出现在 YUI依赖配置器。基本上,而不是有许多文件的多个请求,文件被链接到一个HTTP请求,以减少网页加载时间。

This concept is a new one for me -- I first came across it at the YUI dependency configurator. Basically, instead of having multiple requests for many files, the files are chained into one http request to cut down on page load time.

任何人都知道如何实现这一个的LAMP堆栈? (我看到了一个类似的问题已经被问过,但它<一个href=\"http://stackoverflow.com/questions/47937/combining-and-caching-multiple-javascript-files-in-aspnet\">seems是ASP具体

Anyone know how to implement this on a LAMP stack? (I saw a similar question was asked already, but it seems to be ASP specific.

谢谢!

更新:这两个答案是有帮助的。(我的代表处是没有高到足以回应还让我在这里加入了一些离别的想法)。我也来跨越另一篇博客中与特定的PHP实施例可能是有用的。大卫的构建答案,不过,是让我考虑了不同的方法。谢谢你,大卫!

Update: Both answers are helpful...(my rep isn't high enough to comment yet so I'm adding some parting thoughts here). I also came across another blog post with PHP-specific examples that might be useful. David's build answer, though, is making me consider a different approach. Thanks, David!

推荐答案

有多种方法,最明显的两个将是:

There are various ways, the two most obvious would be:


  1. 构建像YUI一个工具,它建立了一个定制的基础上,根据需要,这样你仍然可以提供服务的文件为静态你打勾的组件唯一的版本。 MooTools的和jQuery UI,当你下载的包,给你最精简和effecient库可能都提供包建设者这样。我敢肯定,一个通用的多用途工具存在那里。

  2. 创建一个简单的Perl / PHP / Python的/ Ruby脚本,供应一堆根据请求JavaScript文件。因此,onerequest.js负荷=​​ UI&安培;负载=效应会去一个PHP脚本加载中的文件,并提供它们与正确的内容类型。有许多这样的例子,但我个人不是一个粉丝。

我preFER不通过任何类型的脚本提供静态文件,但我也想发展我的code。与10个左右的单独小班文件,而不10 HTTP请求的成本。所以,我想出了结合了所有最常见的类和函数的自定义生成过程,然后minifies他们变成像project.min.js一个单一的文件,并在所有我的意见/模板,包括生产该文件的一个条件。

I prefer not to serve static files through any sort of script, but I also like to develop my code with 10 or so seperate small class files without the cost of 10 HTTP requests. So I came up with a custom build process that combines all the most common classes and functions and then minifies them into a single file like project.min.js and have a condition in all my views/templates that includes this file on production.

编辑 - 自定义构建过程其实是一个非常简单的Perl脚本。它读取每一个我作为参数传递的文件,并将其写入到一个新的文件,可选地传递了整个事情的经过 JSMIN (在所有你喜欢的语言版本)自动。

Edit - The "custom build process" is actually an extremely simple perl script. It reads in each of the files that I've passed as arguments and writes them to a new file, optionally passing the entire thing through JSMIN (available in all your favourite languages) automatically.

在命令像它看起来像:

perl build-project-master.pl core.js class1.js etc.js /path/to/live/js/file.js

这篇关于如何为多个JavaScript文件的请求合并为一个http请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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