Search code examples
angularjsgulp

Gulp v3.8.8 will not concat controller folder


I want to exclude controller dir from my js folder and keep the controller files js on the same condition in dist folder.

Example :

js/controller/order/controller.js

js/controller/item/controller.js

So when i build the project, it should be in the dist folder with same structure, but not a single js file.

gulp.task('js', function() {
    gulp.src('./styles')
        .pipe(concat('scripts.js'))
        .pipe(gulp.dest('./dist/js'))
        .pipe(browserSync.reload({
            stream: true
        }));
});

Solution

  • You can exclude your controller folder with the negated blob:

    # example structure 
    src
     |--gulpfile.js
     |
     |--include
     |    |-- index.js
     |    `-- another.js
     |
     `--exclude
          `-- exclude.js
    

    then in your gulp file:

    #gulpfile.js
    const gulp = require('gulp');
    const merge = require('merge-stream');
    const concat = require('gulp-concat');
    
    gulp.task('js', function() {
      const copy = gulp.src('./src/exclude/**/*.js')
        .pipe(gulp.dest('./dist/js'))
    
      const build = gulp.src(['./src/**/*.js', '!./src/exclude/**/*.js'])
        .pipe(concat('concat.js'))
        .pipe(gulp.dest('./dist/js'))
    
      return merge(copy, build);
    });
    

    Or if you don't want them to be in a single task:

    const gulp = require('gulp');
    const concat = require('gulp-concat');
    
    gulp.task('concatJs', function() {
      return gulp.src(['./src/**/*.js', '!./src/exclude/**/*.js'])
        .pipe(concat('concat.js'))
        .pipe(gulp.dest('./dist/js'))
    });
    
    gulp.task('copyJs', function() {
      return gulp.src('./src/exclude/**/*.js')
      .pipe(gulp.dest('./dist/js'))
    })
    
    gulp.task('js', ['concatJs', 'copyJs']);
    

    Result:

    dist
      `--js
          |--concat.js
          `--exclude.js