简体   繁体   中英

Gulp compiling files to dest folder and src folder

My gulpfile is adding the compiled css and js files to both the dest (css / js) folders and the src (scss / coffee) folders.

The desired outcome is that the compiled files only print to the dest folders .

This is only happening when running gulp on Windows and not on Mac. Also, if I run the tasks without the watch, it compiles correctly; 'gulp styles' compiles the css files only to the css folder - 'gulp scripts' compiles the js files only to the js folder.

var gulp = require('gulp'),
    plumber = require('gulp-plumber'),
    rename = require('gulp-rename'),
    autoprefixer = require('gulp-autoprefixer'),
    coffee = require('gulp-coffee'),
    jshint = require('gulp-jshint'),
    uglify = require('gulp-uglify'),
    minifycss = require('gulp-minify-css'),
    sass = require('gulp-sass');

gulp.task('styles', function () {
    return gulp.src(['scss/**/*.scss'])
        .pipe(plumber({
            errorHandler: function (error) {
                console.log(error.message);
                this.emit('end');
            }
        }))
        .pipe(sass())
        .pipe(autoprefixer('last 2 versions'))
        .pipe(gulp.dest('css/'))
        .pipe(rename({ suffix: '.min' }))
        .pipe(minifycss())
        .pipe(gulp.dest('css/'))
});

gulp.task('scripts', function () {
    return gulp.src('coffee/**/*.coffee')
        .pipe(plumber({
            errorHandler: function (error) {
                console.log(error.message);
                this.emit('end');
            }
        }))
        .pipe(coffee({ bare: true }))
        .pipe(jshint())
        .pipe(jshint.reporter('default'))
        .pipe(gulp.dest('js/'))
        .pipe(rename({ suffix: '.min' }))
        .pipe(uglify())
        .pipe(gulp.dest('js/'))
});

gulp.task('default', function () {
    gulp.watch("scss/**/*.scss", ['styles']);
    gulp.watch("coffee/**/*.coffee", ['scripts']);
});

This is because you are calling gulp.dest twice once before minification and once after minification. Calling dest writes file to disk on the path provided.

If you just want minified versions

For Css

.pipe(autoprefixer('last 2 versions'))
      .pipe(rename({ suffix: '.min' }))
      .pipe(minifycss())
      .pipe(gulp.dest('css/'))

For Js

pipe(jshint.reporter('default'))
      .pipe(rename({ suffix: '.min' }))
      .pipe(uglify())
      .pipe(gulp.dest('js/'))

You possibly could try adding a . to the destination paths to help preserve the source path:

.pipe(gulp.dest('css/')) -> .pipe(gulp.dest('./css/'))

and

.pipe(gulp.dest('js/')) -> .pipe(gulp.dest('./js/'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM