gpt4 book ai didi

node.js - Gulp Watching 在不更改文件的情况下创建无限循环

转载 作者:太空宇宙 更新时间:2023-11-03 22:30:45 25 4
gpt4 key购买 nike

与其他问题类似,在这个非常淡化的代码片段中,运行默认 gulp 任务(通过运行gulpnpm start) ;此代码片段创建了一个无限循环,一遍又一遍地运行脚本任务。这是gulpfile.js(实际上是目前的全部内容):

'use strict';

const gulp = require('gulp');

// COPY SCRIPTS TO BUILD FOLDER
gulp.task('scripts', function() {
return gulp.src('./scripts/**/*.js')
.pipe(gulp.dest('build/scripts'))
});

// WATCH FILES
gulp.task('watch', function() {
gulp.watch('./scripts/**/*.js', ['scripts']);
});

// DEFAULT
gulp.task('default', ['watch']);

更奇怪的是,无论是否重新构建build文件夹,脚本任务都会在调用npm start后立即执行!然后循环开始。

如果您好奇,这是我的 package.json 中粘贴的(也是唯一的)脚本对象:

  "scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "gulp"
},

我的目录中唯一的其他东西是一个 scripts 文件夹,其中包含 app.jshome.js 文件。显然,一旦运行此任务,就会创建 build 文件夹(如果尚未存在),并将上述两个文件复制到其中。

您可以看到我只是在根目录的第一级文件夹(称为脚本)中查找脚本,因此我不应该通过引用同一组脚本上的更改来无限循环。另外,即使我明确地指向一个具有相对路径(例如 ./scripts/home.js)的特定文件,这种情况仍然会发生。

我预计会感到尴尬,但我完全困惑了。

最佳答案

我发现的一些可能会导致错误的事情。

编辑-尝试使用 watch 插件 npm install --save-dev gulp-watch

// Try and declare your plugins like this for now.
var gulp = require('gulp'),
watch = require('gulp-watch');

// Provide a callback, cb
gulp.task('scripts', function(cb) {
// Dont use ./ on your src as watch can have a problem with this
return gulp.src('scripts/**/*.js')
.pipe(gulp.dest('./build/scripts'), cb); // call cb and dont forget ;
});

// remove ./ on watch
gulp.task('watch', function() {
gulp.watch('scripts/**/*.js', ['scripts']);
});

gulp.task('default', ['watch']);

所以这是非常奇怪的行为,但这应该可以解决问题。我唯一一次在 gulp 中使用 ./ 是在我的目的地。

还要记住 gulpfile 只是一个 JS,所以请记住你的分号等。

关于node.js - Gulp Watching 在不更改文件的情况下创建无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37474321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com