我有一个gulp任务,它获取html文件,查找其中的脚本,缩小它们,应用修订并输出assets文件夹中的所有内容:
templates/index.html
<!-- build:js js/app.js -->
<script src="lib/js/a.js"></script>
<script src="lib/js/b.js"></script>
<!--endbuild -->
gulpfile.js
var useref = require('gulp-useref'),
filter = require('gulp-filter'),
uglify = require('gulp-uglify'),
rev = require('gulp-rev'),
revReplace = require('gulp-rev-replace');
gulp.task('build',function() {
var assets = useref.assets({searchPath: './'}),
jsapp = filter('**/app.js'),
return gulp
.src(gulp.src('templates/index_src.html'))
// collect all assets from source file by the means of useref
.pipe(assets)
//build js/app.js
.pipe(jsapp)
.pipe(uglify())
.pipe(jsapp.restore())
// Take inventory of the file names for future rev numbers
.pipe(rev())
// Apply the concat and file replacement with useref
.pipe(assets.restore())
.pipe(useref())
// Replace the file names in the html with rev numbers
.pipe(revReplace())
// output files
.pipe(gulp.dest('./'));
});
这很好但是将所有内容(js / app.js和index.html)输出到根目录(./);
是否有任何方法可以在gulp.dest
内输出条件并将js / app.js输出到项目的根文件夹,但将index.html输出到其他位置(例如./templates_cache
) ?
答案 0 :(得分:0)
还为html文件添加过滤器,以便只能将这些文件输出到其他文件夹:
var htmlFilter = filter('**/*.html')
将输出更改为:
// output for js files
.pipe(jsapp)
.pipe(gulp.dest('./'))
.pipe(jsapp.restore())
// output for html files
.pipe(htmlFilter)
.pipe(gulp.dest('./templates_cache'));