我是新手,可以通过youtube教程来学习它。我陷入了gulp watch命令,该命令应该跟踪我的js文件,但无法正常工作。我在StackOverflow上发现了许多类似的问题,但没有一个帮助我。
这是我的代码
Package.json
{
"name": "gulp-tutorial",
"version": "1.0.0",
"description": "This is gulp tutorial",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"gulpfile",
"scss",
"js"
],
"author": "Fahad Shaikh",
"license": "GPL-3.0",
"devDependencies": {
"gulp": "^4.0.2",
"gulp-autoprefixer": "^7.0.1",
"gulp-rename": "^1.4.0",
"gulp-sass": "^4.0.2",
"gulp-sourcemaps": "^2.6.5",
"gulp-watch": "^5.0.1"
}
}
gulpfile.js
var gulp = require('gulp');
var rename = require('gulp-rename');
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var sourcemaps = require('gulp-sourcemaps');
var watch = require('gulp-watch');
var jsDIST = './dist/js/';
gulp.task('js', function () {
return gulp.src(jsSRC)
.pipe(gulp.dest(jsDIST));
});
gulp.task('default', gulp.series('js'));
gulp.task('watch', gulp.series('default'), function () {
gulp.watch('./src/js/**/*.js', ['js']);
});
根据Internet上的教程和文档,在运行gulp watch
之后,我不应该重新获得CLI的控制权,但是一旦watch task
完成后,我就可以运行另一个命令。
答案 0 :(得分:1)
由于您使用的是gulp v4,请尝试以下方法:
// you don't need this and you aren't using it anyway
// var watch = require('gulp-watch');
var jsDIST = './dist/js/';
// gulp.task('js', function () {
// return gulp.src(jsSRC)
// .pipe(gulp.dest(jsDIST));
// });
function js() {
return gulp.src(jsSRC)
.pipe(gulp.dest(jsDIST));
}
// your main problem was in this task
// gulp.task('watch', gulp.series('default'), function () {
// gulp.watch('./src/js/**/*.js', ['js']); // array is incorrect in gulp v4
// });
function watch() {
gulp.watch('./src/js/**/*.js', js);
};
// gulp.task('default', gulp.series('js')); // 'taskName must be in quotes'
gulp.task('default', gulp.series(js, watch)); // functionName not in quotes
现在,您只需使用gulp
命令即可运行它,而无需执行其他任何操作。