今天早上,我的gulp构建脚本开始失败;除了在项目目录中正常工作外,我没有对我的系统进行任何其他更改。
我有一个带有各种HTML片段,图像,javascript,LESS的/ src目录,我正在使用gulp来编译/连接/复制它们并将它们放在/ web目录中。 'default'任务运行一个'build'任务,它调用所有其他任务,然后设置gulp.watch来监视目录的变化,并运行相应的任务是找到更改。
在每个任务开始时,我使用del()来清理目标文件夹。但是,当将内容放在目标文件夹中时,任务失败并显示“EEXIST”错误,或者任务正常完成,并且某些文件在输出目录中随机丢失。
这是我的命令提示符,它显示随机失败的任务:
> user$ gulp
[11:58:02] Using gulpfile PROJECTPATH/gulpfile.js
[11:58:02] Starting 'styles'...
[11:58:02] Starting 'scripts'...
[11:58:02] Starting 'images'...
[11:58:02] Starting 'fonts'...
[11:58:02] Starting 'html'...
[11:58:03] Finished 'styles' after 726 ms
[11:58:03] gulp-imagemin: Minified 1 image (saved 7.38 kB - 10.4%)
[11:58:03] Finished 'fonts' after 714 ms
[11:58:03] Finished 'images' after 722 ms
[11:58:03] Finished 'scripts' after 741 ms
[11:58:03] Finished 'html' after 737 ms
[11:58:03] Starting 'build'...
[11:58:03] Finished 'build' after 9.61 μs
[11:58:03] Starting 'default'...
[11:58:03] Finished 'default' after 125 ms
> user$ gulp
[11:58:22] Using gulpfile PROJECTPATH/gulpfile.js
[11:58:22] Starting 'styles'...
[11:58:22] Starting 'scripts'...
[11:58:22] Starting 'images'...
[11:58:22] Starting 'fonts'...
[11:58:22] Starting 'html'...
[11:58:22] 'scripts' errored after 86 ms
[11:58:22] Error: EEXIST, mkdir 'PROJECTPATH/web/project_files/js'
[11:58:22] 'fonts' errored after 74 ms
[11:58:22] Error: ENOENT, open 'PROJECTPATH/web/project_files/fonts/glyphicons-halflings-regular.eot'
[11:58:23] 'html' errored after 144 ms
[11:58:23] Error: EEXIST, mkdir 'PROJECTPATH/web/project_files/templates/instruction'
[11:58:23] Finished 'styles' after 728 ms
[11:58:23] gulp-imagemin: Minified 1 image (saved 7.38 kB - 10.4%)
[11:58:23] Finished 'images' after 736 ms
> user$ gulp
[11:58:28] Using gulpfile PROJECTPATH/gulpfile.js
[11:58:28] Starting 'styles'...
[11:58:28] Starting 'scripts'...
[11:58:28] Starting 'images'...
[11:58:28] Starting 'fonts'...
[11:58:28] Starting 'html'...
[11:58:28] Finished 'styles' after 729 ms
[11:58:28] Finished 'fonts' after 707 ms
[11:58:28] Finished 'scripts' after 737 ms
[11:58:28] gulp-imagemin: Minified 1 image (saved 7.38 kB - 10.4%)
[11:58:28] Finished 'images' after 733 ms
[11:58:28] Finished 'html' after 740 ms
[11:58:28] Starting 'build'...
[11:58:28] Finished 'build' after 9.73 μs
[11:58:28] Starting 'default'...
[11:58:28] Finished 'default' after 120 ms
> user$ gulp
[11:58:34] Using gulpfile PROJECTPATH/gulpfile.js
[11:58:34] Starting 'styles'...
[11:58:34] Starting 'scripts'...
[11:58:34] Starting 'images'...
[11:58:34] Starting 'fonts'...
[11:58:34] Starting 'html'...
[11:58:34] 'scripts' errored after 95 ms
[11:58:34] Error: EEXIST, mkdir 'PROJECTPATH/web/project_files/js'
[11:58:34] 'fonts' errored after 84 ms
[11:58:34] Error: ENOENT, open 'PROJECTPATH/web/project_files/fonts/glyphicons-halflings-regular.eot'
[11:58:34] 'html' errored after 143 ms
[11:58:34] Error: EEXIST, mkdir 'PROJECTPATH/web/project_files/templates/instruction'
[11:58:35] Finished 'styles' after 737 ms
[11:58:35] gulp-imagemin: Minified 1 image (saved 7.38 kB - 10.4%)
[11:58:35] Finished 'images' after 742 ms
这是我正在使用的gulp文件:
var gulp = require('gulp'),
less = require('gulp-less'),
autoprefixer = require('gulp-autoprefixer'),
minifycss = require('gulp-minify-css'),
jshint = require('gulp-jshint'),
uglify = require('gulp-uglify'),
imagemin = require('gulp-imagemin'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
fileinclude = require('gulp-file-include'),
cache = require('gulp-cache'),
gutil = require('gulp-util'),
rename = require('gulp-rename'),
merge = require('merge-stream'),
del = require('del'),
libraries = [
'src/js/jquery-*.js',
'src/js/bootstrap/transition.js',
'src/js/bootstrap/collapse.js',
'src/js/bootstrap/modal.js',
'src/js/bootstrap/affix.js',
'src/js/bootstrap-datepicker.js',
'src/js/bootbox.js',
'src/js/chosen.jquery.js',
'src/js/underscore.js',
'src/js/sha1.js',
'src/js/DeviceIdConverter.js',
'src/js/custom.js'
],
ieLibraries = [
'src/js/html5shiv.js',
'src/js/respond.js'
],
appConfig = [
'src/js/country.json',
'src/js/locations.json',
'src/js/make_model.json',
'src/js/project.js'
];
gulp.task('styles', function() {
del(['web/project_files/css']);
return gulp.src('src/less/project.less')
.pipe(less())
.pipe(gulp.dest('web/project_files/css'))
});
gulp.task('scripts', function() {
del(['web/project_files/js']);
var libraryStream = gulp.src(libraries)
.pipe(concat('libraries.js')),
ieLibraryStream = gulp.src(ieLibraries)
.pipe(concat('ie8.js')),
appConfigStream = gulp.src(appConfig)
.pipe(rename(function(path) {
path.extname = '.js';
}));
return merge(libraryStream, ieLibraryStream, appConfigStream)
.pipe(gulp.dest('web/project_files/js'));
});
gulp.task('images', function() {
del(['web/project_files/img']);
return gulp.src('src/img/**/*.+(jpg|png)')
.pipe(imagemin({optimizationLevel: 3, progressive: true, interlaced: true }))
.pipe(gulp.dest('web/project_files/img'))
});
gulp.task('fonts', function() {
del(['web/project_files/fonts']);
return gulp.src('src/fonts/**/*.+(eot|svg|ttf|woff|otf)')
.pipe(rename(function(path) {
path.dirname = '';
}))
.pipe(gulp.dest('web/project_files/fonts'));
});
gulp.task('html', function() {
del(['web/project.htm']);
del(['web/project_files/templates']);
var htmlStream = gulp.src('src/html/index.html')
.pipe(fileinclude())
.pipe(rename('project.htm'));
var templateStream = gulp.src('src/html/templates/**/*.html')
.pipe(rename(function(path) {
path.dirname = "/project_files/templates/"+path.dirname;
path.extname = ".tpl";
}));
return(merge(htmlStream, templateStream))
.pipe(gulp.dest('web'));
});
gulp.task('build', ['styles', 'scripts', 'images', 'fonts', 'html'], function() {
});
gulp.task('default', ['build'], function() {
gulp.watch('src/html/**/*.html', ['html']);
gulp.watch('src/less/**/*.less', ['styles']);
gulp.watch('src/js/**/*.js', ['scripts']);
gulp.watch('src/img/**/*.+(jpg|png)', ['images']);
gulp.watch('src/fonts/**/*.+(eot|svg|ttf|woff|otf)', ['fonts']);
});
错误消息显示目标位置存在,但是del()不应该在每个子任务开始时删除目标目录吗?
更新:
这是我现在构建我的任务的方式,这似乎正在快速正确地工作:
gulp.task('scripts', function() {
del(['web/js'], function(err) {
if (err) return;
return gulp.src('src/js/**/*.js')
.pipe(concat('mything.js'))
.pipe(gulp.dest('web/js'));
});
});
答案 0 :(得分:29)
不应该在每个子任务开始时删除目标目录吗?
是的,但是你最终在del完成之前创建了新东西(它是异步的)。
我创建了一个单独的任务,只有在完成构建时才将其用作依赖项。
gulp.task('clean', function(cb) {
del(['something/**'], cb);
});
..或使用del.sync