javascript – 如何将参数从gulp观察器传递给任务
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 如何将参数从gulp观察器传递给任务,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2054字,纯文字阅读大概需要3分钟。
内容图文
![javascript – 如何将参数从gulp观察器传递给任务](/upload/InfoBanner/zyjiaocheng/768/768ce68b50d34247888bd0d148338262.jpg)
如果我有这样的观察者:
gulp.watch('js/**/*.js').on('change', path => {
gulp.series(build, reload)();
});
……并且任务构建看起来像这样:
const build = done => {
return gulp
.src(path) // Use "path" here
.pipe(
rename({
dirname: ''
})
)
.pipe(uglify())
.pipe(gulp.dest('build'));
};
如何将路径参数传递给构建任务?
解决方法:
作为练习,我相信我按照你的意愿工作.但首先让我说限制源管道的传统方式是使用像gulp-newer这样的东西.你应该看看它是否能达到你想要的效果.
但是这里有一些可能对你有用的东西[未经过充分测试!]:
function build (path) {
return new Promise(resolve => {
// using setTimeout() to prove async/await is working as expected
setTimeout(() => {
resolve('resolved');
}, 2000);
// put your gulp.src pipeline here using 'path'
console.log("2 path = " + path);
});
};
function anotherTask (path) {
return new Promise(resolve => {
// put your gulp.src pipeline here
console.log("4 path = " + path); });
};
function testWatch () {
console.log("in testWatch");
// debounceDelay because gulp likes to call the watcher 2 or 3times otherwise
// see [gulp watch task running multiple times when a file is saved][2]
var watcher = gulp.watch('js/**/*.js', { debounceDelay: 2000 });
// I added the async/await because I wasn't sure those functions would be run in series
// as you wanted.
// With the event listener route I couldn't get gulp.series to work,
// so went with async/await.
watcher.on('change', async function(path, stats) {
console.log('1 File ' + path + ' was changed');
await build(path);
console.log("3 after build");
// I would assume that the **last** task in the chain doesn't need 'await'
// or to return a promise as in anotherTask
await anotherTask(path);
console.log("5 after anotherTask");
});
};
gulp.task('default', gulp.series(testWatch));
上面提到的gulp watch running multiple times代码.
输出(我的js watch src与你的不同):
in testWatch
1 File src\js\main.js was changed
2 path = src\js\main.js
3 after build
4 path = src\js\main.js
5 after anotherTask
1 File src\js\taxonomy.js was changed
2 path = src\js\taxonomy.js
3 after build
4 path = src\js\taxonomy.js
5 after anotherTask
内容总结
以上是互联网集市为您收集整理的javascript – 如何将参数从gulp观察器传递给任务全部内容,希望文章能够帮你解决javascript – 如何将参数从gulp观察器传递给任务所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。