How can I make it so that when I minify a project, I do not delete a file inside the _build folder?

Currently with these lines of code I am minifying my project.

        gulp.task('minify-js', function() {
          gulp.src('js/*.js')
            .pipe($.uglify())
            .pipe(gulp.dest('./_build/'));
        });

        gulp.task('minify-css', function() {
          gulp.src(['./css/**/*.css'])
            .pipe($.rename({suffix: '.min'}))
            .pipe($.minifyCss({keepBreaks:true}))
            .pipe(gulp.dest('./css/'))
            .pipe(gulp.dest('./_build/css/'));
        });

        gulp.task('minify-html', function() {
          var opts = {
            comments: true,
            spare: true,
            conditionals: true
          };

          gulp.src('./*.html')
            .pipe($.minifyHtml(opts))
            .pipe(gulp.dest('./_build/'));
        });

        gulp.task('fonts', function() {
          gulp.src('./fonts/**/*.{ttf,woff,eof,eot,svg}')
            .pipe($.changed('./_build/fonts'))
            .pipe(gulp.dest('./_build/fonts'));
        });

        gulp.task('server', function(done) {
          return browserSync({
            server: {
              baseDir: './'
            }
          }, done);
        });

        gulp.task('server-build', function(done) {
          return browserSync({
            server: {
              baseDir: './_build/'
            }
          }, done);
        });

        gulp.task('clean:build', function (cb) {
          del([
            './_build/'
            // if we don't want to clean any file we can use negate pattern
            //'!dist/mobile/deploy.json'
          ], cb);
        });

        require('events').EventEmitter.prototype._maxListeners = 100;


        gulp.task('usemin', function() {
          return gulp.src('./index.html')
            // add templates path
            .pipe($.htmlReplace({
              'templates': '<script type="text/javascript" src="js/templates.js"></script>'
            }))
            .pipe($.usemin({
              css: [$.minifyCss(), 'concat'],
              libs: [$.uglify()],
              nonangularlibs: [$.uglify()],
              angularlibs: [$.uglify()],
              controllers:[$.uglify()],
              contservicesapp:[$.uglify()],
              services:[$.uglify()],
              appcomponents: [$.uglify()],
              mainapp: [$.uglify()],
              templates:[$.uglify()],
              directives:[$.uglify()]
            }))
            .pipe(gulp.dest('./_build/'));
        });

When I minify it, it is inside a folder called "_build". Inside the _build folder I have a file called "cv.pdf". I have added it to this folder because when I minify it is not included. I would like that when the project is minified my file "cv.pdf" is not deleted.

How can I do it?

1 answer

  • answered 2017-11-12 22:51 Mark

    Try this:

    gulp.task('clean:build', function (cb) {
          del([
            './_build/**',
    
            '!./_build',
            '!./_build/cv.pdf'
    
          ], cb);
        });
    

    Adapted from del: excluding a file from a directory from deletion. Which explains how to delete an entire directory except for one file. This is probably the easiest way, you could also only specifically include only css, js, html, fonts and images if the above code doesn't work. Let me know.