【问题标题】:Gruntjs with grunt-nodemon, watch and jshint带有 grunt-nodemon、watch 和 jshint 的 Gruntjs
【发布时间】:2014-09-24 06:50:56
【问题描述】:

我正在尝试使用这 3 个插件运行 GruntJS,以便它可以监视更改,首先:检查文件,然后重新加载快速服务器。 我对下面配置的问题是,如果 jshint 对文件进行 lint,nodemon 不会运行,反之亦然。

// Gruntfile.js

// our wrapper function (required by grunt and its plugins)
// all configuration goes inside this function
module.exports = function(grunt) {

  // ===========================================================================
  // CONFIGURE GRUNT ===========================================================
  // ===========================================================================
  grunt.initConfig({

    // get the configuration info from package.json ----------------------------
    // this way we can use things like name and version (pkg.name)
    pkg: grunt.file.readJSON('package.json'),

    // all of our configuration will go here

    // configure jshint to validate js files -----------------------------------
  jshint: {
      options: {
        reporter: require('jshint-stylish') // use jshint-stylish to make our errors look and read good
      },

    // when this task is run, lint the Gruntfile and all js files in src
      build: ['Grunfile.js', 'routes/*.js']
    },

    watch: {

      // for scripts, run jshint and uglify
      scripts: {
        files: 'routes/*.js',
        tasks: ['jshint']
      }
    },

    concurrent: {
      dev: {

        tasks: ['jshint', 'nodemon', 'watch'],
        options: {
          logConcurrentOutput: true
        }
      }
    }, // concurrent

    nodemon: {
      dev: {
        script: './server.js'
      }
    } // nodemon


  });

  // ===========================================================================
  // LOAD GRUNT PLUGINS ========================================================
  // ===========================================================================
  // we can only load these if they are in our package.json
  // make sure you have run npm install so our app can find these
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-nodemon');


      grunt.registerTask('default', '', function() {
    var taskList = [
        'jshint',
        'nodemon',
        'watch'
    ];
    grunt.task.run(taskList);
});

};

编辑(澄清):

我第一次运行 grunt 时,jshint 对文件进行 lint,然后 nodemon 启动并且 jshint 不再 lint。

输出:

grunt
Running "default" task

Running "jshint:build" (jshint) task

✔︎ No problems


Running "nodemon:dev" (nodemon) task
[nodemon] v1.2.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node ./server.js`
Express server listening on port 3000

【问题讨论】:

  • 你是如何运行 gruntjs 的?
  • 只是在控制台里咕哝。
  • 我记得 Grunt 也有类似的问题,但我从来没有解决过。您可以查看gulp,它可以更好地处理并发性(默认情况下一切都并行运行)。
  • 很遗憾听到这个消息,您总是可以在一个终端中运行 nodemon,而在另一个终端中运行,但如果我们可以使用一个工具/命令运行所有好东西,那就太酷了。

标签: node.js gruntjs nodemon


【解决方案1】:

尝试将其作为函数任务运行:

grunt.registerTask('default', '', function() {
    var taskList = [
        'jshint',
        'nodemon',
        'watch'
    ];
    grunt.task.run(taskList);
});

编辑:我用来实现自动重新运行任务的目标的另一种方法,包括使用grunt-express-server,应用于您的设置:

module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON("package.json"),
        watch: {
            express: {
                files: ['routes/*.js'],
                tasks: ['jshint', 'express:dev'],
                options: {
                    spawn: false
                }
            }
        },
        express: {
            dev: {
                options: {
                    script: 'app.js',
                }
            }
        },
        jshint: {
            options: {
                node: true
            },
            all: {
                src: ['routes/*.js']
            }
        }
    });

    grunt.loadNpmTasks('grunt-express-server');
    grunt.loadNpmTasks('grunt-contrib-jshint');

    grunt.registerTask('default', '', function() {
        var taskList = [
            'jshint',
            'express:dev',
            'watch'
        ];
        grunt.task.run(taskList);
    });
};

【讨论】:

  • 同样的结果,nodemon 启动服务器但是 jshint 没有 lint :(
  • 从您的编辑看来,它运行正常,不是吗?或者保存正在观看的文件时它没有重新启动并运行jshint?
  • 第一次运行“grunt”时,它会检查文件,然后启动 nodemon。 nodemon 启动服务器后,如果我修改一个 .js 文件并保存它,nodemon 会重新加载服务器,但 jshint 不再 lint。
  • 我已经解决了你的问题,虽然没有使用 nodemon,而是使用 grunt-express-server - 我可以更新我的答案以表明如果你没有结婚使用 nodemon
【解决方案2】:

真是个愚蠢的错误。 我没有加载 grunt-concurrent,只是安装了 grunt-concurrent 并将其添加到 Kelz 的函数中,现在它可以工作了:)。 谢谢大家。

最终代码:

// Gruntfile.js

// our wrapper function (required by grunt and its plugins)
// all configuration goes inside this function
module.exports = function(grunt) {
  // ===========================================================================
  // CONFIGURE GRUNT ===========================================================
  // ===========================================================================
  grunt.initConfig({

    // get the configuration info from package.json ----------------------------
    // this way we can use things like name and version (pkg.name)
    pkg: grunt.file.readJSON('package.json'),

    // all of our configuration will go here

    // configure jshint to validate js files -----------------------------------
    jshint: {
      options: {
        reporter: require('jshint-stylish') // use jshint-stylish to make our errors look and read good
      },

      // when this task is run, lint the Gruntfile and all js files in src
      build: ['Grunfile.js', 'routes/*.js']
    },

    watch: {
      // for scripts, run jshint and uglify
      scripts: {
        files: 'routes/*.js',
        tasks: ['jshint']
      }
    }, // watch

    nodemon: {
      dev: {
        script: './server.js'
      }
    }, // nodemon

    concurrent: {
      dev: {
        tasks: ['jshint', 'nodemon', 'watch'],
        options: {
          logConcurrentOutput: true
        }
      }
    } // concurrent
  });

  // ===========================================================================
  // LOAD GRUNT PLUGINS ========================================================
  // ===========================================================================
  // we can only load these if they are in our package.json
  // make sure you have run npm install so our app can find these
  grunt.loadNpmTasks('grunt-concurrent');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-nodemon');

  grunt.registerTask('default', '', function() {
    var taskList = [
        'concurrent',
        'jshint',
        'nodemon',
        'watch'
    ];
    grunt.task.run(taskList);
  });
};

【讨论】:

    猜你喜欢
    • 2016-02-07
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    相关资源
    最近更新 更多