如何在Visual Studio的Node.js项目中将错误设置为警告

时间:2019-03-04 18:01:47

标签: javascript node.js visual-studio typescript compiler-warnings

我想在整个解决方案上将警告作为错误政策。启用C#项目很容易,但是我一直在努力使用NodeJS(JavaScript和TypeScript)。

我在Visual Studio解决方案中有一个Web项目。

我的目标是:

  • 将来自JavaScript和Typescript的所有警告视为Visual Studio中的错误-在生成输出中以及在错误窗口中。
  • 在Visual Studio和MS Build或任何其他CI中具有相同的输出。

使用当前解决方案,我只能部分实现

  • 警告错误仅适用于JavaScript,不适用于TypeScript。
  • Visual Studio识别的错误格式仅适用于TypeScript,而不适用于JavaScript。
  • 监视模式显示的是来自Javascript的警告,而不是来自打字稿的警告。

package.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"
  ]
}

config-overrides.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;
};

0 个答案:

没有答案