另一种方法:
假设你已经安装:
如果你不这样做,你可以像这样使用 npm 安装:
npm install gulp run-sequence main-bower-files gulp-inject --save-dev
将依赖项保存到 html 文件中
一旦你有了它,我们就开始配置 gulp 任务
//Here we only copy files to folder inside source code.
//In this case ./src/lib/
gulp.task("bower:copyfiles", function(cb){
return gulp.src(mainBowerFiles())
.pipe(gulp.dest('./src/lib'))
cb();
});
//This task is the one wich insert the script tag into
// HTML file. In this case is index.html and is in root
gulp.task('bower:insertfiles', function(cb){
return gulp.src('./src/index.html') //file with tags for injection
.pipe(inject(gulp.src(['./src/lib/*.js', './src/lib/*.css'], {read: false}), {
starttag: '<!-- bower:{{ext}} -->',
endtag: '<!-- endbower -->',
relative:true
}
))
.pipe(gulp.dest('./src')); //where index.html will be saved. Same dir for overwrite old one
})
//And this task will launch the process of copy and include into index.html
gulp.task('bower:buildlib', function(cb) {
runSequence('bower:copyfiles', 'bower:insertfiles',cb);
})
现在我们已经完成了一半的过程,我们需要将标签插入到 index.html 中,让 gulp 知道必须在哪里包含内容
<html>
<head>
<meta charset="utf-8">
<title></title>
<!-- bower:css -->
<!-- HERE WILL BE INSERTED THE CODE. -->
<!-- endbower -->
</head>
<body>
<!-- bower:js -->
<!-- HERE WILL BE INSERTED THE CODE. -->
<!-- endbower -->
</body>
</html>
最后一步是在命令行中运行我们的任务
gulp bower:buildlib
注意事项:
已知某些使用 bower 安装的库具有不同的文件配置。 f.e.:当您安装引导程序时,不包含 css 文件,因为内部 bower.json(在 bower_components 上的库文件夹中或您拥有的任何内容中)是以这种方式设置的。您可以在项目根目录的 bower.json 中覆盖这些选项来解决这个问题,像这样添加它(相同的引导示例):
"overrides":{
"bootstrap":{
"main":[
"dist/js/bootstrap.js",
"dist/css/bootstrap.min.css",
"less/bootstrap.less"
]
}
}
通过这种方式,您可以设置包含哪些文件,哪些不包含。