我正在尝试修复Gulp,但似乎无法正常工作。我的主要问题是console.log在终端中不可见。最重要的是,Gulp似乎没有读取文件夹名称。您知道可能是什么问题,为什么console.log在终端中不可见?
// Include gulp
var gulp = require('gulp');
// Include plugins
var path = require('path');
var sass = require('gulp-sass');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var cleanCSS = require('gulp-clean-css');
var folders = require('gulp-folders-4x');
var imagemin = require('gulp-imagemin');
// Config
var inputFolders = 'src'
var outputFolders = 'public'
var scssInput = 'styles';
var scssOutput = 'styles'
var jsInput = 'scripts';
var jsOutput = 'scripts';
var mediaInput = 'images';
var mediaOutput = 'images';
var backupImageFolder = 'backup-image';
var folder = '';
console.log(__dirname);
// compile scss into css
gulp.task('compile-scss',function() {
var processors = [
autoprefixer()
];
return gulp.src(path.join(inputFolders, folder, scssInput, '*.scss'))
.pipe(sass().on('error', sass.logError))
.pipe(postcss(processors))
.pipe(cleanCSS({compatibility:'*'}))
.pipe(gulp.dest(outputFolders + '/' + folder + '/' + scssOutput));
});
// concatenate and minify js
gulp.task('scripts', folders(inputFolders, function(folder) {
return gulp.src(inputFolders + '/' + folder + '/' + jsInput + '/*.js')
// .pipe(concat('scripts.js'))
.pipe(uglify())
.pipe(gulp.dest(outputFolders + '/' + folder + '/' + jsOutput));
}));
// deploy any media and losslessly optimise if possible
gulp.task('media', folders(inputFolders, function(folder) {
return gulp.src([inputFolders + '/' + folder + '/' + mediaInput + '/*', inputFolders + '/' + folder + '/' + mediaInput + '/**/*'])
.pipe(imagemin())
.pipe(gulp.dest(outputFolders + '/' + folder + '/' + mediaOutput));
}));
// copy across html
gulp.task('html', folders(inputFolders, function(folder) {
return gulp.src(inputFolders + '/' + folder + '/*.html')
.pipe(gulp.dest(outputFolders + '/' + folder))
}));
// copy across back-up image for the banner
gulp.task('copy-backup-image', folders(inputFolders, function(folder) {
return gulp.src(inputFolders + '/' + folder + '/' + backupImageFolder + '/*' )
.pipe(gulp.dest(outputFolders + '/' + folder + '/' + backupImageFolder))
}));
// Watch files for changes, run affected tasks
gulp.task('watch', function() {
gulp.watch([inputFolders + '/*/' + scssInput + '/*.scss'], gulp.series('compile-scss'));
gulp.watch([inputFolders + '/*/' + scssInput + '/**/*.scss'], gulp.series('compile-scss'));
gulp.watch([inputFolders + '/*/' + jsInput + '/*.js'], gulp.series('scripts'));
gulp.watch([inputFolders + '/*/' + mediaInput + '/*'], gulp.series('media'));
gulp.watch([inputFolders + '/*/' + mediaInput + '/**/*'], gulp.series('media'));
gulp.watch([inputFolders + '/*/*.html'], gulp.series('html'));
gulp.watch([inputFolders + '/*/' + backupImageFolder + '/*'], gulp.series('copy-backup-image'));
});
// Default task
// gulp.task('default', function(done) {
// gulp.series('compile-scss', 'scripts', 'media', 'html', 'copy-backup-image', 'watch');
// done();
// });
// gulp.task('default', gulp.series(['compile-scss', 'scripts', 'media', 'html', 'copy-backup-image', 'watch']));
// Default task
gulp.task(
'default',
gulp.series(gulp.parallel('compile-scss', 'scripts', 'media', 'html', 'copy-backup-image'), 'watch'),
function() {
console.log("Running build task in development mode, use gulp prod for production mode");
done();
}
);
答案 0 :(得分:1)
请参阅gulp.task签名https://gulpjs.com/docs/en/api/task#signature:
task([taskName], taskFunction)
您有:
gulp.task(
'default',
gulp.series(gulp.parallel('compile-scss', 'scripts', 'media', 'html', 'copy-backup-image'), 'watch'),
function() {
console.log("Running build task in development mode, use gulp prod for production mode");
done();
}
);
有效的是:
gulp.task(
'default',
gulp.series(.......) `watch),
function() {
console.log();
done();
}
);
因此,您的default task
有三个参数-console.log
将永远无法达到。将该函数移到gulp.series
中,如下所示:
gulp.task(
'default',
gulp.series(....., 'watch', function() { console.log......})
);
我尝试过:
gulp.task('default', gulp.series(
gulp.parallel(scripts),
watch,
function (done) { console.log("Here"); done() }
));
比您的版本更简单的版本,但结构与我建议的相同,并且可以运行。请注意done
的调用,而我使用的是函数名,例如scripts
,而不是任务名,例如'scripts'
,但这没什么区别。当然,如果您在之前的任务中有其他错误,或者没有终止,则不会到达最后一个console.log
。如果您在一组较为简单的已知良好任务上尝试使用我的default
结构,它将可以正常工作。
您的'compile-scss'
任务不再包含对folders
函数的调用-因此folder
是未定义的。