Bower是一个基于node的软件包管理器,它可以用来管理web站点中的框架、类库、静态资源、工具包等网络资源。

安装bower

打开终端,输入:

$ npm install -g bower

安装bower需要首先安装:node, npm and git.

初始化一个bower包

使用bower init命令来创建bower.json文件:

$ bower init
? name test
? description test
? main file test.js
? keywords test
? authors sobird
? license MIT
? homepage http://sobird.me
? 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: 'test',
  description: 'test',
  main: 'test.js',
  keywords: [
    'test'
  ],
  authors: [
    'sobird'
  ],
  license: 'MIT',
  homepage: 'http://sobird.me',
  ignore: [
    '**/.*',
    'node_modules',
    'bower_components',
    'test',
    'tests'
  ],
  dependencies: {
    angular: 'angularjs#^1.5.5',
    jaring: 'crossyou/jaring',
    underscore: '^1.8.3',
    jquery: '^2.2.3'
  }
}

? Looks good? Yes

通过上命令,可在项目目录下建立一个名叫bower.json的文件,用来表示该目录隶属于一个bower包。

包的安装

打开终端,输入:

$ bower install jquery

通过上面的命令,jquery包将会安装到当前目录下的bower_components目录里。

bower支持下面几种包安装方式:

# installs the project dependencies listed in bower.json
$ bower install
# registered package
$ bower install jquery
# GitHub shorthand
$ bower install desandro/masonry
# Git endpoint
$ bower install git://github.com/user/package.git
# URL
$ bower install http://example.com/script.js

包的使用

在当前项目目录下创建一个html文件:

<!doctype html>
<html>
<head>
    <title>Learning Bower</title>
</head>
<body>

<button>Animate Me!!</button>
<div style="background:red;height:100px;width:100px;position:absolute;">
</div>

<script type="text/javascript" src="bower_components/jquery/dist/jquery.min.js"></script>
<script type="text/javascript">

    $(document).ready(function(){
        $("button").click(function(){
            $("div").animate({left:'250px'});
        });
    });
</script>
</body>
</html>

打开上面的html文件,点击页面中的按钮,动画执行了,说明jquery引用成功。

所有包的列表

如果你想找出所有安装在应用程序中的包,可以使用list命令:

$ bower list
bower check-new     Checking for new versions of the project dependencies...
gulp-dev D:gulp-dev
├── angular#1.5.5 extraneous (1.5.6-build.4802+sha.05d2703 available)
├── jaring#af78fc719c
├── jquery#2.2.3 (latest is 3.0.0-beta1)
└── underscore#1.8.3

包的搜索

打开终端,输入一下命令:

$ bower search jquery

有时候你不太确定包的完整名字,可以通过包的搜索功能,检索出带有你所键入的关键字包的列表,从中选择正确的包进行安装。

包的信息

如果你想看到关于特定的包的信息,可以使用info 命令来查看该包的所有信息:

$ bower info angular
bower cached        https://github.com/angular/bower-angular.git#1.5.5
bower validate      1.5.5 against https://github.com/angular/bower-angular.git#*

{
  name: 'angular',
  version: '1.5.5',
  license: 'MIT',
  main: './angular.js',
  ignore: [],
  dependencies: {},
  homepage: 'https://github.com/angular/bower-angular'
}

Available versions:
  - 1.5.5
  - 1.5.4
  ...
  - 1.0.3

Show 3153 additional prereleases with ‘bower info angular --verbose’
You can request info for a specific version with 'bower info angular#'

如果你想得到特定版本包的信息,也可以如下命令:

$ bower info angular
bower cached        https://github.com/angular/bower-angular.git#1.5.5
bower validate      1.5.5 against https://github.com/angular/bower-angular.git#*

{
  name: 'angular',
  version: '1.5.5',
  license: 'MIT',
  main: './angular.js',
  ignore: [],
  dependencies: {},
  homepage: 'https://github.com/angular/bower-angular'
}

包的卸载

卸载包可以使用uninstall 命令:

$ bower uninstall jquery