javascript 当我输入 npm start 时如何启动 Gulp watch 任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28029929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to start Gulp watch task when I type npm start
提问by SoEzPz
I have a gulp.js file that includes:
我有一个 gulp.js 文件,其中包括:
gulp.task('default', ['watch']);
Which starts up the watch task
启动监视任务
gulp.task('watch', function(){
gulp.watch(productionScripts, ['autoConcat']);
});
Then on any saved changes to files in productionScripts, the watch task will concat the files.
然后,在对 productionScripts 中文件的任何保存更改时,watch 任务将连接文件。
What I would like to do, is in my package.json, I would like to spool up this watch when I type npm start (this already starts my node server).
我想做的是在我的 package.json 中,当我输入 npm start (这已经启动了我的节点服务器)时,我想将这个手表后台处理。
package.json
包.json
"start": "node server.js",
UPDATE--------
更新--------
Ben(b3nj4m.com), I tried what you stated. The watch and server start up. However, everything runs twice (probably due to the editor, not related), but I do lose my server log when I start it up with gulp.
本(b3nj4m.com),我试过你说的。手表和服务器启动。然而,一切都运行了两次(可能是由于编辑器,而不是相关的),但是当我用 gulp 启动它时,我确实丢失了我的服务器日志。
[15:31:18] Starting 'autoConcat'...
[15:31:18] Finished 'autoConcat' after 147 ms
[15:31:19] Starting 'autoConcat'...
[15:31:19] Finished 'autoConcat' after 138 ms
[15:31:20] Starting 'autoConcat'...
[15:31:20] Finished 'autoConcat' after 127 ms
[15:31:23] Starting 'autoConcat'...
It's like there is a loop between the server restarting on a change, and the concatenated file changing.
这就像服务器在更改时重新启动和连接文件更改之间存在循环。
回答by Ben
You could run your server from your gulpfile:
你可以从你的 gulpfile 运行你的服务器:
var child = require('child_process');
var fs = require('fs');
gulp.task('default', ['server', 'watch']);
gulp.task('server', function() {
var server = child.spawn('node', ['server.js']);
var log = fs.createWriteStream('server.log', {flags: 'a'});
server.stdout.pipe(log);
server.stderr.pipe(log);
});
gulp.task('watch', function(){
gulp.watch(productionScripts, ['autoConcat']);
});
Then change your npm start
definition to look like:
然后将您的npm start
定义更改为:
"scripts": {
"start": "gulp"
}
回答by nashcheez
You could concatenate multiple tasks in your start
in package.json
using the package concurrently
as such:
您可以start
在package.json
使用包中连接多个任务,concurrently
如下所示:
{
"start": "concurrent \"node server.js\" \"gulp\" "
}
And run npm start
from your terminal. This would execute all statements within start
.
并npm start
从您的终端运行。这将执行start
.
For references: https://www.npmjs.com/package/concurrently
参考资料:https: //www.npmjs.com/package/concurrently
EDIT:
编辑:
As pointed out by @Josh in the comments, the CLI name now matches the package name. Hence, you could write the script as:
正如@Josh 在评论中指出的那样,CLI 名称现在与包名称匹配。因此,您可以将脚本编写为:
{
"start": "concurrently \"node server.js\" \"gulp\" "
}
回答by Ben
I have something like this in one of my projects. Note that it will background both processes - you can use ps
to get the ID and stop it with kill <pid>
.
我的一个项目中有这样的事情。请注意,它将使两个进程都成为后台 - 您可以使用它ps
来获取 ID 并使用kill <pid>
.
"scripts": {
"start": "{ gulp watch & node server.js & }"
}
To disable logging, too:
要禁用日志记录,也:
"scripts": {
"start": "{ gulp watch --silent & node server.js & }"
}
回答by JoshuaDavid
One best practice to consider is to use nodemonand gulp-nodemonand then like the accepted answer, trigger the gulp script from npm with npm start
. It's blazing fast and you get the node server restarted on file changes. For example:
要考虑的一种最佳实践是使用nodemon和gulp -nodemon,然后像接受的答案一样,使用npm start
. 它非常快,您可以在文件更改时重新启动节点服务器。例如:
gulpfile.js
gulpfile.js
var gulp = require('gulp');
var nodemon = require('gulp-nodemon');
...
var nodemonOptions = {
script: 'bin/www.js',
ext: 'js',
env: { 'NODE_ENV': 'development' },
verbose: false,
ignore: [],
watch: ['bin/*', 'routes/*', 'app.js']
};
gulp.task('start', function () {
nodemon(nodemonOptions)
.on('restart', function () {
console.log('restarted!')
});
});
package.json
包.json
{
...
"scripts": {
"start": "gulp start"
},
"devDependencies": {
"gulp": "^3.9.0",
"gulp-nodemon": "^2.0.4"
}
}