gulp-filter - 使用 gulp-filter 时如何修复类型错误?

标签 gulp-filter

我正在模仿 John Papa 在 Gulp 上出色的 Pluralsight 类(class)中的代码。

当我使用约翰类(class)中显示的代码时:

.pipe(jsFilter)
.pipe($.uglify())
.pipe(jsFilter.restore())

我在第 3 行代码中收到错误消息:
TypeError: Object #<StreamFilter> has no method 'restore'

当我使用 gulp-filter 的自述文件中显示的代码时
.pipe(jsFilter)
.pipe($.uglify())
.pipe(jsFilter.restore)

我收到一个错误,它不能通过管道传输到 undefined。

根据我在网上找到的信息,这两种模式都适用于其他人。关于为什么会发生这种情况的任何线索?

这是整个任务,如果有帮助并且控制台日志记录表明一切正常,直到过滤器恢复调用。

如果有帮助,这里是整个任务:
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));
});

最佳答案

方式restore在 gulp-filter 的 2.x 和 3.x 版本之间,作品发生了变化。

看来您正在使用 3.x 分支,因此为了使用 restore你必须设置 restore选项 true定义过滤器时:

var jsFilter = $.filter('**/*.js', {restore: true});

然后你就可以做
.pipe(jsFilter.restore)

有关更多信息,请查看最新版本的 gulp-filter 文档的这一部分:

https://github.com/sindresorhus/gulp-filter/tree/v3.0.1#restoring-filtered-files

关于gulp-filter - 使用 gulp-filter 时如何修复类型错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32914640/

相关文章:

javascript - gulp-filter 没有正确过滤掉排除的文件

javascript - CSS 和 JS 缩小不适用于 gulp-filter、gulp-csso、gulp-uglify

javascript - Gulp-filter 正在过滤..什么都没有

css - 使用 gulp 替换单个文件中的字符串

javascript - gulp-filter 正在过滤一切

javascript - 一个流中的 Gulp-filter 不为目标传送所有文件