陈斌彬的技术博客

Stay foolish,stay hungry

Bower - 管理你的客户端依赖关系

安装Bower

一旦你已经安装了上面所说的所有必要文件,键入以下命令安装 Bower:

$ npm install -g bower

这行命令是 Bower 的全局安装,-g 操作表示全局。

开始使用Bower

安装完 bower 之后就可以使用所有的 bower 命令了。可以键入 help命令来查看 bower 可以完成那些操作,如下:

$ bower help
Usage:

    bower <command> [<args>] [<options>]

Commands:

    cache                   Manage bower cache
    help                    Display help information about Bower
    home                    Opens a package homepage into your favorite browser
    info                    Info of a particular package
    init                    Interactively create a bower.json file
    install                 Install a package locally
    link                    Symlink a package folder
    list                    List local packages
    lookup                  Look up a package URL by name
    prune                   Removes local extraneous packages
    register                Register a package
    search                  Search for a package by name
    update                  Update a local package
    uninstall               Remove a local package

Options:

    -f, --force             Makes various commands more forceful
    -j, --json              Output consumable JSON
    -l, --log-level         What level of logs to report
    -o, --offline           Do not hit the network
    -q, --quiet             Only output important information
    -s, --silent            Do not output anything, besides errors
    -V, --verbose           Makes output more verbose
    --allow-root            Allows running commands as root

包的安装

Bower 是一个软件包管理器,所以你可以在应用程序中用它来安装新的软件包。举例来看一下来如何使用 Bower 安装 JQuery,在你想要安装该包的地方创建一个新的文件夹,键入如下命令:

$ bower install jquery

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

$ 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

包的使用

现在就可以在应用程序中使用 jQuery 包了,在 jQuery 里创建一个简单的 html5 文件:

<!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/jquery.min.js"></script>
<script type="text/javascript">

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

正如你所看到的,你刚刚引用 jquery.min.js 文件,现阶段完成。

所有包的列表

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

$ bower list
bower check-new     Checking for new versions of the project dependencies..
blog /Users/shekhargulati/day1/blog
└── jquery#2.0.3 extraneous

包的搜索

假如你想在你的应用程序中使用 twitter 的 bootstrap 框架,但你不确定包的名字,这时你可以使用 search 命令:

$ bower search bootstrap
Search results:

bootstrap git://github.com/twbs/bootstrap.git
angular-bootstrap git://github.com/angular-ui/bootstrap-bower.git
sass-bootstrap git://github.com/jlong/sass-twitter-bootstrap.git

包的信息

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

$ bower info bootstrap
bower bootstrap#*           not-cached git://github.com/twbs/bootstrap.git#*
bower bootstrap#*              resolve git://github.com/twbs/bootstrap.git#*
bower bootstrap#*             download https://github.com/twbs/bootstrap/archive/v3.0.0.tar.gz
bower bootstrap#*              extract archive.tar.gz
bower bootstrap#*             resolved git://github.com/twbs/bootstrap.git#3.0.0

{
  name: 'bootstrap',
  version: '3.0.0',
  main: [
    './dist/js/bootstrap.js',
    './dist/css/bootstrap.css'
  ],
  ignore: [
    '**/.*'
  ],
  dependencies: {
    jquery: '>= 1.9.0'
  },
  homepage: 'https://github.com/twbs/bootstrap'
}

Available versions:
  - 3.0.0
  - 3.0.0-rc1
  - 3.0.0-rc.2
  - 2.3.2
 .....

如果你想得到单个包的信息,也可以使用 info 命令:

$ bower info bootstrap#3.0.0
bower bootstrap#3.0.0           cached git://github.com/twbs/bootstrap.git#3.0.0
bower bootstrap#3.0.0         validate 3.0.0 against git://github.com/twbs/bootstrap.git#3.0.0

{
  name: 'bootstrap',
  version: '3.0.0',
  main: [
    './dist/js/bootstrap.js',
    './dist/css/bootstrap.css'
  ],
  ignore: [
    '**/.*'
  ],
  dependencies: {
    jquery: '>= 1.9.0'
  },
  homepage: 'https://github.com/twbs/bootstrap'
}

包的卸载

卸载包可以使用 uninstall 命令:

$ bower uninstall jquery

bower.json文件的使用

bower.json 文件的使用可以让包的安装更容易,你可以在应用程序的根目录下创建一个名为 “bower.json” 的文件,并定义它的依赖关系。使用 bower init 命令来创建 bower.json 文件:

$ bower init
[?] name: blog
[?] version: 0.0.1
[?] description:
[?] main file:
[?] keywords:
[?] authors: Shekhar Gulati <shekhargulati84@gmail.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? No

{
  name: 'blog',
  version: '0.0.1',
  authors: [
    'Shekhar Gulati <shekhargulati84@gmail.com>'
  ],
  license: 'MIT',
  ignore: [
    '**/.*',
    'node_modules',
    'bower_components',
    'test',
    'tests'
  ],
  dependencies: {
    jquery: '~2.0.3'
  }
}

[?] Looks good? Yes

可以查看该文件:

{
  "name": "blog",
  "version": "0.0.1",
  "authors": [
    "Shekhar Gulati <shekhargulati84@gmail.com>"
  ],
  "license": "MIT",
  "ignore": [
    "**/.*",
    "node_modules",
    "bower_components",
    "test",
    "tests"
  ],
  "dependencies": {
    "jquery": "~2.0.3"
  }
}

注意看,它已经加入了 jQuery 依赖关系。

现在假设也想用 twitter bootstrap,我们可以用下面的命令安装 twitter bootstrap 并更新 bower.json 文件:

$ bower install bootstrap --save

它会自动安装最新版本的 bootstrap 并更新 bower.json 文件:

{
  "name": "blog",
  "version": "0.0.1",
  "authors": [
    "Shekhar Gulati <shekhargulati84@gmail.com>"
  ],
  "license": "MIT",
  "ignore": [
    "**/.*",
    "node_modules",
    "bower_components",
    "test",
    "tests"
  ],
  "dependencies": {
    "jquery": "~2.0.3",
    "bootstrap": "~3.0.0"
  }
}