【发布时间】:2018-12-24 02:37:52
【问题描述】:
我正在执行 gulp.watch 作为 .NET Core 项目中预编译脚本的一部分。在执行dotnet run 时,gulp.watch 调用似乎阻塞了线程,因此应用程序根本无法启动。
如何告诉gulp.watch 将句柄返回给线程?
我创建了一个最小的工作示例来重现问题,使用dotnet new 并通过npm 安装gulp。
这是我的gulpfile.js:
var gulp = require('gulp');
gulp.task('copy', () => {
gulp.src("watch_src/foo.txt")
.pipe(gulp.dest("watch_dst/"));
});
gulp.task('watch', () => {
var watcher = gulp.watch("watch_src/foo.txt", ['copy']);
watcher.on('change', function(){
console.log('foo.txt changed!');
});
});
gulp.task('default', ['watch']);
我的Program.cs 文件如下所示:
using System;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
我的project.json 文件如下所示:
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"imports": "dnxcore50"
}
},
"scripts": {
"precompile": "gulp"
}
}
执行dotnet run 将执行预编译脚本,并且对foo.txt 的更改由控制台上的事件处理程序反映。但是应该打印Hello World! 的Main 方法没有被执行:
c:\Temp\src\gulptest>dotnet run
Compiling gulptest for .NETCoreApp,Version=v1.0
[17:03:48] Using gulpfile c:\Temp\src\gulptest\gulpfile.js
[17:03:48] Starting 'watch'...
[17:03:48] Finished 'watch' after 11 ms
[17:03:48] Starting 'default'...
[17:03:48] Finished 'default' after 43 ╬╝s
foo.txt changed!
[17:04:15] Starting 'copy'...
[17:04:15] Finished 'copy' after 34 ms
【问题讨论】: