问题描述
通常,在使用jQuery时,需要包含多个插件。这很快就会变得混乱,特别是当一些插件需要额外的组件(图像和CSS文件)时。
Often, when working with jQuery, the need arises to include multiple plugins. This can quickly become messy work, especially when some plugins require additional components (images and CSS files).
有哪些推荐的方法:
- a。管理所需的文件/组件(
.js
,.css
和图片)以易于维护的方式,并且; - b。将这些插件包更新到最新版本
- a. Manage the required files/components (
.js
,.css
and images) in a way that is easy to maintain, and; - b. Keep these plugin packages updated to the latest versions
我不一定要找工具来做这件事(尽管可以执行我认为这种管理很有用,但更多的是思考方式。
I'm not necessarily looking for a tool to do this (although one that could perform this management would be useful, I suppose), but more of a way of thinking.
推荐答案
更新:这些天有,和,负责照顾al我自动为我们提供以下内容。
Update: These days there is Bower, Component and Browserify which take care of all of the following for us automatically.
我很惊讶没有人介绍我的工作。所以这就是我管理脚本和资源的方式。
I'm surprised no one has covered what I do yet. So here's how I manage scripts and resources.
我在每个项目中使用 SVN
进行设置。我包含的几乎所有脚本都有一个SVN镜像(github现在有svn)这意味着我可以使用SVN外部并将任何分支或版本或任何我想要的项目直接提取到项目中脚本
文件夹。当我们使用SVN时,很容易跟踪,管理和更新这些脚本。
I have each project I work on setup with SVN
. Nearly all of the scripts I include have a SVN mirror (github has svn these days) this means that I can then use SVN externals and fetch whatever branch or version or whatever I want of that project directly into the projects scripts
folder. As we are using SVN, it is easy to track, manage and update these scripts.
如果项目不在SVN上,那么我只需将它添加到一个普通的SVN我做的项目,例如项目A和项目B,都使用 jquery-project-not-in-svn
,所以我们坚持 jquery- project-not-in-svn
进入我们公共项目的SVN存储库,然后使用项目A和B上的SVN外部引用它 - 如前所述。
If a project is not on SVN, then I just add it to a common SVN project I have made, so for instance Project A and Project B, both use jquery-project-not-in-svn
, so we stick jquery-project-not-in-svn
into our common project's SVN repository, and then use SVN externals on Projects A and B to reference it - as explained before.
现在包括管理,提取和更新。
Now that covers managing, fetching and updating.
以下是我如何处理脚本包含和请求。
Here is how I cover script inclusions and requests.
由于每个项目现在都有自己的脚本目录,其中包含所需的所有脚本(由SVN外部管理),我们现在不得不担心缩小它们以减少服务器的负载。每个项目的根目录中都有一个 Makefile
,其中包含命令 update
。此命令将执行以下操作:
As each project now has it's own scripts directory that contains all the scripts it needs (which is managed by SVN externals), we now have to worry about minifying them to reduce load on our server. Each project has a Makefile
in it's root, which contains the command update
. This command will perform the following:
- 执行SVN更新(这将适当更新所有SVN外部)
- 完成后,它会将所有js文件打包并缩小为
scripts / all.js
和scripts / all.min.js
- Perform a SVN update (this will update all SVN externals appropriately)
- Once that is done, it will pack and minify all the js files into
scripts/all.js
andscripts/all.min.js
我无法分享确切的 Makefile
但我可以分享一个公共处理包装/合并和缩小CSS和Javascript的方法。以下是链接:
I can't share the exact Makefile
but I can share one which is public that handles packing/merging and minification of CSS and Javascript. Here is the link:http://github.com/balupton/jquery-sparkle/blob/9921fcbf1cbeab7a4f2f875a91cb8548f3f65721/Makefile
通过做这些事情,我们实现了:
By doing these things, we have achieved:
- 管理多个项目的外部脚本资源
- 自动更新适当的脚本资源
- 打包所有使用过的脚本将项目资源合并到一个文件中
- 缩小该文件,以便只执行一个JS请求和一个CSS请求。
- Management of external script resources over multiple projects
- Updating of appropriate script resources automatically
- Packing all used script resources of the project into one file
- Minifying that file, such that only one JS request and one CSS request are performed.
祝你好运,如果你想了解更多信息,请随时发表评论。
So good luckmate, feel free to post a comment if you would like to learn more.
这篇关于管理jQuery插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!