I
I
Ivan Nikolaevich2015-04-15 16:02:23
Node.js
Ivan Nikolaevich, 2015-04-15 16:02:23

Why can gulp dest produce an empty file?

When working on a remote test server, a strange error appeared that intermittently (I could not identify the dependency), when executing the task, there is a chance that gulp.dest will spit out an empty file.
There was an assumption that this is due to the fact that several developers from under their users on the server launched their gulp watch and are actively working with them.
Below is the code for the entire GULP file. What could be the problem?

var gulp         = require('gulp'),
        compass      = require('gulp-compass'),
        plumber      = require('gulp-plumber'),
        autoprefixer = require('gulp-autoprefixer'),
        livereload   = require('gulp-livereload'),
        connect      = require('gulp-connect'),
        notify       = require('gulp-notify'),
        imagemin     = require('gulp-imagemin'),
        newer        = require('gulp-newer'),
        fileinclude  = require('gulp-file-include'),
        uglify       = require('gulp-uglify'),
        jshint       = require('gulp-jshint');
 
gulp.task('connect', function() {
        connect.server({
                root: 'build/',
                livereload: true
        });
});
 
gulp.task('css',function () {
        gulp.src('assets/sass/*.scss')
                .pipe(plumber())
                .pipe(compass({
                        css: 'build/css',
                        sass: 'assets/sass',
                        image: 'build/img/',
                        logging: true,
                        relative: true,
                        sourcemap: false,
                        time: true,
                        debug: false,
                        generated_images_path: 'build/img/'
                }))
                .pipe(autoprefixer({
                        browsers: ['last 12 versions','> 1%'],
                        cascade: false,
                        remove: false
                }))
                .pipe(gulp.dest('build/css'))
});
 
gulp.task('html', function () {
        gulp.src('assets/html/*.html')
                .pipe(fileinclude({
                        prefix: '@@',
                        basepath: '@file'
                }))
                .pipe(gulp.dest('build'))
});
 
 
gulp.task('js', function () {
        gulp.src('assets/js/project.js')
                .pipe(plumber())
                .pipe(gulp.dest('build/js/'));
});
 
 
gulp.task('img', function () {
        gulp.src('assets/img/**/*')
                .pipe(newer('build/img/'))
                .pipe(imagemin({
                        progressive: true
                }))
                .pipe(gulp.dest('build/img/'))
 
        gulp.src('assets/pic/**/*')
                .pipe(newer('build/pic/'))
                .pipe(imagemin({
                        progressive: true
                }))
                .pipe(gulp.dest('build/pic/'))
});
 
 
gulp.task('watch',function () {
        gulp.watch('assets/sass/**/*.scss',['css'])
        gulp.watch('assets/html/**/*.html',['html'])
        gulp.watch('assets/img/**/*',['img'])
        gulp.watch('assets/pic/**/*',['img'])
        gulp.watch('assets/js/*.js',['js'])
});
gulp.task('default', ['js', 'css', 'html', 'img', 'watch']);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egideus, 2018-03-13
@Egideus

the problem may be in the delay when saving the file, watch tries to access the file before it has time to be saved to disk.
The solution is to use either setTimeOut:
watch('*.scss', function() {
setTimeout(function(){
gulp.start('sass');
}, 100);
});
or a special parameter for watch:
watch('*.scss', {readDelay: 100}, function() {
gulp.start('sass');
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question