我已将gulp更新至4.0.0版,请阅读说明https://github.com/gulpjs/gulp/tree/master/docs/getting-started,并完全重写了代码。
'use strict';
const { series,
parallel,
src,
dest,
watch } = require('gulp'),
sass = require('gulp-sass'),
autoprefixer = require('gulp-autoprefixer'),
gcmq = require('gulp-group-css-media-queries'),
sourcemaps = require('gulp-sourcemaps'),
notify = require('gulp-notify'),
gutil = require('gulp-util'),
ftp = require('vinyl-ftp'),
cleanCSS = require('gulp-clean-css'),
rename = require('gulp-rename'),
uglify = require('gulp-uglify'),
concat = require('gulp-concat'),
del = require('del'),
csscomb = require('gulp-csscomb'),
changed = require('gulp-changed'),
gulpif = require('gulp-if');
function comb () {
return src(path.scss.files)
.pipe(csscomb('.csscomb.json')
.on("error", notify.onError(function (error) {
return "File: " + error.message;
})))
.pipe(dest(path.scss.combFolder)
.on('end', () => { if(true) console.log(' --------------- completed COMB'); }));
}
function combUpdate () {
return src(path.scss.combFiles)
.pipe(dest(path.scss.folder)
.on('end', () => { if(true) console.log(' --------------- updated COMB -> SCSS'); }));
}
function scss () {
return src(path.scss.combFiles)
.pipe(sourcemaps.init())
.pipe(csscomb('.csscomb.json')
.on("error", notify.onError(function (error) {
return "File: " + error.message;
})))
.pipe(sass()
.on("error", notify.onError(function (error) {
return "File: " + error.message;
})))
.pipe(gcmq())
.pipe(autoprefixer({ browsers: ['last 5 versions', '> 1%'], cascade: true }))
.pipe(notify({ message: 'Compiled!', sound: false }))
.pipe(sourcemaps.write('./'))
.pipe(dest(path.css.folder)
.on('end', () => { if(true) console.log(' --------------- completed SCSS'); }));
}
function mincss () {
return src([path.css.files,
'!' + path.css.filesMin,
'!' + path.css.folder + '**/font-awesome.css',
'!' + path.css.folder + '**/normalize.css'
])
.pipe(cleanCSS({ specialComments: 'false' }))
.pipe(rename({ suffix: '.min' }))
.pipe(dest(path.css.folder)
.on('end', () => { if(true) console.log(' --------------- completed MINIFY (.min.css)'); }));
}
function minjs () {
return src([path.js.files, '!' + path.js.filesMin])
.pipe(uglify())
.pipe(rename({ suffix: '.min' }))
.pipe(dest(path.js.folder)
.on('end', () => { if(true) console.log(' --------------- completed UGLIFY (.min.js)'); }));
}
function processBuild (srcUrl, baseUrl, destUrl, newest) {
return src(srcUrl)
.pipe(gulpif(newest, changed(path.tmp.temp + baseUrl)))
.pipe(dest(destUrl + baseUrl));
}
function setBuild (destUrl, newest) {
processBuild(path.css.files, path.css.folder, destUrl, newest); /*CSS*/
processBuild(path.css.mapFiles, path.css.mapFolder, destUrl, newest); /*MAP*/
processBuild(path.html.files, path.html.folder, destUrl, newest); /*HTML*/
processBuild(path.js.files, path.js.folder, destUrl, newest); /*JS*/
return Promise.resolve('finished');
}
function tempBuild () { return setBuild(path.tmp.temp, false); }
function distBuild () { return setBuild(path.tmp.dist, false); }
function upldBuild (cb) {
setBuild(path.tmp.upld, true);
return Promise.resolve('finished');
cb();
}
function cleanTemp () { return del(path.tmp.temp, {force: true}); };
function cleanDist () { return del(path.tmp.dist, {force: true}); };
function cleanUpld () { return del(path.tmp.upld, {force: true}); }
function watchFiles() {
watch(path.scss.files,
series(comb, scss, mincss,
cleanUpld, upldBuild, cleanDist, distBuild, cleanTemp, tempBuild));
watch([path.js.files, '!' + path.js.filesMin],
series(minjs,
cleanUpld, upldBuild, cleanDist, distBuild, cleanTemp, tempBuild));
watch(path.html.files,
series(cleanUpld, upldBuild,
delay, upload2mbc,
cleanDist, distBuild, cleanTemp, tempBuild));
}
function delay (done) {
setTimeout(done, 100);
}
function upload2mbc() {
var upload = [path.tmp.upld + '**/*.*'];
return src( upload, { base: '', buffer: false } )
.pipe(conn.newer('/public_html/ext' ))
.pipe(conn.dest('/public_html/ext' )
.on('end', () => { if (true) console.log(' --------------- completed FILES UPLOADED'); }));
}
exports.default = watchFiles;
exports.upload = upload2mbc;
exports.updateSass = series(comb, combUpdate);
upload2mbc 不会将任何内容发送到服务器,因为 upldBuild 没有时间来构建文件夹。 http://prntscr.com/l8fi9g
watch(path.html.files,
series(cleanUpld, upldBuild,
upload2mbc,
cleanDist, distBuild, cleanTemp, tempBuild));
如果我分别运行两个任务-一切正常。 http://prntscr.com/l8fimr
function upldBuild () {
setBuild(path.tmp.upld, true);
return Promise.resolve('finished');
}
function upldBuild (cb) {
return setBuild(path.tmp.upld, true);
cb();
}
我已经挖了很多资料,但是我仍然不明白我的错误在哪里。预先感谢您的帮助。