0

我有一个 gulp 任务,它获取 html 文件,在其中找到脚本,缩小它们,应用修订并输出资产文件夹中的所有内容:

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)?

4

1 回答 1

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'));
于 2015-04-10T18:05:36.120 回答