gulp常用插件之gulp-cache使用

更多gulp常用插件使用请访问:gulp常用插件汇总


gulp-cache这是一款基于临时文件的gulp缓存代理任务。

更多使用文档请点击访问gulp-cache工具官网

安装

一键安装不多解释

npm install --save-dev gulp-cache

使用

简单使用:

import gulp from 'gulp';
import favicons from 'gulp-favicons';
import srcset from 'gulp-srcset';
import cache from 'gulp-cache';

gulp.task('favicon', () =>
    gulp.src('src/favicon.svg')
        .pipe(cache(
            // 目标插件,其输出将被缓存
            favicons(faviconsConfig),
            //`gulp-cache` 插件的选项.
            {
                //用桶存储缓存中的收藏夹图标。
                name: 'favicons'
            }
        ))
        .pipe(gulp.dest('./favicons'))
);

gulp.task('images', () =>
    gulp.src('src/**/*.{jpg,png,svg}')
        .pipe(cache(
            // 目标插件,其输出将被缓存
            srcset(srcsetRules),
            //`gulp-cache` 插件的选项.
            {
                // 存储桶以将图像存储在缓存中。
                name: 'images'
            }
        ))
        .pipe(gulp.dest('./images'))
);

复杂用法示例:

import fs from 'fs';
import gulp from 'gulp';
import jshint from 'gulp-jshint';
import cache from 'gulp-cache';

const jsHintVersion = '2.4.1';
const jshintOptions = fs.readFileSync('.jshintrc');

function makeHashKey(file) {
    //取消文件内容,jshint版本和选项
    return `${file.contents.toString('utf8')}${jshintVersion}${jshintOptions}`;
}

gulp.task('lint', () =>
    gulp.src('src/**/*.js')
        .pipe(cache(
            //目标插件,其输出将被缓存
            jshint('.jshintrc'),
            // `gulp-cache` 插件的选项.
            {
                key: makeHashKey,
                // 结果表明成功
                success(jshintedFile) {
                    return jshintedFile.jshint.success;
                },
                // 作为成功操作
                value(jshintedFile) {
                    // 将在下次运行任务时返回缓存命中的文件对象
                    return {
                        jshint: jshintedFile.jshint
                    };
                }
            }
        ))
        .pipe(jshint.reporter('default'))
});

猜你喜欢

转载自www.cnblogs.com/jiaoshou/p/12182281.html