我试图弄清楚如何使用Gulp,但我不断收到一个我不知道如何解决的错误
当我使用它们的功能编译SCSS和JS文件时,它会正常编译
但是,当我尝试设置javascript gulp.task('watch')
或javascript gulp.task('default')
时,会收到错误消息msg
这是我的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
答案 0 :(得分:1)
这些是gulp v3语法:
gulp.task('watch', function () {
gulp.watch('./app/*.scss', ['sass']);
gulp.watch('./app/js/**/*.js', ['js']);
});
gulp.task('default', ['sass', 'js', 'watch']);
如果您使用的是gulp v4,请改用此版本:
gulp.task('watch', function () {
gulp.watch('./app/*.scss', gulp.series('sass'));
gulp.watch('./app/js/**/*.js', gulp.series('js'));
});
gulp.task('default', gulp.series('sass', 'js', 'watch'));
并搜索如何从gulp3迁移到gulp4。
答案 1 :(得分:1)
莱昂纳多的最后一条评论对我有用-gulp工作正常,然后突然停了下来。
我在每个gulp.task中都添加了一个返回值,它再次起作用:
var util = require('gulp-util'),
gulp = require("gulp"),
concat = require("gulp-concat"),
del = require("del");
gulp.task('localcss', async function () {
return gulp
.src(['wwwroot/css/TradingReplay/tradingreplay-local.css'])
.pipe(gulp.dest('wwwroot/dist'));
});