gulp-sass 只编译变动文件(增量编译)

项目使用gulp来处理日常构建,但是当样式文件越来越多后如果每次都是全量编译会导致效率及其差,那么就来解决一下吧~

问题:

gulp-sass每次都会全量编译路径下所有*.scss

解决方案:

每次只将变动的文件进行编译,不再全量编译。

解决这个问题主要使用gulp-watch插件,github地址npm地址
我比较喜欢使用gulp-debug查看当前管道输出,很方便,npm地址
接下在就直接贴出代码了,首先看下引入的依赖

const gulp = require('gulp');
const sass = require('gulp-sass');
const watch = require('gulp-watch');
const debug = require('gulp-debug');
const path = require('path');
const join = src => path.join('app', src);

没啥毛病,继续。

gulp.task('watch:different-scss', () => {

  const baseDir = '/public/css_route';
  const scssPath = join(`${baseDir}/*.scss`);

  // ignoreInitial 首次启动不触发文件变动callback
  watch(scssPath, { ignoreInitial: true }, file => {
	// 要注意的是 file 参数类型为 Vinyl file object
	// 获取当前变动文件的文件名
    const basePath = path.basename(file.path);
    const filePath = join(`${baseDir}/${basePath}`);

    gulp.src(filePath)
      .pipe(debug({ title: 'Find Different: ' }))
      .pipe(sass().on('error', sass.logError))
      .pipe(debug({ title: 'Output Different: ' }))
      .pipe(gulp.dest(join('/public/styles')));

  })
  
});

补充说一下,在watchcallback中返回的参数类型是Vinyl,并不是普通的文件流(stream)
Vinyl github

# 接下在run task就好啦~
$ gulp watch:different-scss

贴图看下效果:
bash
Ok~完事收工!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

unstorm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值