前端开发圈愈来愈热闹,第三方js库层出不穷,版本更迭突飞猛进;且许多库又对其余库有着依赖关系。而咱们的页面须要的库也愈来愈多样化。以往须要引入第三方库,须要搜索寻找所需库的特定版本下载,还须要搜索寻找下载特定版本的依赖库,十分麻烦;库的更新也是件十分繁琐的事情。是否有一个工具:搜索、自动安装/卸载、检查更新、确保依赖关系……css
Bower 是 twitter 推出的一款包管理工具,基于nodejs的模块化思想,把功能分散到各个模块中,让模块和模块之间存在联系,经过 Bower 来管理模块间的这种联系。html
npm是专门管理node模块的管理工具,而bower是node的模块,由于bower是依赖node,npm和git。正如前面所言,npm是擅长的是管理node模块,而bower管理的范围更大,涉及html,css,js和图片等媒体资源。或许,这也是人们喜欢在服务器端使用npm,而在客户端使用bower。前端
Bower 的基础功能有哪些?node
Bower依赖于Node.js,Git(资源主要经过git进行下载),所以你须要提早安装好,才能正常安装bower。 Bower能够经过NPM进行安装:jquery
一旦你已经安装了上面所说的全部必要文件,键入如下命令安装Bower:git
$ npminstall -g bower
这行命令是Bower的全局安装,-g 操做表示全局。es6
$ bower –version //检查版本确认是否安装好 $ npmupdate -g bower //更新Bower版本 $ npmuninstall --global bower 卸载Bower
安装完bower以后就可使用全部的bower命令了。能够键入help 命令来查看bower能够完成那些操做,以下:github
PS D:\> bowerhelp Usage: bower <command> [<args>] [<options>] Commands: cache Managebowercache help DisplayhelpinformationaboutBower home Opens a package homepageintoyourfavoritebrowser info Infoof a particularpackage init Interactivelycreate a bower.jsonfile install Install a package locally link Symlink a package folder list Listlocalpackages - and possibleupdates login AuthenticatewithGitHuband storecredentials lookup Lookup a package URLbyname prune Removeslocalextraneouspackages register Register a package search Searchfor a package byname update Update a localpackage uninstall Remove a localpackage unregister Remove a package fromtheregistry version Bump a package version Options: -f, --force Makesvariouscommandsmoreforceful -j, --json OutputconsumableJSON -l, --loglevel Whatleveloflogsto report -o, --offline Do not hitthenetwork -q, --quiet Onlyoutputimportantinformation -s, --silent Do not outputanything, besideserrors -V, --verbose Makesoutputmoreverbose --allow-root Allowsrunningcommandsas root -v, --version OutputBowerversion --no-color Disablecolors See 'bower help <command>' for moreinformationon a specificcommand.
上面help 信息列出 bower 提供的命令:web
Bower是一个软件包管理器,举例来看一下来如何使用Bower安装JQuery,首先使用命令行将找到包存放的本地目录,而后执行:ajax
bowerinstalljquery
安装后的库默认存放在项目的 bower_components 子目录,若是要指定其余位置,可在 .bowerrc 文件的 directory 属性设置。
Bower使用包的名字去在线索引中搜索该包的网址 — bower.com。某些状况下,若是一个库很新(或者你不想使用默认网址),可能须要咱们手动指定该库的网址。指定的网址能够是 github 地址、http 网址、本地文件。
bowerinstallgit://github.com/documentcloud/backbone.git bowerinstallhttp://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js bowerinstall ./some/path/relative/to/this/directory/backbone.js
默认状况下,会安装该库的最新版本,可是也能够手动指定版本号。
bowerinstalljquery-ui#1.10.1
若是某个库依赖另外一个库,安装时默认将所依赖的库一块儿安装。Bower会根据该库的 bower.json 文件下的 dependencies 配置自动给你安装指定依赖库的指定版本。好比,jquery-ui依赖jquery,安装时会连jquery一块儿安装。
假如你想在你的应用程序中使用twitter的bootstrap框架,但你不肯定包的名字,这时你可使用search 命令:
PS D:\> bowersearchbootstrap Searchresults: bootstrapgit://github.com/twbs/bootstrap.git angular-bootstrapgit://github.com/angular-ui/bootstrap-bower.git bootstrap-sass-officialgit://github.com/twbs/bootstrap-sass.git sass-bootstrapgit://github.com/jlong/sass-bootstrap.git bootstrap-datepickergit://github.com/eternicode/bootstrap-datepicker.git bootstrap-selectgit://github.com/silviomoreto/bootstrap-select.git angular-ui-bootstrap-bowergit://github.com/angular-ui/bootstrap-bower angular-ui-bootstrapgit://github.com/angular-ui/bootstrap.git bootstrap-daterangepickergit://github.com/dangrossman/bootstrap-daterangepicker.git bootstrap-timepickergit://github.com/jdewit/bootstrap-timepicker bootstrap-switch git://github.com/nostalgiaz/bootstrap-switch.git bootstrap-cssgit://github.com/jozefizso/bower-bootstrap-css.git select2-bootstrap-cssgit://github.com/t0m/select2-bootstrap-css.git eonasdan-bootstrap-datetimepickergit://github.com/Eonasdan/bootstrap-datetimepicker.git seiyria-bootstrap-slidergit://github.com/seiyria/bootstrap-slider.git angular-bootstrap-colorpickergit://github.com/buberdds/angular-bootstrap-colorpicker.git bootstrap-multiselectgit://github.com/davidstutz/bootstrap-multiselect.git bootstrap.cssgit://github.com/bowerjs/bootstrap.git bootstrap-datetimepickergit://github.com/tarruda/bootstrap-datetimepicker.git angular-bootstrap-datetimepickergit://github.com/dalelotts/angular-bootstrap-datetimepicker bootstrap-modalgit://github.com/jschr/bootstrap-modal.git bootstrap-tourgit://github.com/sorich87/bootstrap-tour.git bootstrap-tagsinputgit://github.com/TimSchlechter/bootstrap-tagsinput.git bootstrap-additionsgit://github.com/mgcrea/bootstrap-additions.git bootstrap-file-inputgit://github.com/grevory/bootstrap-file-input.git angular-bootstrap-switch git://github.com/frapontillo/angular-bootstrap-switch.git bootstrap-socialgit://github.com/lipis/bootstrap-social.git twbs-bootstrap-sassgit://github.com/twbs/bootstrap-sass ember-addons.bs_for_embergit://github.com/ember-addons/bootstrap-for-ember.git jasny-bootstrapgit://github.com/jasny/bootstrap.git
若是你想看到关于特定的包的信息,可使用info 命令来查看该包的全部信息:
PS D:\> bowerinfobootstrap bowerbootstrap#* not-cached git://github.com/twbs/bootstrap.git#* bowerbootstrap#* resolve git://github.com/twbs/bootstrap.git#* bowerbootstrap#* download https://github.com/twbs/bootstrap/archive/v3.3.6.tar.gz bowerbootstrap#* progress received 0.9MB of 3.8MB downloaded, 24% bowerbootstrap#* progress received 1.0MB of 3.8MB downloaded, 27% bowerbootstrap#* progress received 1.1MB of 3.8MB downloaded, 30% bowerbootstrap#* progress received 1.3MB of 3.8MB downloaded, 34% bowerbootstrap#* progress received 1.4MB of 3.8MB downloaded, 37% bowerbootstrap#* progress received 1.6MB of 3.8MB downloaded, 41% bowerbootstrap#* progress received 1.7MB of 3.8MB downloaded, 46% bowerbootstrap#* progress received 1.9MB of 3.8MB downloaded, 50% bowerbootstrap#* progress received 2.1MB of 3.8MB downloaded, 55% bowerbootstrap#* progress received 2.3MB of 3.8MB downloaded, 59% bowerbootstrap#* progress received 2.4MB of 3.8MB downloaded, 64% bowerbootstrap#* progress received 2.6MB of 3.8MB downloaded, 69% bowerbootstrap#* progress received 2.8MB of 3.8MB downloaded, 74% bowerbootstrap#* progress received 3.0MB of 3.8MB downloaded, 79% bowerbootstrap#* progress received 3.2MB of 3.8MB downloaded, 85% bowerbootstrap#* progress received 3.4MB of 3.8MB downloaded, 88% bowerbootstrap#* progress received 3.5MB of 3.8MB downloaded, 92% bowerbootstrap#* progress received 3.7MB of 3.8MB downloaded, 96% bowerbootstrap#* extract archive.tar.gz bowerbootstrap#* resolved git://github.com/twbs/bootstrap.git#3.3.6 { name: 'bootstrap', description: 'The most popular front-end framework for developing responsive, mobile first projects on the web.', keywords: [ 'css', 'js', 'less', 'mobile-first', 'responsive', 'front-end', 'framework', 'web' ], homepage: 'http://getbootstrap.com', license: 'MIT', moduleType: 'globals', main: [ 'less/bootstrap.less', 'dist/js/bootstrap.js' ], ignore: [ '/.*', '_config.yml', 'CNAME', 'composer.json', 'CONTRIBUTING.md', 'docs', 'js/tests', 'test-infra' ], dependencies: { jquery: '1.9.1 - 2' }, version: '3.3.6' } Availableversions: - 3.3.6 - 3.3.5 - 3.3.4 - 3.3.2 - 3.3.1 - 3.3.0 - 3.2.0 - 3.1.1 - 3.1.0 - 3.0.3 - 3.0.2 - 3.0.1 - 3.0.0 - 2.3.2 - 2.3.1 - 2.3.0 - 2.2.2 - 2.2.1 - 2.2.0 - 2.1.1 - 2.1.0 - 2.0.4 - 2.0.3 - 2.0.2 - 2.0.1 - 2.0.0 - 1.4.0 - 1.3.0 - 1.2.0 - 1.1.1 - 1.1.0 - 1.0.0 Show 4 additionalprereleaseswith ‘bowerinfobootstrap --verbose’ Youcanrequestinfofor a specificversionwith 'bower info bootstrap#<version>'
查看指定版本包的信息:
PS D:\> bowerinfobootstrap#3.3.6 bowerbootstrap#3.3.6 cached git://github.com/twbs/bootstrap.git#3.3.6 bowerbootstrap#3.3.6 validate 3.3.6 against git://github.com/twbs/bootstrap.git#3.3.6 { name: 'bootstrap', description: 'The most popular front-end framework for developing responsive, mobile first projects on the web.', keywords: [ 'css', 'js', 'less', 'mobile-first', 'responsive', 'front-end', 'framework', 'web' ], homepage: 'http://getbootstrap.com', license: 'MIT', moduleType: 'globals', main: [ 'less/bootstrap.less', 'dist/js/bootstrap.js' ], ignore: [ '/.*', '_config.yml', 'CNAME', 'composer.json', 'CONTRIBUTING.md', 'docs', 'js/tests', 'test-infra' ], dependencies: { jquery: '1.9.1 - 2' }, version: '3.3.6' } 已安装包列表 PS D:\> bowerlist bowercheck-new Checkingfor new versionsoftheprojectdependencies... root D:\ └── jquery#2.2.0 extraneous (latest is 3.0.0-beta1)
项目根目录下(也能够放在用户的主目录下,这样就不用每次都配置)的 .bowerrc 文件是 Bower 的配置文件,它大概像下面这样。
{ "directory" : "components", "json" : "bower.json", "endpoint" : "https://Bower.herokuapp.com", "searchpath" : "", "shorthand_resolver" : "" }
其中的属性含义以下。
bower.json文件的使用可让包的安装更容易,你能够在应用程序的根目录下建立一个名为“bower.json”的文件,并定义它的依赖关系。bower.json的做用是:
其中dependencies 记录着生产环境依赖的库;devDependencies 记录着开发时依赖的 node package。其版本规则见 npm 的version rules。
使用bower init 命令能够来建立bower.json文件,它会自动提示你输入一系列的内容,以生成最终的文件,包括项目名称、做者信息、项目描述信息、关键词、开源证书等等。
PS D:\> bowerinit ? namenewone ? description a new oneproject ? mainfilenewone.js ? whattypesofmodulesdoesthis package expose? es6 ? keywordstest ? authorsbiaodianfu <biaodianfu#gmail.com> ? licenseMIT ? homepagehttp://www.biaodianfu.com ? set currently installed components as dependencies? Yes ? add commonly ignored files to ignore list? Yes ? would you like to mark this package as private which prevents it from being accidentally published to the registry? No { name: 'newone', authors: [ 'biaodianfu <biaodianfu#gmail.com>' ], description: 'a new one project', main: 'newone.js', moduleType: [ 'es6' ], keywords: [ 'test' ], license: 'MIT', homepage: 'http://www.biaodianfu.com', ignore: [ '**/.*', 'node_modules', 'bower_components', 'test', 'tests' ], dependencies: { jquery: '^2.2.0' } }
注意看,它已经加入了jQuery依赖关系。如今假设也想用twitter bootstrap,咱们能够用下面的命令安装twitter bootstrap并更新bower.json文件:
$ bowerinstallbootstrap --save
-save 就是把下载的包信息写入到配置文件的依赖项里,它会自动安装最新版本的bootstrap并更新bower.json文件:
{ "name": "newone", "authors": [ "biaodianfu <biaodianfu@gmail.com>" ], "description": "a new one project", "main": "newone.js", "moduleType": [ "es6" ], "keywords": [ "test" ], "license": "MIT", "homepage": "http://www.biaodianfu.com", "ignore": [ "**/.*", "node_modules", "bower_components", "test", "tests" ], "dependencies": { "jquery": "^2.2.0", "bootstrap": "^3.3.6" } }
须要注意的是,这里有两个版本的依赖,一个是dependencies,另外一个是devDependencies,分别表明生产环境和开发环境中的依赖包,它们能够分别经过下面两个指令自动添加:
bowerinstalljquery --save //添加到dependencies bowerinstallangular --save-dev //添加到devDependencies
按照上面的作法,例如,我建立了一个前端开发的较为齐全的包,就能够挂在git或者bower的官网上了。而你要作的是,只把这个bower.jsonpush到线上便可。别人经过下载这个bower.json文件,在已经安装bower的前提下,直接运行bower install就可使用这个前端开发包了。
你能够注册本身的包,这样其余人也可使用了,这个操做只是在服务器上保存了一个映射,服务器自己不托管代码。
提交你的 bower 包给 bower.com:
bowerregister <my-package-name> <git-endpoint>
实例:在 bower.com 登记jquery
bowerregisterjquerygit://github.com/jquery/jquery
注意,若是你的库与现有的库重名,就会提交失败。