gulp-cache:如何为LESS版本使用文件缓存

时间:2016-10-25 06:52:58

标签: maven gulp

我们正在使用gulp将所有LESS文件编译到Maven项目的目标/中。仅此任务需要大约51秒,因此我们希望加快速度并跳过未更改的LESS文件。我们需要一个文件缓存,因为从Maven调用gulp并且构建在IDE中运行,因此gulp进程不能保留在内存中。

充其量,缓存的CSS文件应该被复制到目标/即使/ target被Clean&删除了。建立。

这是我的代码:

var cache = require('gulp-cache');
var fileCache = new cache.Cache({ cacheDirName: 'gulp-cache' });

gulp.task('less', function () {
    return gulp.src([webappPath + '/**/*.less'])
            .pipe(fileCache('less'))
            .pipe(sourcemaps.init())
            .pipe(less({
                paths: [path.join(__dirname, 'less', 'includes')],
                plugins: [cleancss],
                relativeUrls: true
            }))
            .pipe(sourcemaps.write('.'))
            .pipe(gulp.dest(target));
    ;
});

.pipe(fileCache('less'))行遇到错误:

  

TypeError:fileCache不是函数。

https://www.npmjs.com/package/gulp-cache上的文件)

1 个答案:

答案 0 :(得分:2)

(1)gulp-cache插件需要包装您的less插件。这样,只有已更改的文件才会传递到less

(2)您不一定需要实例化自己的cache.Cache对象。如果不这样做,gulp-cache将为您创建一个。如果您想拥有多个缓存,则只需自己完成。在这种情况下,您可以使用cache.Cache选项传递fileCache对象。

gulp.task('less', function () {
  return gulp.src([webappPath + '/**/*.less'])
    .pipe(sourcemaps.init())
    .pipe(cache(less({
      paths: [path.join(__dirname, 'less', 'includes')],
      plugins: [cleancss],
      relativeUrls: true
    }), {
      fileCache: new cache.Cache({ cacheDirName: 'gulp-cache' })
    }))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest(target));
});
相关问题