我试图弄清楚如何使用Gulp,但我一直遇到一个错误,我不知道如何解决
当我使用它们的函数编译我的SCSS和JS文件时,它会正常编译,
但是当我尝试设置javascript gulp.task('watch')
或javascript gulp.task('default')
时,我会收到错误消息
这是我的gulpfile.js
const gulp = require('gulp');
const babel = require('gulp-babel');
const cssnano = require('gulp-cssnano');
const sass = require('gulp-sass');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');
const myFiles = [
'./app/js/vendors/*.js',
'./app/js/functions.js',
'./app/js/view.js'
]
gulp.task('sass', function () {
return gulp.src('./app/style.scss')
.pipe(sass())
.pipe(cssnano())
.pipe(gulp.dest('./dist/css'))
})
gulp.task('js', function () {
return gulp.src(myFiles)
.pipe(babel({
presets: ['@babel/env']
}))
.pipe(concat('all.js'))
.pipe(uglify())
.pipe(gulp.dest('dist'));
})
gulp.task('watch', function () {
gulp.watch('./app/*.scss', ['sass']);
gulp.watch('./app/js/**/*.js', ['js']);
});
gulp.task('default', ['sass', 'js', 'watch']);
当我发送$ gulp default
时,我收到了以下错误信息:
assert.js:339
throw err;
^
AssertionError [ERR_ASSERTION]: Task function must be specified
at Gulp.set [as _setTask] (C:\Program Files (x86)\Ampps\www\gulp\node_modules\undertaker\lib\set-task.js:10:3)
at Gulp.task (C:\Program Files (x86)\Ampps\www\gulp\node_modules\undertaker\lib\task.js:13:8)
at Object.<anonymous> (C:\Program Files (x86)\Ampps\www\gulp\gulpfile.js:40:6)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
如果我删除 default
函数并尝试运行 $ gulp watch
[10:19:14] Using gulpfile C:\Program Files (x86)\Ampps\www\gulp\gulpfile.js
[10:19:14] Starting 'watch'...
[10:19:14] 'watch' errored after 3.4 ms
[10:19:14] Error: watching ./app/*.scss: watch task has to be a function (optionally generated by using gulp.parallel or gulp.series)
at Gulp.watch (C:\Program Files (x86)\Ampps\www\gulp\node_modules\gulp\index.js:31:11)
at C:\Program Files (x86)\Ampps\www\gulp\gulpfile.js:35:10
at taskWrapper (C:\Program Files (x86)\Ampps\www\gulp\node_modules\undertaker\lib\set-task.js:13:15)
at bound (domain.js:402:14)
at runBound (domain.js:415:12)
at asyncRunner (C:\Program Files (x86)\Ampps\www\gulp\node_modules\async-done\index.js:55:18)
at process._tickCallback (internal/process/next_tick.js:61:11)
编辑:如下建议,我更改了gulpfile.js,但现在出现了不同的错误。
gulp.task('watch', function () {
gulp.watch('./app/*.scss', 'sass');
gulp.watch('./app/js/**/*.js', 'js');
});
gulp.task('default', gulp.series('sass', 'js', 'watch'));
错误:
$ gulp default
[13:55:37] Using gulpfile C:\Program Files (x86)\Ampps\www\gulp\gulpfile.js
[13:55:37] Starting 'default'...
[13:55:37] Starting 'sass'...
[13:55:37] Finished 'sass' after 153 ms
[13:55:37] Starting 'js'...
[13:55:40] Finished 'js' after 2.49 s
[13:55:40] Starting 'watch'...
[13:55:40] 'watch' errored after 1.84 ms
[13:55:40] Error: watching ./app/*.scss: watch task has to be a function (optionally generated by using gulp.parallel or gulp.series)
at Gulp.watch (C:\Program Files (x86)\Ampps\www\gulp\node_modules\gulp\index.js:31:11)
at C:\Program Files (x86)\Ampps\www\gulp\gulpfile.js:40:10
at taskWrapper (C:\Program Files (x86)\Ampps\www\gulp\node_modules\undertaker\lib\set-task.js:13:15)
at bound (domain.js:402:14)
at runBound (domain.js:415:12)
at asyncRunner (C:\Program Files (x86)\Ampps\www\gulp\node_modules\async-done\index.js:55:18)
at process._tickCallback (internal/process/next_tick.js:61:11)
[13:55:40] 'default' errored after 2.65 s