我正在模仿John Papa在Gulp上出色的Pluralsight课程的代码.
当我使用John的课程中显示的代码时:
.pipe(jsFilter)
.pipe($.uglify())
.pipe(jsFilter.restore())
Run Code Online (Sandbox Code Playgroud)
我在第3行代码上遇到错误:
TypeError: Object #<StreamFilter> has no method 'restore'
Run Code Online (Sandbox Code Playgroud)
当我使用gulp-filter自述文件中显示的代码时
.pipe(jsFilter)
.pipe($.uglify())
.pipe(jsFilter.restore)
Run Code Online (Sandbox Code Playgroud)
我收到一个错误,它不能管道到未定义.
根据我在网上找到的内容,这两种模式都适用于其他模式.关于为什么会发生这种情况的任何线索?
这是整个任务,如果这有帮助,并且控制台日志记录指示在过滤器恢复调用之前一切正常.
如果有帮助,这是整个任务:
gulp.task('build-dist', ['inject', 'templatecache'], function() {
log('Building the distribution files in the /dist folder');
var assets = $.useref.assets({searchPath: './'});
var templateCache = config.temp + config.templateCache.file;
var jsFilter = $.filter('**/*.js');
return gulp
.src(config.index)
.pipe($.plumber({errorHandler: onError}))
.pipe($.inject(gulp.src(templateCache, {read: false}), {
starttag: '<!-- inject:templates:js -->'
}))
.pipe(assets)
.pipe(jsFilter)
.pipe($.uglify())
.pipe(jsFilter.restore())
.pipe(assets.restore())
.pipe($.useref())
.pipe(gulp.dest(config.dist));
});
Run Code Online (Sandbox Code Playgroud)
小智 11
restoregulp-filter的2.x和3.x版本之间的工作方式有所改变.
看来你正在使用3.x分支,所以为了使用restore你必须在定义过滤器时设置restore选项true:
var jsFilter = $.filter('**/*.js', {restore: true});
Run Code Online (Sandbox Code Playgroud)
那你就能做到
.pipe(jsFilter.restore)
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请查看文档的此部分以获取最新版本的gulp-filter:
https://github.com/sindresorhus/gulp-filter/tree/v3.0.1#restoring-filtered-files