简体   繁体   English

JSHint Gulp到错误窗口VS2015

[英]JSHint Gulp to error window VS2015

How can I get the output from JSHint to show up in my Error List in Visual Studio 2015 when using Gulp, rather than just output to the Task Runner? 如何在使用Gulp时将JSHint的输出显示在Visual Studio 2015的错误列表中,而不是仅输出到Task Runner?

I've tried this package but that doesn't seem to do anything except format the Gulp output slightly differently. 我已经尝试过这个软件包,但除了格式化Gulp输出略有不同之外,似乎没有做任何事情。

This is my gulpfile.js : 这是我的gulpfile.js

gulp.task('default', function () {
    gulp.src(["Scripts/**/*.js"])
        .pipe(jshint(".jshintrc"))
        .pipe(jshint.reporter("jshint-visual-studio"));
});

Actual output (in Task Runner Window): 实际输出(在任务运行窗口中):

实际输出

Preferred output (in Error List): 首选输出(在错误列表中):

首选输出

Please Note: I'm using Visual Studio 2015, so Web Essentials is no longer an option for JSHint as the functionality has been removed. 请注意:我正在使用Visual Studio 2015,因此Web Essentials不再是JSHint的选项,因为功能已被删除。

I managed to get an official word on this from Mads Kristensen in this conversation on Twitter: 我在Twitter上的这次谈话中成功地从Mads Kristensen那里得到了一个官方消息:

鸣叫

So in VS2015RC (and RTM too), there is no way to get messages to go to the output window. 所以在VS2015RC(以及RTM)中,没有办法让消息进入输出窗口。 The task runner only outputs to the error window if the Gulp task returns a failure - but honestly, I haven't managed to get this working either. 如果Gulp任务返回失败,任务运行器只输出到错误窗口 - 但老实说,我还没有设法让它工作。

He did also confirm that this kind of functionality will be coming post-RTM. 他也确认这种功能将在RTM之后发布。

Thanks to Eonasdan for pointing this out! 感谢Eonasdan指出这一点!

Source code of your package (jshint-visual-studio) just add errors to errors array and log it to console . 包的 源代码 (jshint-visual-studio)只是将错误添加到errors数组并将其记录到console The code can't do anything to change console output, you must do it by yourself. 代码无法更改console输出,您必须自己完成。

You can use this plugin . 你可以使用这个插件

Once the plugin is installed, open the Task Runner Explorer from the View –> Other Windows –> Task Runner Explorer menu. 一旦安装了该插件,从视图中打开任务运行资源管理器 - > 其他窗口 - > 任务运行资源管理器菜单。

This window will show you all the tasks in the gulp file and allow you to bind those tasks to certain Visual Studio events. 此窗口将显示gulp文件中的所有任务,并允许您将这些任务绑定到某些Visual Studio事件。 This way, we don't need to remember to run the gulp tasks from the command line. 这样,我们不需要记住从命令行运行gulp任务。 The IDE can handle it for us. IDE可以为我们处理它。

What I like to do is bind the watch task to the Solution Load event and bind the scripts task to the Before Build event. 我喜欢做的是将监视任务绑定到Solution Load事件,并将脚本任务绑定到Before Build事件。

在此输入图像描述

With these bindings, we can make sure that all.min.js is correctly generated when we build the application and also regenerated anytime I make changes to the js files. 通过这些绑定,我们可以确保在构建应用程序时正确生成all.min.js,并且在我对js文件进行更改时也可以重新生成。

The Task Runner Explorer also shows the output of any running tasks. Task Runner Explorer还显示任何正在运行的任务的输出。 Here you can see the output from the watch task, which is always running in the background in this configuration. 在这里,您可以看到监视任务的输出,该任务始终在此配置的后台运行。

在此输入图像描述

From this article. 从这篇文章。

Found a simple way to do this in VS2013, don't know if it works in 2015. Inside your jshint task in gulp just catch the error using jshints error event handler and log a message prefixed "error:" to the console. 在VS2013中找到了一个简单的方法,不知道它是否在2015年工作。在gulp中的jshint任务中使用jshints错误事件处理程序捕获错误并将一个前缀为“error:”的消息记录到控制台。 Visual Studio uses this convention to identify the errors that failed the build from the output. Visual Studio使用此约定来标识从输出中构建失败的错误。

.pipe(jshint({

// .... //

.on('error', function (e) {
    console.log("error: JSHint failed.");
})

You'll see this in your ErrorList: 你会在你的ErrorList中看到这个:

在此输入图像描述

You could use gulp-jshint with following task definition: 您可以将gulp-jshint与以下任务定义一起使用:

gulp.task('lint', function () {
    gulp.src(["Scripts/**/*.js"])
      .pipe(jshint())
      .pipe(jshint.reporter('default'))
      .pipe(jshint.reporter('fail'))

It will make gulp exit with code 1. And for code 1 to fail build you need to go to Project settings, Build Events tab and add following code to Pre-build event command line: 它将使代码1退出gulp。对于代码1构建失败,您需要转到项目设置,构建事件选项卡并将以下代码添加到预构建事件命令行:

gulp -b $(ProjectDir) --gulpfile $(ProjectDir)gulpfile.js lint

It will fail the build with following message: 它将使构建失败并显示以下消息:

Error   242 The command "ATTRIB -R gulp -b --gulpfile lint" exited with code 1. 

Here you have my jshint vs17 reporter (should work with prev. version too) : img 在这里你有我的jshint vs17记者(也应该与普通版本一起使用): img

  1. Include the gulp run command to your msbuild process (.csproj) file 将gulp run命令包含在msbuild进程(.csproj)文件中

     <Target Name="BeforeBuild"> <Exec Condition=" '$(IsTfsServerBuild)' == 'true' " Command="npm install" /> <Exec Condition=" '$(Configuration)' != 'Release' " Command="gulp --gulpfile &quot;$(ProjectDir)gulpfile.js&quot; --targetDir &quot;$([System.String]::Copy('$(TargetDir)').TrimEnd('\\\\'))&quot; --projectDir &quot;$([System.String]::Copy('$(ProjectDir)').TrimEnd('\\\\'))&quot; --configuration &quot;$(Configuration)&quot; --isTfs $(IsTfsServerBuild)" /> 

  2. Create module file gulp.jshint.vs.reporter.js 创建模块文件gulp.jshint.vs.reporter.js

 module.exports = jshintVSReporter; function logVsError(file, line, col, message, code) { console.error(formatVsOutput("error", file, line, col, message, code)); } function logVsWarning(file, line, col, message, code) { console.warn(formatVsOutput("warning", file, line, col, message, code)); } /** * Formats the proper visual strudio output messange * @param {string} type - the messange type * @param {string} file - the file path * @param {number} line - the line no in file * @param {number} col - the cloumn no in line * @param {string} message - the messange * @param {string} code - the error code * @returns {string} - vs-output-formated string */ function formatVsOutput(type, file, line, col, message, code, program) { var vsLine = (program ? program + ":" : "") + file; if (line) { vsLine += "(" + line; if (col) { vsLine += "," + col; } vsLine += ")"; } vsLine += ": " + type;//(type||"warning"); if (code) vsLine += " : " + code; if (message) { vsLine += ": " + message; } return vsLine; } /** * @typedef {Object} JSHintError * @property {string} id - usually '(error)' * @property {string} code - error/warning code ('WXXX' - warnings, 'EXXX' - errors, 'IXXX' - informations) * @property {string} reason - error/warning message * @property {string} evidence - a piece of code that generated this error * @property {number} line - the line in file number * @property {number} character - the erro cloumn in line numer * @property {string} scope - message scope, usually '(main)' unless the code was eval'ed * * @typedef {Object} JSHint * @property {string} file - file name * @property {JSHintError} error - the error description * * The custom visual studio jhint reporter * @param {Array<JSHint>} errors - the jshint error list */ function jshintVSReporter(errors) { if (errors) { var file, i, error, isError, msg; for (i = 0; i < errors.length; i++) { file = errors[i].file; error = errors[i].error; isError = error.code && error.code[0] === "E"; msg = error.reason + " (jshint:" + error.code + ")"; if (isError) { logVsError(file, error.line, error.character, msg, error.code, "JSHint"); } else { logVsWarning(file, error.line, error.character, msg, error.code, "JSHint"); } } } } 

  1. Import your local module const vsJsHintReporter = require('./gulp.jshint.vs.reporter'); 导入本地模块const vsJsHintReporter = require('./gulp.jshint.vs.reporter');
  2. Use in gulp task .pipe(jshint(config.jshint)).pipe(jshint.reporter(vsJsHintReporter)) 用于.pipe(jshint(config.jshint)).pipe(jshint.reporter(vsJsHintReporter))任务.pipe(jshint(config.jshint)).pipe(jshint.reporter(vsJsHintReporter))

I'm planning to publish the reporter to npm. 我打算把记者发布到npm。

I have build fail (and error reporting) with gulp working (well enough for me, maybe sufficient for others.) 我已经使用gulp工作构建失败(和错误报告)(对我来说足够好,对其他人来说足够了。)

Here is what I used/did... 这是我用过/做过的......

As per this page, I added/edited this in my project .json, which hooks into the prebuild event... 根据这个页面,我在我的项目 .json中添加/编辑了这个,它挂钩到prebuild事件......

  "scripts": {
    "prebuild": [ "gulp default" ]
  }

As per this page, I included the following for my jshint task... 根据这个页面,我为jshint任务包括以下内容......

// =============================
// jsHint - error detection
// =============================
gulp.task("jshint", function () {
    var jshGlobals = [
        '$',
        'jQuery',
        'window',
        'document',
        'Element',
        'Node',
        'console'
    ];

    gulp.src([paths.jsFiles, norefs])
        .pipe(jshint({
            predef: jshGlobals,
            undef: true,
            eqnull: true
        }))
        .pipe(jshint.reporter('jshint-stylish'))
        .pipe(jshint.reporter('fail'))
});

The latter two lines being the most significant. 后两行是最重要的。 You will need to npm install jshint-stylish if you don't already have it. 你需要npm安装jshint-stylish如果你还没有它。

Alternatively, for jshint-stylish , you can let VS handle it for you. 或者,对于jshint-stylish ,你可以让VS为你处理它。 Add the line for jshint-stylish as indicated below to your package .json... 将如下所示的jshint-stylish线添加到你的包中 .json ...

{
  "name": "ASP.NET",
  "version": "0.0.0",
  "devDependencies": {
    "es6-promise": "~3.1.2",
    "gulp": "^3.8.11",
    "del": "^2.2.0",
    "jshint": "~2.9.1",
    "jshint-stylish": "~2.1.0",
    "gulp-jshint": "~2.0.0",
    "gulp-flatten": "~0.2.0",
    "gulp-rename": "~1.2.2",
    "gulp-cssmin": "0.1.7",
    "gulp-uglify": "1.2.0",
    "gulp-postcss": "~6.1.0",
    "autoprefixer": "~6.3.3"
  }
}

Which gives me this when there is an error (in addition to the failed build) which is sufficient for me to dig further if/as necessary... 当出现错误(除了失败的构建之外)时,这给了我这个,这足以让我在必要时进一步挖掘...... 构建后的vs错误列表窗口

As opposed to the more detailed error info I get when running the same task via command line or Task Runner... 与通过命令行或任务运行器运行相同任务时获得的更详细的错误信息相反... 通过cmd.exe输出gulp默认任务

I imagine this solution can be improved but figured I would share as I hadn't seen a whole lot about it elsewhere. 我想这个解决方案可以改进,但我想分享,因为我在其他地方没有看到很多关于它的内容。

Cheers. 干杯。

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

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