Gulpjs combine two tasks into a single task

后端 未结 11 1035
攒了一身酷
攒了一身酷 2020-12-24 01:56

I currently have two tasks, that both compile sass files. I would still like to concat the two directories into separate files but it seems that it would be more maintainabl

相关标签:
11条回答
  • 2020-12-24 02:26

    Turns out that the site() function was returning an error. In order to fix it, I needed to make sure bootstrap() ran first so I ended up with this:

    // Compile Our Sass
    gulp.task('sass', function() {
      var bootstrap = function() {
        return gulp
          .src('./public/bower/bootstrap-sass/lib/*.scss')
          .pipe(sass())
          .pipe(concat('bootstrap.css'))
          .pipe(gulp.dest('./public/dist/css'));
      };
    
      var site = function() {
        return gulp
          .src('./public/src/scss/*.scss')
          .pipe(sass())
          .pipe(concat('site.css'))
          .pipe(gulp.dest('./public/dist/css'));
      };
    
      return bootstrap().on('end', site);
    });
    
    0 讨论(0)
  • 2020-12-24 02:29

    Its also possible, to create another task to compile other tasks by passing an array of task names like this:

    gulp.task('sass-file-one', function () {
         console.log('compiled sass-file-one')
    });
    gulp.task('sass-file-two', function () {
         console.log('compiled sass-file-two')
    });
    
    gulp.task('compile-all-sass', ['sass-file-one','sass-file-two']);
    

    then you can simply run gulp compile-all-sass

    0 讨论(0)
  • 2020-12-24 02:31

    In Gulp version 4, we have parallel, so you can do:

    // Compile Our Sass
    gulp.task('bootstrap-sass', function() {
      return gulp.src('./public/bower/bootstrap-sass/lib/*.scss')
        .pipe(sass())
        .pipe(concat('bootstrap.css'))
        .pipe(gulp.dest('./public/dist/css'));
    });
    
    gulp.task('site-sass', function() {
      return gulp.src('./public/app/scss/*.scss')
        .pipe(sass())
        .pipe(concat('site.css'))
        .pipe(gulp.dest('./public/dist/css'));
    });
    
    gulp.task('sass', gulp.parallel('bootstrap-sass', 'site-sass')); // Combine
    

    You can also write it as a single task by doing:

    gulp.task('sass', gulp.parallel(
      function() {
          return gulp.src('./public/bower/bootstrap-sass/lib/*.scss')
            .pipe(sass())
            .pipe(concat('bootstrap.css'))
            .pipe(gulp.dest('./public/dist/css'));
      },
      function() {
          return gulp.src('./public/app/scss/*.scss')
            .pipe(sass())
            .pipe(concat('site.css'))
            .pipe(gulp.dest('./public/dist/css'));
      }));
    

    I prefer the first method because it is easier to maintain

    0 讨论(0)
  • 2020-12-24 02:31

    Have you tried using merge-stream?

    merge = require('merge-stream');
    // Compile Our Sass
    gulp.task('sass', function() {
      var bootstrap = gulp
          .src('./public/bower/bootstrap-sass/lib/*.scss')
          .pipe(sass())
          .pipe(concat('bootstrap.css'))
          .pipe(gulp.dest('./public/dist/css'));
    
      var site = gulp
          .src('./public/src/scss/*.scss')
          .pipe(sass())
          .pipe(concat('site.css'))
          .pipe(gulp.dest('./public/dist/css'));
    
      return merge(bootstrap, site);
    
    });
    

    See https://blog.mariusschulz.com/2015/05/02/merging-two-gulp-streams for more details

    0 讨论(0)
  • 2020-12-24 02:36

    I just found a gulp plugin called gulp-all and tried it. It's simple to use.

    https://www.npmjs.com/package/gulp-all

    The documentation of the package says:

    var all = require('gulp-all')
    
    var styl_dir = 'path/to/styles/dir'
    var js_dir   = 'path/to/scripts/dir'
    
    function build() {
        return all(
            gulp.src(styl_dir + '/**/*')
                // build Styles 
                .pipe(gulp.dest('dist_dir')),
            gulp.src(js_dir + '/**/*')
                // build Scripts 
                .pipe(gulp.dest('dist_dir'))
        )
    }
    
    gulp.task('build', build);
    

    also you can put subtasks in an array:

    var scriptBundles = [/*...*/]
    
    function build(){
        var subtasks = scriptBundles.map(function(bundle){
            return gulp.src(bundle.src).pipe(/* concat to bundle.target */)
        })
        return all(subtasks)
    }
    
    0 讨论(0)
提交回复
热议问题