Bower是一个前端的包管理工具,用于管理项目中的需要用到的依赖包十分方便。

本机环境:

  • OS: Yosemite 10.10.2
  • Git: 1.9.5
  • Nodejs: 0.10.32
  • 使用cnpm代替npm

安装Bower

  • 全局安装Bower
1
cnpm install -g bower
  • 查看版本信息
1
2
❯❯❯❯❯❯ bower -v
1.3.12

使用Bower

安装完成后,可以在命令行直接使用bower命令。

下面来尝试新建一个文件夹,并在文件夹路径下执行:

1
bower install jquery

Bower会在该目录下生成bower_components文件夹,并把下载好的包放在这个文件夹里面。此时目录结构如下:

1
2
3
4
5
6
7
8
9
10
11
.
└── bower_components
└── jquery
├── MIT-LICENSE.txt
├── bower.json
├── dist
│   ├── jquery.js
│   ├── jquery.min.js
│   └── jquery.min.map
└── src
...

在执行刚才的bower install命令时,可以看到terminal里面的Bower进度提示如下:

1
2
3
4
❯❯❯❯❯❯ bower install jquery
bower jquery#* cached git://github.com/jquery/jquery.git#2.1.4
bower jquery#* validate 2.1.4 against git://github.com/jquery/jquery.git#*
bower jquery#~2.1.4 install jquery#2.1.4

bower jquery#~2.1.4可以发现Bower是通过#号来确定需要下载的版本,我们没有指定版本,Bower自动帮我们下载最新的。所以,如果需要下载特定版本的包,可以在安装命令中使用#号来声明。

1
2
3
4
❯❯❯❯❯❯ bower install jquery#1.9.1
bower jquery#1.9.1 not-cached git://github.com/jquery/jquery.git#1.9.1
bower jquery#1.9.1 resolve git://github.com/jquery/jquery.git#1.9.1
...

bower install命令还有更多其他options可以使用。这里暂时不作介绍。

查看当前Bower下载过的包

bower list命令可以查看当前下载过的包

1
2
3
4
❯❯❯❯❯❯ bower list
bower check-new Checking for new versions of the project dependencies..
bower-test /Users/levblanc/projects/bower-test
└── jquery#1.9.0 extraneous (latest is 3.0.0-alpha1+compat)

bower list --paths命令可以查看当前下载过的所有包的在项目中的对应路径。
这个命令最有用的时刻,就是在其它工具中需要声明/配置前端依赖包地址的时候。

1
2
3
❯❯❯❯❯❯ bower list --paths
jquery: 'bower_components/jquery/jquery.js'

使用bower.json文件管理依赖包

跟NodeJs的package.json文件一样,可以通过bower.json文件来管理项目需要下载的包。这样项目组里的其它成员就不需要逐一去bower install各种包了。

bower.json文件的生成方法与package.json也非常类似。可以通过bower init命令来生成,也可以手动直接在项目根目录下创建。

使用bower init命令的话,Bower会问一些问题,直接回答即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
❯❯❯❯❯❯ bower init
? name: bower-test
? version: 0.0.0
? description:
? main file:
? what types of modules does this package expose?: amd
? keywords: bower
? authors: test@testbower.com
? license: MIT
? homepage:
? 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?: (y/N) ? would you like to mark this package as private which prevents it from being accidentally published to the registry?: Yes
{
name: 'bower-test',
version: '0.0.0',
authors: [
'test@testbower.com'
],
moduleType: [
'amd'
],
keywords: [
'bower'
],
license: 'MIT',
private: true,
ignore: [
'**/.*',
'node_modules',
'bower_components',
'test',
'tests'
],
dependencies: {
backbone: '~1.2.1',
jquery: '1.9.0'
}
}
? Looks good?: Yes

完成所有问题以后,会在项目根目录生成bower.json文件。手动创建的话,跟随对应格式即可。此时文件夹结构如下:

1
2
3
4
.
├── bower.json
└── bower_components
...

当项目文件夹下已有bower.json文件时,直接执行bower install命令,便会自动下载所有依赖包。

###下载并保存依赖包到bower.json

使用安装命令的-Soption(注意S必须是大写),可以在下载的同时,把这次下载的包的信息自动加入到bower.json文件中。

1
bower install -S backbone

下载完成后bower.json文件如下:

1
2
3
4
5
6
...
"dependencies": {
"jquery": "1.9.0",
"backbone": "~1.2.1"
}
...

Bower自定义

如果想要把依赖包下载到自己希望的目录,怎么办呢?很简单,使用.bowerrc文件就可以了。

在项目根目录创建.bowerrc文件,使用json格式来写。

1
2
3
{
"directory": "app/vender/"
}

保存好以后,执行bower install命令可以见到效果。

1
2
3
4
5
6
7
8
9
.
├── app
│   └── vendor
│   ├── backbone
│   │   ...
│   ├── jquery
│ │ ...
└── bower.json

注意:.bowerrc文件需要与bower.json结合使用。在.bowerrc中声明了下载目录之后之所以能够自动下载包,是因为在bower.jsondependencies中声明过。

更多bower配置选项,可参考官网