Generate a minified file for each file inside a folder

Asked

Viewed 114 times

0

I set 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 file .js that exists inside the folder src/AppBundle/Resources/public/Frontend/js/, except the file main.js which was included in the above task.

For example:

src/Appbundle/Resources/public/Frontend/js/teste.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

It is possible to do this with Gulp?

1 answer

1


Yes. Just that your attribute src delete this particular file. In a simple way, this could be solved 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'))
});

Understand the denial (!) in the attribute src, where I say, after I have selected all the files .js, one must be removed. See also that I removed your method concat(), to avoid concatenating all files into one. Finally, note that this task only checks files directly inside the folder /js. Files inside folders internal to this folder must be searched with the selector **

Browser other questions tagged

You are not signed in. Login or sign up in order to post.