有没有办法让bower在从GitHub克隆包后运行grunt包?
我正在尝试使用Bower,但其中一个我使用的软件包是Bootstrap扩展x-editable。问题在于,虽然其他软件包将完全构建的版本推送到github,因此当通过Bower安装时,您会获得已构建的版本x-editable,但该软件包希望您运行grunt文件来构建软件包。
这是npm等其他软件包管理器中的常见做法,但我找不到如何使Bower在安装时构建它的方法。这意味着我需要另一种机制来完成软件包的安装。
有没有办法让bower在从GitHub克隆包后运行grunt包?
我正在尝试使用Bower,但其中一个我使用的软件包是Bootstrap扩展x-editable。问题在于,虽然其他软件包将完全构建的版本推送到github,因此当通过Bower安装时,您会获得已构建的版本x-editable,但该软件包希望您运行grunt文件来构建软件包。
这是npm等其他软件包管理器中的常见做法,但我找不到如何使Bower在安装时构建它的方法。这意味着我需要另一种机制来完成软件包的安装。
在Node中,通过安装构建是反模式且强烈建议不要使用。与Node一样,Bower软件包应该是预构建的。这是因为最终用户不需要关心软件包需要哪种预处理器或构建系统。
你的最佳选择是说服作者预先构建、分叉并自己构建,或者在安装组件后手动构建。
Bower团队计划添加将软件包发布到类似npm的服务器的功能。这将使需要构建步骤的软件包变得更好用。
在bower中有三个钩子,如果由于某些原因无法像@Sindre Sorhus指出的那样进行预构建,则可以使用postinstall来解决问题。
在.bowerrc文件中执行以下操作:
{
"scripts": {
"preinstall": "<your command here>",
"postinstall": "<your command here>",
"preuninstall": "<your command here>"
}
}
这是有关Bower Hooks的官方文档。Hooks允许您在安装和卸载包时执行自定义操作。这些自定义操作可以是Shell命令、Node模块或其他任何可执行文件。 Hooks将在指定事件发生时被调用,例如安装前、安装后、卸载前或卸载后。Hooks能够帮助您简化构建过程并确保您的项目符合特定的规范。你可以使用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文件。