Generate a mined file for each file within a folder

0

I configured the following task to generate a minified file:

gulp.task('frontend-js', function () {
    return gulp.src([
        'bower_components/jquery/dist/jquery.js',
        'bower_components/jquery-ui/jquery-ui.js',
        'bower_components/bootstrap/dist/js/bootstrap.js',
        'src/AppBundle/Resources/public/Frontend/js/main.js'
    ])
        .pipe(concat('main.min.js'))
        .pipe(uglify())
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest('web/js'));
});

The above script generates a single minified file.

I want to generate a minified file for each .js file that exists inside the src/AppBundle/Resources/public/Frontend/js/ folder, except for the main.js file that was included in the above task.

For example:

  

src / AppBundle / Resources / public / Frontend / js / test.js = > web / js / teste.min.js   src / AppBundle / Resources / public / Frontend / js / teste2.js = > web / js / teste2.min.js   src / AppBundle / Resources / public / Frontend / js / teste3.js = > web / js / teste3.min.js

Is it possible to do this with gulp?

    
asked by anonymous 20.06.2016 / 14:21

1 answer

1

Yes. It is enough that your src attribute excludes this particular file. In a simple way, this could be resolved with the following task :

gulp.task('js', function(){
    return gulp.src([
            'src/AppBundle/Resources/public/Frontend/js/*.js',
            '!src/AppBundle/Resources/public/Frontend/js/main.js'
         ])
        .pipe(uglify())
        .pipe(rename({
            suffix: '.min'
        }))
        .pipe(gulp.dest('web/js'))
});

Notice the negation ( ! ) in the src attribute, where I say that after I have selected all the .js files, one must be removed. Also, I've removed your concat() method, to avoid concatenating all files in one. Finally, note that this task only scans files directly inside the /js folder. Files inside folders inside this folder should be searched with the **

    
29.06.2016 / 21:03