管理jQuery插件

gpm*_*dam 16 javascript jquery organization jquery-plugins

通常,在使用jQuery时,需要包含多个插件.这很快就会变得混乱,特别是当一些插件需要额外的组件(图像和CSS文件)时.

有哪些"推荐"方式:

  • 一个.管理所需的文件/组件(.js,.css的方式,易于维护和图像),和;
  • 将这些插件包更新到最新版本

我不一定在寻找一种工具来做到这一点(尽管我认为可以执行这种管理的工具很有用),但更多的是一种思考方式.

bal*_*ton 11

更新:现在有Bower,ComponentBrowserify自动为我们处理以下所有事情.

我很惊讶没人介绍我做的事情.所以这就是我管理脚本和资源的方式.

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.

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.

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:

  • 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 and scripts/all.min.js

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请求.

祝你好运,如果你想了解更多,请随时发表评论.