twitter-bootstrap 如何让 bower 构建包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17170500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 17:22:05  来源:igfitidea点击:

How make bower build the package?

twitter-bootstrapgruntjspackage-managersbower

提问by Guy Korland

Is there a way to make bower run a package grunt after it was cloned from GitHub?

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

I'm trying to use Bower but one of the packages I'm using is the Bootstrap extension, x-editable. The problem is that while other packages push a fully built version to github so when installed by Bower you have a built version x-editable expect you to run a grunt file to build the package.

我正在尝试使用 Bower,但我使用的软件包之一是 Bootstrap 扩展,x-editable。问题是,虽然其他包将完整构建的版本推送到 github,因此当由 Bower 安装时,您有一个构建版本 x-editable 希望您运行 grunt 文件来构建包。

That is a common practice in other package managers like npm but I could find how to make Bower build it on install. Which means I need another mechanism to complete the installation of the package.

这是其他包管理器(如 npm)的常见做法,但我可以找到如何让 Bower 在安装时构建它。这意味着我需要另一种机制来完成包的安装。

采纳答案by Sindre Sorhus

Building on install is an anti-pattern and is strongly recommended against in Node. Like Node, Bower packages should be pre-built. This is because the end-user shouldn't have to care what preprocessor or build-system a package requires.

在安装上构建是一种反模式,强烈建议在 Node.js 中反对。像 Node 一样,Bower 包应该是预先构建的。这是因为最终用户不必关心包需要什么预处理器或构建系统。

Your best options are to either convince the author to pre-build, fork and do it yourself, or build manually after you've installed the component.

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

The Bower team is planning to add the ability to publish packages to a server similar to how it works in npm. This will make it much better for packages needing a build-step.

Bower 团队计划添加将包发布到服务器的功能,类似于 npm 中的工作方式。这将使需要构建步骤的包变得更好。

回答by schurpf

There are 3 hooks in bower, postinstall may solve your problem if for whatever reason you can not pre-built like @Sindre Sorhus points out.

bower 中有 3 个钩子,如果出于某种原因您无法像 @Sindre Sorhus 指出的那样预先构建,则安装后可能会解决您的问题。

In .bowerrc do:

在 .bowerrc 中:

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

source https://github.com/bower/bower/blob/master/HOOKS.md

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

回答by Renaat De Muynck

You could use composer for handling post install:

您可以使用 composer 处理安装后:

bower.json:

凉亭.json:

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

composer.json:

作曲家.json:

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

Then I run composer install:

然后我运行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\bootstrapd49e0aedf207bebd028e26cc86a9e58
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\jquerycb4373d29144ca260ac7c3997f4381
bower installing bootstrap#2.3.2
bower installing jquery#1.8.3

After Bower finishes installing, the LESS compiler processes the .less files and puts a nice bootstrap.cssin my public folder. something similar could be done for JavaScript files with Closure Compiler.

Bower 完成安装后,LESS 编译器会处理 .less 文件并将一个 nice 文件放入bootstrap.css我的公共文件夹中。可以使用 Closure Compiler 对 JavaScript 文件执行类似的操作。