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.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 10,998評(píng)論 0 23
  • 重要感受到廖彩杏老師說(shuō)她家有50多本字母書(shū)是什么感覺(jué)了。這次928活動(dòng)我有手欠,忍不住又收字母書(shū)。而這本《ABC ...
    木木sani閱讀 446評(píng)論 0 1
  • 每天都會(huì)認(rèn)真閱讀《遇見(jiàn)未知的自己》這本心靈修行書(shū),在那段特殊的日子里,原本痛苦浮躁的心,靜下來(lái)了,靜靜的反思,靜靜...
    牛牛頭閱讀 1,016評(píng)論 1 50
  • #幸福是需要修出來(lái)的~每天進(jìn)步1%~幸福實(shí)修09班~張媛 20170804(19/30)09班 【幸福三朵玫瑰】 ...
    自在如我是閱讀 218評(píng)論 1 0
  • 很多時(shí)候,我們行色匆匆,象上了發(fā)條一樣,匆匆起床,匆匆洗涮,匆匆吃飯,匆匆趕車(chē),匆匆趕活,生活就處在這種緊張忙碌的...
    微酸小小魚(yú)閱讀 362評(píng)論 0 0

友情鏈接更多精彩內(nèi)容