【问题标题】:Grunt - Execute and Watch application at same timeGrunt - 同时执行和监视应用程序
【发布时间】:2017-01-17 13:21:54
【问题描述】:

我正在开发一个简单的节点应用程序,我偶然发现了这个名为 grunt-cli 的工具。

在介绍 grunt 之后,我计划将它与我的应用程序一起使用。

Gruntfile.js

module.exports = function(grunt){

    grunt.initConfig({
        execute: {
            target:{
                src: ['app.js']
            }
        },
        watch: {
            scrpits: {
                files: ['app.js'],
                tasks: ['execute'],
                options: {
                    spawn: false
                }
            }
        }
    });

    grunt.loadNpmTasks('grunt-execute');
    grunt.loadNpmTasks('grunt-contrib-watch');

    grunt.registerTask('default', ['execute', 'watch']);
};

package.json

{
  "name": "exampleapp",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Priyank Thakkar",
  "license": "ISC",
  "dependencies": {
    "express": "^4.14.0",
    "grunt": "^0.4.1"
  },
  "devDependencies": {
    "grunt-contrib-watch": "^1.0.0",
    "grunt-execute": "^0.2.2"
  }
}

app.js

var express = require('express');

var app = express();

app.set('port', process.env.port || 3005);

app.listen(3005, function(){
   console.log('application started at http://localhost:' + app.get('port'));
});

运行execute然后watch是否正确?不知何故,我觉得终端只卡在执行任务上,它没有观察应用程序的变化。

【问题讨论】:

    标签: javascript node.js gruntjs grunt-contrib-watch


    【解决方案1】:

    您的 grunt 执行目标正在阻止 watch 并且永远不会结束。这两个任务需要在不同的线程中运行。

    你可以使用类似 grunt-concurrent 的东西同时执行这两个任务: https://github.com/sindresorhus/grunt-concurrent

    【讨论】:

    • 我同意你的建议。这是两个阻塞任务。所以我不得不通过 grunt-concurrent 运行它们。谢谢,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 2011-03-24
    • 1970-01-01
    • 2016-05-17
    相关资源
    最近更新 更多