简体   繁体   English

带grunt-contrib-connect的grunt-contrib-watch

[英]Grunt-contrib-watch with grunt-contrib-connect

Unfortunately, grunt-contrib-watch and grunt-contrib-connect don't seem to be playing nice. 不幸的是, grunt-contrib-watchgrunt-contrib-connect似乎玩起来并不好。

On the grunt-contrib-connect readme it says: 在grunt-contrib-connect 自述文件中说:

Note that this server only runs as long as grunt is running. 请注意,此服务器仅在grunt运行时才运行。 Once grunt's tasks have completed, the web server stops. 一旦grunt的任务完成,Web服务器就会停止。 This behavior can be changed with the keepalive option, and can be enabled ad-hoc by running the task like grunt connect::keepalive. 可以使用keepalive选项更改此行为,并且可以通过运行诸如grunt connect :: keepalive之类的任务来临时启用此行为。

Fine. 精细。 But what if I want to run my watch task in tandem with the connect server? 但是,如果我想与连接服务器一起运行监视任务,该怎么办? Like so: 像这样:

connect: {
  server: {
    options: {
      port: 8000,
      hostname: 'localhost',
      keepalive: true
    }
  }
},
watch: {
  options: {
    livereload: true
  },
  files: ['**'],
  tasks: ['connect'],
}

Here, the connect task runs when a file is changed. 在这里, connect任务在文件更改时运行。 If I set the connect's keepalive option to true, then grunt-contrib-watch stops watching because it technically hasn't finished it's task. 如果我将connect的keepalive选项设置为true,那么grunt-contrib-watch会停止监视,因为从技术上讲它尚未完成任务。 If I falsify the keepalive option, then the connect server dies after it has finished the tasks. 如果我伪造了keepalive选项,那么连接服务器将在完成任务后死亡。

Yes, I could run the commands... 是的,我可以运行命令...

$ grunt connect
$ grunt watch

...in separate shells, but is there no way of running them with one command? ...在单独的外壳中,但是无法用一个命令运行它们吗?

Livereload in grunt-contrib-watch informs for changes in files at a port here below you can see it is at 35729. grunt-contrib-watch中的 Livereload在下面的端口通知文件的更改,您可以在35729看到它。

On the other hand the livereload in grunt-contrib-connect listens for changes at the port 35729. 另一方面, grunt-contrib-connect中的livereload在端口35729上侦听更改。

So we should should configure them as - 因此,我们应该将它们配置为-

connect: {
  server: {
    options: {
      port: 8000,
      hostname: 'localhost',
      livereload: 35729
    }
  }
},
watch: {
  options: {
    livereload: 35729
  },
  files: ['**'],
  tasks: []
}

You need not provide "connect" as a task here. 您无需在此处提供“连接”作为任务。 As the work of reloading is done by livereload here. 由于重新加载的工作在这里由livereload完成。 Now to make these two work with a single command we will register them as - 现在,要使用一个命令使这两个功能一起工作,我们将它们注册为-

grunt.registerTask("server", ["connect", "watch"]);

Now the connect is run and then watch is run. 现在运行连接,然后运行监视。 Now normally registerTasks works by finishing the first task then the second task and so on. 现在正常情况下,registerTasks通过完成第一个任务然后完成第二个任务,以此类推。 But due to the behaviour of connect as stated by you - 但是由于您所说的连接行为-

Note that this server only runs as long as grunt is running 请注意,此服务器仅在grunt运行时才运行

Connect is run only once. Connect仅运行一次。 But watch will keep on running looking for changes (keeping grunt running) and thus keeping the connect server up. 但是监视将继续运行以查找更改(保持运行状态),从而保持连接服务器正常运行。

Now when you try 现在,当您尝试

grunt server

things will work like a charm. 事情会像魅力一样运作。

I use grunt-nodemon , which encapsulates watch and a nodejs launcher in a single task: 我使用grunt-nodemon ,它将watch和nodejs启动器封装在一个任务中:

nodemon: {
  dev: {
    script: 'app.js',
    options: {
      ignore: [
        'node_modules/**',
        'public/**'
      ],
      ext: 'js'
    }
  }
}

Then executed with: 然后执行:

$ grunt nodemon:dev

Now, nodemon only launches the app.js script with nodejs, so you will need a small app.js to load a static static express server: 现在,nodemon仅使用nodejs启动app.js脚本,因此您将需要一个小的app.js来加载静态静态快递服务器:

var express = require('express');
var server = express(); // better instead
server.configure(function(){
  server.use(express.static(__dirname + '/public'));
});

server.listen(3000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM