Only Grab the Files You Need While Using Bower

Bower is an amazing tool to bring some sanity to the multitudes of frontend packages that can be used in your application.

We’ve written about using Bower before in our article,Manage Front End Resources with Bower, but it can be improved even further.

While you should keep yourbower_componentsfolder out of your git repository, many people don’t like having the entire repo for a resource in their project. For instance, pulling in Bootstrap pulls in a lot of files that you would not need.

Fortunately there’s a great package to solve this very problem.

Bower Installer

bower-installerbyBret Littlehelps us bring in just the files that we need for our project.

Instead of pulling in the entire Bootstrap repo:

We can just pull in the necessary files:

Configuration

To get started using bower-installer is very simple. Let’s say you have a project where you’ve pulled in Angular, Bootstrap, jQuery, and animate.css. You would pull these packages in like so:

$ bowerinstallangular bootstrap jquery animate.css--save

This would create abower.jsonfile that looks like so:

{"name":"bower","dependencies":{"angular":"~1.3.15","bootstrap":"~3.3.4","jquery":"~2.1.3","animate.css":"~3.2.5"}}

This would also install these packages (and their giant repos) into thebower_components/folder. Let’s use bower-installer to quickly grab only the files that we need out of each of these packages.

For instance, we only wantjquery.jsandangular.js.

Using bower-installer

First, we need to install the bower-installer package:

$ npminstall-g bower-installer

This will use Node and npm to install bower-installer globally.

After installing, the simplest way to use bower-installer is to specify the path you would like to use. For this example, we’re going to place the necessary files in a folder calledlibs/.

Add the followinginstallattribute to yourbower.jsonfile:

"install":{"path":"libs"}

This will place all the files we need in a newlibs/folder (not inside of your bower_components folder).

Now, we just need to activate bower-installer. From the command line, type:

$ bower-installer

Now, if we look at our folder structure, you’ll see there is alibs/folder with only the resources we need!

bower-installer will try to grab the relevant files, but if it can’t because the main files aren’t defined, then you can override with thespecific files you want.

Conclusion

bower-installer is a simple, but neat package that can help fix one of the main complaints of Bower.

There are many other ways to configure bower-installer than to just drop in the files in one main folder. The officialGitHub repoprovides great explanations of all the other ways that it can be configured.

Give it a look through and see if you can use it in your own projects.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,916评论 0 23
  • 重要感受到廖彩杏老师说她家有50多本字母书是什么感觉了。这次928活动我有手欠,忍不住又收字母书。而这本《ABC ...
    木木sani阅读 417评论 0 1
  • 每天都会认真阅读《遇见未知的自己》这本心灵修行书,在那段特殊的日子里,原本痛苦浮躁的心,静下来了,静静的反思,静静...
    牛牛头阅读 971评论 1 50
  • #幸福是需要修出来的~每天进步1%~幸福实修09班~张媛 20170804(19/30)09班 【幸福三朵玫瑰】 ...
    自在如我是阅读 186评论 1 0
  • 很多时候,我们行色匆匆,象上了发条一样,匆匆起床,匆匆洗涮,匆匆吃饭,匆匆赶车,匆匆赶活,生活就处在这种紧张忙碌的...
    微酸小小鱼阅读 339评论 0 0