Gulp Gulp 恢复功能未结束?

x759pob2  于 2022-12-08  发布在  Gulp
关注(0)|答案(1)|浏览(187)

我在一个用来生成样式的特殊函数中遇到了返回问题。
我有3个样式表要生成,它们有不同的文件名/变量。所以我创建了一个函数,而不是复制代码。
我遇到的问题是这些任务没有完成。我尝试向sass任务添加回调函数,它确实完成了任务,但似乎generateStyles从未真正返回或完成。
我错过了什么?
先谢谢你,

gulp.task('sass', function () {
  generateStyle('summer');
  generateStyle('winter');
  generateStyle('spring');
});

function generateStyle(season) {
    if (season == undefined) {
        console.log('no season setted in generateStyle');
        return;
    }

    return gulp.src('assets/styles/all.scss')
     .pipe(insert.prepend('@import "assets/styles/seasons/'+ season +'.scss";'))
     .pipe(sass({includePaths: ['node_modules'],outputStyle: 'compressed'}).on('error', sass.logError))
     .pipe(sourcemaps.write())
      .pipe(rename('all-'+ season +'.css'))
      .pipe(gulp.dest(assetsOutputPath + 'css'));

      // should I add a callback here? or a promise?

}

// Main watch
gulp.task('default', function() {
  gulp.watch('assets/styles/**/*.scss',  gulp.series('sass'));
});
1tuwyuhd

1tuwyuhd1#

根据我的理解,任务本身必须返回一个流,所以我将样式表打包到单独的任务中,它可以工作,而不需要使用回调或promise。

// The function
function generateStyle(season) {
    if (season == undefined) {
        console.log('no season setted in generateStyle');
        return;
    }

    return gulp.src('assets/styles/all.scss')
     .pipe(insert.prepend('@import "assets/styles/seasons/'+ season +'.scss";'))
     .pipe(sass({includePaths: ['node_modules'],outputStyle: 'compressed'}).on('error', sass.logError))
     .pipe(sourcemaps.write())
      .pipe(rename('all-'+ season +'.css'))
      .pipe(gulp.dest(assetsOutputPath + 'css'));

}

// Individual tasks
gulp.task('styles-winter', function(){
    return generateStyle('winter');
});
gulp.task('styles-spring', function(){
    return generateStyle('spring');
});
gulp.task('styles-summer', function(){
    return generateStyle('summer');
});

// Main watch
gulp.task('default', function() {
  gulp.watch('assets/styles/**/*.scss',  gulp.series('styles-winter', 'styles-summer', 'styles-spring'));
});

相关问题