Convert gulp watch in gulp@3.9.1 to gulp@4(将 gulp@3.9.1 中的 gulp watch 转换为 gulp@4)
问题描述
我们正在从 gulp@3.9.1
切换到 gulp@4
并且在切换时遇到问题.当我们运行 gulp watch
时,我们会收到以下错误并试图找出解决方法.
We are switching from gulp@3.9.1
to gulp@4
and are having trouble switching over. When we run gulp watch
, we are getting the following errors and trying to figure out how to resolve it.
将 gulp watch
任务转换为使用 gulp@4
的正确方法是什么?
What is the proper way to convert the gulp watch
task to work with gulp@4
?
错误信息
AssertionError [ERR_ASSERTION]:任务从未定义:minify-css
AssertionError [ERR_ASSERTION]: Task never defined:
minify-css
命令:gulp watch
- 这应该按顺序运行
minify-js
然后minify-css
minify-js
应该在clean-scripts
成功完成后运行minify-css
应该在clean-css
成功完成后运行
- This should run
minify-js
thenminify-css
in order minify-js
should run afterclean-scripts
has completed successfullyminify-css
should run afterclean-css
has completed successfully
当前任务.
var gulp = require('gulp'),
cssmin = require('gulp-clean-css'),
clean = require('gulp-clean'),
uglify = require('gulp-uglify');
var src = {
js: 'js/some-dir/**/*.js',
css: 'css/some-dir/**/*.css'
};
var dest = {
js: 'js/dest/some-dir/**/*.js',
css: 'css/dest/some-dir/**/*.css'
};
gulp.task('clean-css', function() {
return gulp.src(dest.css)
.pipe(clean({read:false, force: true});
});
gulp.task('minify-css', ['clean-css'], function() {
gulp.src(src.css)
.pipe(cssmin())
.pipe(gulp.dest(dest.css));
});
gulp.task('clean-scripts', function() {
return gulp.src(dest.js)
.pipe(clean({read:false, force: true});
});
gulp.task('minify-js', ['clean-scripts'], function() {
gulp.src(src.js)
.pipe(uglify())
.pipe(gulp.dest(dest.js));
});
gulp.task('watch', ['minify-js', 'minify-css'], function() {
gulp.watch(src.js, ['minify-js']);
gulp.watch(src.css, ['minify-css']);
});
我们尝试这样做,但它导致了错误消息
gulp.task('watch', gulp.series('minify-js', 'minify-css', function() {
gulp.watch(src.js, ['minify-js']);
gulp.watch(src.css, ['minify-css']);
}));
推荐答案
我建议你转换你的 minify-js
, minify-css
, clean-scripts
和 clean-css
任务到函数:
I would recommend converting your minify-js
, minify-css
, clean-scripts
and clean-css
tasks to functions:
var dest = {
js: 'js/dest/some-dir/**/*.js',
css: 'css/dest/some-dir/**/*.css'
};
function cleanCss() {
return gulp.src(dest.css)
.pipe(clean({read:false, force: true});
});
function minifyCss() {
return gulp.src(src.css)
.pipe(cssmin())
.pipe(gulp.dest(dest.css));
});
function cleanScripts() {
return gulp.src(dest.js)
.pipe(clean({read:false, force: true});
});
function minifyJs() {
return gulp.src(src.js)
.pipe(uglify())
.pipe(gulp.dest(dest.js));
});
var minifyJsAndClean = gulp.series(minifyJs, cleanScripts);
var minifyCssAndClean = gulp.series(minifyCss, cleanCss);
var watchers = function (done) {
gulp.watch(src.js, minifyJs);
gulp.watch(src.css, minifyCss);
done();
}
gulp.task('watch', gulp.series(minifyJsAndClean, minifyCssAndClean, watchers));
这篇关于将 gulp@3.9.1 中的 gulp watch 转换为 gulp@4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 gulp@3.9.1 中的 gulp watch 转换为 gulp@4
基础教程推荐
- 在for循环中使用setTimeout 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 动态更新多个选择框 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01