Can I use a Gulp task with multiple sources and multiple destinations?

前端 未结 9 1280
遇见更好的自我
遇见更好的自我 2020-12-02 14:19

I have the following in my gulpfile.js:

   var sass_paths = [
        \'./httpdocs-site1/media/sass/**/*.scss\',
        \'./httpdocs-site2/media/sass/**/*         


        
相关标签:
9条回答
  • 2020-12-02 14:52

    Multiple sources with multiple destinations on gulp without using any extra plugins just doing concatenation on each js and css. Below code works for me. Please try it out.

     const gulp = require('gulp');
     const concat = require('gulp-concat');
     function task(done) {
        var theme = {
            minifiedCss: {
                 common: { 
                    src : ['./app/css/**/*.min.css', '!./app/css/semantic.min.css'], 
                    name : 'minified-bundle.css', 
                    dest : './web/bundles/css/' 
                }
            },
            themeCss:{
                common: { 
                     src : ['./app/css/style.css', './app/css/responsive.css'], 
                     name : 'theme-bundle.css', 
                     dest : './web/bundles/css/' 
                }
            },
            themeJs: {
                common: {
                    src: ['./app/js/jquery-2.1.1.js', './app/js/bootstrap.js'],
                    name: 'theme-bundle.js',
                    dest: './web/_themes/js/'
                }
            }
        }
        Object.keys(theme).map(function(key, index) {
            return gulp.src(theme[key].common.src)
            .pipe( concat(theme[key].common.name) )
            .pipe(gulp.dest(theme[key].common.dest));
        });
        done();
    }
    exports.task = task;
    
    0 讨论(0)
  • 2020-12-02 14:57

    You can use gulp-rename to modify where files will be written.

    gulp.task('sass', function() {
        return gulp.src(sass_paths, { base: '.' })
            .pipe(sass({errLogToConsole: true}))
            .pipe(autoprefixer('last 4 version'))
            .pipe(minifyCSS({keepBreaks:true}))
            .pipe(rename(function(path) {
                path.dirname = path.dirname.replace('/sass', '/css');
                path.extname = '.min.css';
            }))
            .pipe(gulp.dest('.'));
    });
    

    Important bit: use base option in gulp.src.

    0 讨论(0)
  • 2020-12-02 15:03

    you are going to want to use merge streams if you would like to use multiple srcs but you can have multiple destinations inside of the same one. Here is an example.

    var merge = require('merge-stream');
    
    
    gulp.task('sass', function() {
       var firstPath = gulp.src(sass_paths[0])
                   .pipe(sass({errLogToConsole: true}))
                   .pipe(autoprefixer('last 4 version'))
                   .pipe(minifyCSS({keepBreaks:true}))
                   .pipe(rename({ suffix: '.min'}))
                   .pipe(gulp.dest('./httpdocs-site1/media/css'))
                   .pipe(gulp.dest('./httpdocs-site2/media/css'));
       var secondPath = gulp.src(sass_paths[1])
                   .pipe(sass({errLogToConsole: true}))
                   .pipe(autoprefixer('last 4 version'))
                   .pipe(minifyCSS({keepBreaks:true}))
                   .pipe(rename({ suffix: '.min'}))
                   .pipe(gulp.dest('./httpdocs-site1/media/css'))
                   .pipe(gulp.dest('./httpdocs-site2/media/css'));
       return merge(firstPath, secondPath);
    });
    

    I assumed you wanted different paths piped here so there is site1 and site2, but you can do this to as many places as needed. Also you can specify a dest prior to any of the steps if, for example, you wanted to have one dest that had the .min file and one that didn't.

    0 讨论(0)
  • 2020-12-02 15:03

    For the ones that ask themselves how can they deal with common/specifics css files (works the same for scripts), here is a possible output to tackle this problem :

    var gulp = require('gulp');
    var concat = require('gulp-concat');
    var css = require('gulp-clean-css');
    
    var sheets = [
        { src : 'public/css/home/*', name : 'home.min', dest : 'public/css/compressed' },
        { src : 'public/css/about/*', name : 'about.min', dest : 'public/css/compressed' }
    ];
    
    var common = {
        'materialize' : 'public/assets/materialize/css/materialize.css'
    };
    
    gulp.task('css', function() {
        sheets.map(function(file) {
            return gulp.src([
                common.materialize, 
                file.src + '.css', 
                file.src + '.scss', 
                file.src + '.less'
            ])
            .pipe( concat(file.name + '.css') )
            .pipe( css() )
            .pipe( gulp.dest(file.dest) )
        }); 
    });
    

    All you have to do now is to add your sheets as the object notation is constructed.

    If you have additionnal commons scripts, you can map them by name on the object common, then add them after materialize for this example, but before the file.src + '.css' as you may want to override the common files with your customs files.

    Note that in the src attribute you can also put path like this :

    'public/css/**/*.css'

    to scope an entire descendence.

    0 讨论(0)
  • 2020-12-02 15:09

    I had success without needing anything extra, a solution very similar to Anwar Nairi's

    const p = {
      dashboard: {
        css: {
          orig: ['public/dashboard/scss/style.scss', 'public/dashboard/styles/*.css'],
          dest: 'public/dashboard/css/',
        },
      },
      public: {
        css: {
          orig: ['public/styles/custom.scss', 'public/styles/*.css'],
          dest: 'public/css/',
        },
        js: {
          orig: ['public/javascript/*.js'],
          dest: 'public/js/',
        },
      },
    };
    
    gulp.task('default', function(done) {
      Object.keys(p).forEach(val => {
        // 'val' will go two rounds, as 'dashboard' and as 'public'
        return gulp
          .src(p[val].css.orig)
          .pipe(sourcemaps.init())
          .pipe(sass())
          .pipe(autoPrefixer())
          .pipe(cssComb())
          .pipe(cmq({ log: true }))
          .pipe(concat('main.css'))
          .pipe(cleanCss())
          .pipe(sourcemaps.write())
          .pipe(gulp.dest(p[val].css.dest))
          .pipe(reload({ stream: true }));
      });
      done(); // <-- to avoid async problems using gulp 4
    });
    
    0 讨论(0)
  • 2020-12-02 15:14

    I think we should create 1 temporary folder for containing all these files. Then gulp.src point to this folder

    0 讨论(0)
提交回复
热议问题