Bower

介绍

Bower 是一个针对Web开发的包管理器。该工具主要用来帮助用户轻松安装CSS、JavaScript、图像等相关包,并管理这些包之间的依赖。

功能有些类似于Composer。Bower既可以管理基于本地资源的包,也可以管理基于git系统的包。 在Bower中,可以通过 component.json文件来定义包的名称、版本、代码中的主要元素、版本之间的依赖关系等。

安装

Bower 是一个命令行工具。通过npm安装

1
$ npm install -g bower

命令

cache (管理bower缓存)

1
$ bower cache <command> [<args>]

help (显示Bower命令的帮助信息)

1
$ bower help <command>

home (通过浏览器打开一个包的github发布页)

1
2
3
$ bower home
$ bower home <package>
$ bower home <package>#<version>

如果不带<package>, 则打开本地版本的主页

info (查看包的信息)

1
2
3
$ bower info <package>
$ bower info <package> [<property>]
$ bower info <package>#<version> [<property>]

init (创建bower.json文件)

1
bower init

install (安装包到项目)

1
2
$ bower install [<options>]
$ bower install <endpoint> [<endpoint> ..] [<options>]

可以采用多种方式安装:

  • <package>
  • <package>#<version>
  • <name>=<package>#<version>

方式参数说明:

  • <package> 可以是package URL, 物理地址或者注册名
  • <version> 有效的版本号、commit、branch等等
  • <name> 本地包的命名
package can be:
Method examples
Registered package name jquery normalize.css
Git endpoint https://github.com/user/package.git git@github.com:user/package.git
Git endpoint without .git git+https://github.com/user/package git+ssh://git@github.com/user/package
Local folder my/local/folder/
Public Subversion endpoint jquery svn+http://package.googlecode.com/svn/
Private Subversion endpoint svn+ssh://package.googlecode.com/svn/ svn+https://package.googlecode.com/svn/
Shorthand (defaults to GitHub) user/package
URL http://example.com/script.js http://example.com/style.css http://example.com/package.zip(contents will be extracted) http://example.com/package.tar(contents will be extracted)
Registered package name jquery normalize.css
Registered package name jquery normalize.css
Registered package name jquery normalize.css
version can be:
# example
semver version #1.2.3
version range #1.2
#~1.2.3
#^1.2.3
#>=1.2.3 <2.0
Git tag #<tag>
Git commit SHA #<branch>
Subversion revision #<revision>
install options
  • -F, --force-latest: Force latest version on conflict
  • -p, --production: Do not install project devDependencies
  • -S, --save: Save installed packages into the project’s bower.json dependencies
  • -D, --save-dev: Save installed packages into the project’s bower.json devDependencies
  • -E, --save-exact: Configure installed packages with an exact version rather than semver
部分示例:
1
2
3
4
5
6
7
8
# 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
1
2
$ bower link
$ bower link <name> [<local name>]

The link functionality allows developers to easily test their packages. Linking is a two-step process.
Using ‘bower link’ in a project folder will create a global link. Then, in some other package, bower link <name> will create a link in the components folder pointing to the previously created link.
This allows to easily test a package because changes will be reflected immediately. When the link is no longer necessary, simply remove it with bower uninstall <name>.

list (列出项目已安装的包)

list options
  • -p, --paths: Generates a simple JSON source mapping
  • -r, --relative: Make paths relative to the directory config property, which defaults to bower_components

lookup (根据包名查询包的URL)

1
$ bower lookup <name>

login (github)

1
$ bower login
login options
  • -t, --token: Pass an existing GitHub auth token rather than prompting for username and password

prune (删除项目无关的包)

1
$ bower prune

register (注册一个包)

1
$ bower register <name> <url>

search (搜索包)

1
2
$ bower search
$ bower search <name>

update (更新项目的包)

1
$ bower update <name> [<name> ..] [<options>]
update options
  • -F, --force-latest: Force latest version on conflict
  • -p, --production: Do not install project devDependencies

uninstall (删除项目的包)

1
$ bower uninstall <name> [<name> ..] [<options>]
uninstall options
  • -S, --save: Remove uninstalled packages from the project’s bower.json dependencies
  • -D, --save-dev: Remove uninstalled packages from the project’s bower.json devDependencies

    unregister (Unregisters a package)

    1
    $ bower bower unregister <package>

version

1
$ bower version [<newversion> | major | minor | patch]

Run this in a package directory to bump the version and write the new data back to the bower.json file.

更多参考:官方文档

使用

1
$ bower install jquery

上述命令完成以后,你会在你刚才创建的目录下看到一个bower_components的文件夹,其中目录如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ tree bower_components/
bower_components/
└── jquery
├── README.md
├── bower.json
├── component.json
├── composer.json
├── jquery-migrate.js
├── jquery-migrate.min.js
├── jquery.js
├── jquery.min.js
├── jquery.min.map
└── package.json

1 directory, 10 files

然后在HTML中引入文件即可

提交自己的类库

  1. 生成bower.json配置文件
  2. 在github创建一个资源库:js-bower
  3. 本地工程绑定github, 然后push到github上
  4. 注册到bower官方类库

    1
    bower register nodejs-bower git@github.com:raomingchao/js-bower.git
  5. 查询自己的包

    1
    bower search js-bower
  6. 安装自己的包

    1
    bower install nodejs-bower