問題描述
通常,在使用 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.
推薦答案
更新:這幾天有 Bower、組件和Browserify 它會自動為我們處理以下所有事情.
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 外部并將該項目的任何分支或版本或我想要的任何內容直接獲取到項目 scripts代碼>文件夾.由于我們使用的是 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
我無法分享確切的 Makefile
,但我可以分享一個處理 CSS 和 Javascript 的打包/合并和縮小的公共文件.鏈接在這里:http://github.com/balupton/jquery-sparkle/blob/9921fcbf1cbeab7a4f2f875a91cb854生成文件
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 請求.
祝你好運,如果您想了解更多信息,請隨時發表評論.
So good luckmate, feel free to post a comment if you would like to learn more.
這篇關于管理 jQuery 插件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!