将文件编译到dest文件夹和src文件夹

Gulp compiling files to dest folder and src folder

本文关键字:文件夹 src dest 文件 编译      更新时间:2023-09-26

我的gulpfile正在将编译的css和js文件添加到dest(css/js)文件夹和src(scss/coffee)文件夹中。

期望的结果是编译的文件打印到目标文件夹

这仅在Windows上运行gulp而不是在Mac上运行时发生。此外,如果我在没有手表的情况下运行任务,它会正确编译;'gulp styles' 仅将 CSS 文件编译到 CSS 文件夹 - 'gulp scripts' 仅将 js 文件编译到 js 文件夹。

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']);
});

这是因为您在缩小之前和缩小之后调用gulp.dest两次。调用 dest 会将文件写入提供的路径上的磁盘。

如果您只想要缩小版本

对于 CSS

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

对于 Js

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

您可以尝试向目标路径添加.以帮助保留源路径:

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

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