Как настроить ошибки как предупреждение в проекте Node.js в Visual Studio - PullRequest
0 голосов
/ 04 марта 2019

Я хотел бы включить предупреждение в качестве политики ошибок для всего решения.Проект C # легко включить, но я борюсь с NodeJS (JavaScript и TypeScript).

Я получил веб-проект в решении Visual Studio.

Моя цель заключается в следующем:

  • См. Все предупреждения из 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;
};
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...