如何让Bower构建软件包?

24

有没有办法让bower在从GitHub克隆包后运行grunt包?

我正在尝试使用Bower,但其中一个我使用的软件包是Bootstrap扩展x-editable。问题在于,虽然其他软件包将完全构建的版本推送到github,因此当通过Bower安装时,您会获得已构建的版本x-editable,但该软件包希望您运行grunt文件来构建软件包。

这是npm等其他软件包管理器中的常见做法,但我找不到如何使Bower在安装时构建它的方法。这意味着我需要另一种机制来完成软件包的安装。

3个回答

23

在Node中,通过安装构建是反模式且强烈建议不要使用。与Node一样,Bower软件包应该是预构建的。这是因为最终用户不需要关心软件包需要哪种预处理器或构建系统。

你的最佳选择是说服作者预先构建、分叉并自己构建,或者在安装组件后手动构建。

Bower团队计划添加将软件包发布到类似npm的服务器的功能。这将使需要构建步骤的软件包变得更好用。


5
我使用的大部分npm中的“重型”软件包在安装后都有一些构建阶段。将预构建的跨平台(通常是本地)代码提交似乎是更糟糕的选择。尽管如此,即使作为最佳实践,Bower也不鼓励这样做,但我认为该选项应该适用于特殊情况。 - Guy Korland
2
本地扩展在Node中是一个例外,因为它们没有预编译的基础设施,但正在考虑这一点。已经有一个选项可用。Bower有一个API。因此,您可以自由地破解自己的解决方案,但对于普通用户来说,这不是一个好选择,并且永远不会被推荐或支持在Bower中使用。 - Sindre Sorhus
1
@Sinder 我的问题是,我是否可以强制对一个不是由我发布的软件包进行构建?例如,x-editable 在 Bower 存储库中,但为了使用它,您需要运行其 grunt 文件。 - Guy Korland
21
我不太理解为什么它被称作“反模式”。据我所知,Bower 是基于 Git 工作的,通常情况下在 Git 仓库中保存已编译的文件被视为不良实践。 - starwed
1
@starwed 请看我的更新答案。当涉及到可重用模块时,您通常是正确的,但所有可部署的内容都应该被编译。正如您所指出的问题,Bower 是通过 git 运作的,但用户方便性胜过开发者方便性。 - Sindre Sorhus
显示剩余2条评论

4

在bower中有三个钩子,如果由于某些原因无法像@Sindre Sorhus指出的那样进行预构建,则可以使用postinstall来解决问题。

在.bowerrc文件中执行以下操作:

{
    "scripts": {
        "preinstall": "<your command here>",
        "postinstall": "<your command here>",
        "preuninstall": "<your command here>"
    }
}

来源 https://github.com/bower/bower/blob/master/HOOKS.md

这是有关Bower Hooks的官方文档。Hooks允许您在安装和卸载包时执行自定义操作。这些自定义操作可以是Shell命令、Node模块或其他任何可执行文件。 Hooks将在指定事件发生时被调用,例如安装前、安装后、卸载前或卸载后。Hooks能够帮助您简化构建过程并确保您的项目符合特定的规范。

-2

你可以使用Composer来处理安装后的操作:

bower.json:

{
    "dependencies": {
        "bootstrap": "*"
    }
}

composer.json:

{
    "scripts" : {
        "post-install-cmd" : [
            "bower install --no-color",
            "lessc bower_components/bootstrap/less/bootstrap.less public/script/bootstrap.css"
        ]
    }
}

然后我运行 composer install

Loading composer repositories with package information
Installing dependencies (including require-dev)
Nothing to install or update
Generating autoload files
bower warn Package bootstrap is still using the deprecated "component.json" file
bower cloning git://github.com/twitter/bootstrap.git
bower cached git://github.com/twitter/bootstrap.git
bower fetching bootstrap
bower checking out bootstrap#v2.3.2
bower warn Package jquery is still using the deprecated "component.json" file
bower copying C:\Users\renadm\AppData\Roaming\bower\cache\bootstrap\9d49e0aedf207bebd028e26cc86a9e58
bower cloning git://github.com/components/jquery.git
bower cached git://github.com/components/jquery.git
bower fetching jquery
bower checking out jquery#1.8.3
bower copying C:\Users\renadm\AppData\Roaming\bower\cache\jquery\29cb4373d29144ca260ac7c3997f4381
bower installing bootstrap#2.3.2
bower installing jquery#1.8.3

在Bower安装完成后,LESS编译器会处理.less文件并将一个漂亮的bootstrap.css放入我的公共文件夹中。类似的事情也可以通过Closure Compiler来处理JavaScript文件。


与使用其他构建工具(如grunt)相比,它有何不同? - Guy Korland

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接