Looking for way to copy files in gulp and rename based on parent directory

前端 未结 5 1807
自闭症患者
自闭症患者 2020-12-02 06:51

For each module I have some files that need to be copied over to the build directory, and am looking for a way to minimize the repeated code from this:

gulp         


        
相关标签:
5条回答
  • 2020-12-02 07:08

    Not the answer, but applicable to this question's appearance in search results.

    To copy files/folders in gulp

    gulp.task('copy', () => gulp
      .src('index.js')
      .pipe(gulp.dest('dist'))
    );
    
    0 讨论(0)
  • 2020-12-02 07:19

    copy files in parallel

    gulp.task('copy', gulp.parallel(
    () =>  gulp.src('*.json').pipe(gulp.dest('build/')),
    () =>  gulp.src('*.ico').pipe(gulp.dest('build/')),
    () =>  gulp.src('img/**/*').pipe(gulp.dest('build/img/')),
    )
    );
    
    0 讨论(0)
  • 2020-12-02 07:27

    The best way is to configure your base when sourcing files, like so:

    gulp.src('./client/src/modules/**/index.js', {base: './client/src/modules'})
      .pipe(gulp.dest('./build/public/js/'));
    

    This tells gulp to use the modules directory as the starting point for determining relative paths.

    (Also, you can use /**/*.js if you want to include all JS files...)

    0 讨论(0)
  • 2020-12-02 07:27

    Use for preserve input directory tree will be preserved.

    .pipe(gulp.dest(function(file) {
        var src = path.resolve(SRC_FOLDER);
        var final_dist = file.base.replace(src, '');
        return DIST_FOLDER + final_dist;
    }))
    

    Using this, you can put in the src: .src(SRC_FOLDER + '/**/*.js').

    The others answers not worked for me (like using base: on src()}, because some plugins flatten the directory tree.

    0 讨论(0)
  • 2020-12-02 07:31
    return gulp.src('./client/src/modules/(.*)/index.js')  
      .pipe(gulp.dest('./build/public/js/$1'));
    

    Worked for me !

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