如何使用日志语句调试Gruntfile.js?

Gur*_*raj 7 jquery gruntjs karma-runner grunt-contrib-qunit

在我的Gruntfile中,如何在其处理中添加日志语句,如下例所示?

 karma: {
        unit: {
            configFile: "<%= grunt.option('Debug') ? 'build/karma.conf.js' : '' %>",
            console.log(configFile),
            singleRun: true,
            browsers: ['PhantomJS']
        },
    }
Run Code Online (Sandbox Code Playgroud)

Kyl*_*ung 9

Gruntfiles是javascript,因此console.log()只要它是有效的javascript 就可以使用.

grunt.initConfig({
  karma: {
    unit: {
      configFile: 'build/karma.conf.js'
    }
  }
});
if (grunt.option('debug')) {
  console.log(grunt.config('karma.unit.configFile'));
}
Run Code Online (Sandbox Code Playgroud)


Rom*_*lus 2

如果这么简单就好了... console.log() 只向客户端输出客户端内容;但是,由于您正在处理服务器端的事情,因此您不会在浏览器控制台(而不是服务器控制台,可能是您的终端)中看到任何弹出内容。

由于其他人的工作,有一种解决方法,例如: https: //github.com/ethanl/connect-browser-logger

这基本上会将那些服务器端日志提升到客户端供您查看。如果你谷歌一下,你会发现很多其他解决方案(有些能够设置断点、单步执行代码等)。

不破旧!

编辑:天啊,我刚刚意识到你想要专门你的 gruntfile 中进行日志记录。这是一个有点不同的故事,但它仍然应该对你有用!