javascript - 如何在 Visual Studio 的 Node.js 项目中将错误设置为警告
问题描述
我想打开警告作为整个解决方案的错误策略。打开 C# 项目很容易,但我在 NodeJS(JavaScript 和 TypeScript)上苦苦挣扎。
我在 Visual Studio 解决方案中获得了 Web 项目。
我的目标如下:
- 将来自 JavaScript、Typescript 的所有警告视为 Visual Studio 中的错误 - 在构建输出和错误窗口中也是如此。
- 在 Visual Studio 和 MS Build 或任何其他 CI 中具有相同的输出。
使用当前的解决方案,我只能部分实现
- 错误警告仅适用于 JavaScript,不适用于 TypeScript。
- Visual Studio 识别的错误格式仅适用于 TypeScript,不适用于 JavaScript。
- 观看模式显示来自 Javascript 而不是来自打字稿的警告。
包.json:
{
....
"scripts": {
"start:localhost": "cross-env REACT_APP_API_URL=http://localhost:5000/restapi REACT_APP_SIGNALR_URL=http://localhost:5000/hub REACT_APP_USE_MOCKS=true react-app-rewired start",
"build": "react-app-rewired build",
"lint": "eslint .",
"analyze": "source-map-explorer build/static/js/main.*",
...
},
}
tsconfig.js:
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./tscompiled",
"moduleResolution": "node",
"allowJs": true,
"jsx": "react",
"allowSyntheticDefaultImports": true,
"target": "es6",
"lib": [ "es7", "dom" ]
},
"include": [
"./src/**/*.ts",
"./src/**/*.vue",
"./src/**/*.tsx",
"./typings/**/*"
],
"exclude": [
"./node_modules"
]
}
配置覆盖.js
/* eslint-disable */
const path = require('path');
const fs = require('fs');
const rewireBabelLoader = require('react-app-rewire-babel-loader');
const rewireTypescript = require('react-app-rewire-typescript');
var ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const WarningsToErrorsPlugin = require('warnings-to-errors-webpack-plugin');
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
function visualStudioErrorFormatter(error) {
//https://blogs.msdn.microsoft.com/msbuild/2006/11/02/msbuild-visual-studio-aware-error-messages-and-message-formats/
//http://www.gnu.org/prep/standards/standards.html#Errors
return (
//Object.keys(error) -> type, code, severity, content, file, line, character
error.file + '(' + error.line + ',' + error.character + ') : ' + error.severity + ' ' + error.code + ':' + error.content
);
}
module.exports = function override(config, env) {
// config = rewireReactHotLoader(config, env);
config = rewireTypescript(config, env);
config = rewireBabelLoader.include(
config,
resolveApp('node_modules/@MyProject/mykit-react/'),
);
var typescriptChecker = new ForkTsCheckerWebpackPlugin();
var warningsToErrorsPlugin = new WarningsToErrorsPlugin();
typescriptChecker.workersNumber = 2;
typescriptChecker.errorFormatter = visualStudioErrorFormatter;
typescriptChecker.formatter = visualStudioErrorFormatter;
config.plugins.push(warningsToErrorsPlugin);
config.plugins.push(typescriptChecker);
config.context = __dirname;
config.entry = './src/index.tsx';
config.module = {
...config.module,
rules: [
...config.module.rules,
{
test: /\.tsx?$/,
loader: 'ts-loader',
options: {
// disable type checker - we will use it in fork plugin
transpileOnly: true,
}
}
],
};
return config;
};
解决方案
推荐阅读
- linq - 事件 ID:1026 System.Linq.Enumerable.Single 处的 System.InvalidOperationException
- javascript - ASP.NET MVC CORE 无法通过 Jquery .ajax() 模型绑定 TypeScript Json 数据
- javascript - javascript:在数组中分配数组索引的问题,if语句在循环中
- promise - Promise Callback :: fn(arr) 做了什么,promise 的最后一行做了什么?其背后的逻辑是什么?
- wordpress - 在简码中使用简码
- java - org/apache/poi/xssf/usermodel/XSSFWorkbook 的 NoClassDefFoundError
- javascript - 这个递归函数是二次时间复杂度吗?
- asp.net - 无法解析模块说明符“cesium”?
- c++ - 如何为不同项目中的 .cpp 文件创建单元测试
- python-3.x - 使用 Ramajunam 方程估计 1/pi 的值,与 (1/math.pi) 比较时返回错误值