diff --git a/src/interfaces.ts b/src/interfaces.ts index f78fb2116..3ba00fc76 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -4,6 +4,7 @@ import * as typescript from 'typescript'; import { Chalk } from 'chalk'; export interface ErrorInfo { + diagnostic: typescript.Diagnostic; code: number; severity: Severity; content: string; diff --git a/src/utils.ts b/src/utils.ts index d64c119e8..b22831094 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -80,6 +80,7 @@ export function formatErrors( ? undefined : file.getLineAndCharacterOfPosition(diagnostic.start!); const errorInfo: ErrorInfo = { + diagnostic, code: diagnostic.code, severity: compiler.DiagnosticCategory[ diagnostic.category diff --git a/test/comparison-tests/errorFormatterDiagnostic/app.ts b/test/comparison-tests/errorFormatterDiagnostic/app.ts new file mode 100644 index 000000000..07503cf12 --- /dev/null +++ b/test/comparison-tests/errorFormatterDiagnostic/app.ts @@ -0,0 +1,4 @@ +import myComponent = require('components/myComponent'); +import myComponent2 = require('components/myComponent2'); + +console.log(myComponent); \ No newline at end of file diff --git a/test/comparison-tests/errorFormatterDiagnostic/common/components/myComponent.ts b/test/comparison-tests/errorFormatterDiagnostic/common/components/myComponent.ts new file mode 100644 index 000000000..77c1505d6 --- /dev/null +++ b/test/comparison-tests/errorFormatterDiagnostic/common/components/myComponent.ts @@ -0,0 +1 @@ +export = 'myComponent'; \ No newline at end of file diff --git a/test/comparison-tests/errorFormatterDiagnostic/expectedOutput-3.6/bundle.js b/test/comparison-tests/errorFormatterDiagnostic/expectedOutput-3.6/bundle.js new file mode 100644 index 000000000..02dd408f1 --- /dev/null +++ b/test/comparison-tests/errorFormatterDiagnostic/expectedOutput-3.6/bundle.js @@ -0,0 +1,113 @@ +/******/ (function(modules) { // webpackBootstrap +/******/ // The module cache +/******/ var installedModules = {}; +/******/ +/******/ // The require function +/******/ function __webpack_require__(moduleId) { +/******/ +/******/ // Check if module is in cache +/******/ if(installedModules[moduleId]) { +/******/ return installedModules[moduleId].exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = installedModules[moduleId] = { +/******/ i: moduleId, +/******/ l: false, +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); +/******/ +/******/ // Flag the module as loaded +/******/ module.l = true; +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/******/ +/******/ // expose the modules object (__webpack_modules__) +/******/ __webpack_require__.m = modules; +/******/ +/******/ // expose the module cache +/******/ __webpack_require__.c = installedModules; +/******/ +/******/ // define getter function for harmony exports +/******/ __webpack_require__.d = function(exports, name, getter) { +/******/ if(!__webpack_require__.o(exports, name)) { +/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter }); +/******/ } +/******/ }; +/******/ +/******/ // define __esModule on exports +/******/ __webpack_require__.r = function(exports) { +/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { +/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); +/******/ } +/******/ Object.defineProperty(exports, '__esModule', { value: true }); +/******/ }; +/******/ +/******/ // create a fake namespace object +/******/ // mode & 1: value is a module id, require it +/******/ // mode & 2: merge all properties of value into the ns +/******/ // mode & 4: return value when already ns object +/******/ // mode & 8|1: behave like require +/******/ __webpack_require__.t = function(value, mode) { +/******/ if(mode & 1) value = __webpack_require__(value); +/******/ if(mode & 8) return value; +/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value; +/******/ var ns = Object.create(null); +/******/ __webpack_require__.r(ns); +/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value }); +/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key)); +/******/ return ns; +/******/ }; +/******/ +/******/ // getDefaultExport function for compatibility with non-harmony modules +/******/ __webpack_require__.n = function(module) { +/******/ var getter = module && module.__esModule ? +/******/ function getDefault() { return module['default']; } : +/******/ function getModuleExports() { return module; }; +/******/ __webpack_require__.d(getter, 'a', getter); +/******/ return getter; +/******/ }; +/******/ +/******/ // Object.prototype.hasOwnProperty.call +/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; +/******/ +/******/ // __webpack_public_path__ +/******/ __webpack_require__.p = ""; +/******/ +/******/ +/******/ // Load entry module and return exports +/******/ return __webpack_require__(__webpack_require__.s = "./app.ts"); +/******/ }) +/************************************************************************/ +/******/ ({ + +/***/ "./app.ts": +/*!****************!*\ + !*** ./app.ts ***! + \****************/ +/*! no static exports found */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; +eval("\nexports.__esModule = true;\nvar myComponent = __webpack_require__(/*! components/myComponent */ \"./common/components/myComponent.ts\");\nconsole.log(myComponent);\n\n\n//# sourceURL=webpack:///./app.ts?"); + +/***/ }), + +/***/ "./common/components/myComponent.ts": +/*!******************************************!*\ + !*** ./common/components/myComponent.ts ***! + \******************************************/ +/*! no static exports found */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; +eval("\nmodule.exports = 'myComponent';\n\n\n//# sourceURL=webpack:///./common/components/myComponent.ts?"); + +/***/ }) + +/******/ }); \ No newline at end of file diff --git a/test/comparison-tests/errorFormatterDiagnostic/expectedOutput-3.6/bundle.transpiled.js b/test/comparison-tests/errorFormatterDiagnostic/expectedOutput-3.6/bundle.transpiled.js new file mode 100644 index 000000000..4bcb44dd6 --- /dev/null +++ b/test/comparison-tests/errorFormatterDiagnostic/expectedOutput-3.6/bundle.transpiled.js @@ -0,0 +1,113 @@ +/******/ (function(modules) { // webpackBootstrap +/******/ // The module cache +/******/ var installedModules = {}; +/******/ +/******/ // The require function +/******/ function __webpack_require__(moduleId) { +/******/ +/******/ // Check if module is in cache +/******/ if(installedModules[moduleId]) { +/******/ return installedModules[moduleId].exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = installedModules[moduleId] = { +/******/ i: moduleId, +/******/ l: false, +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); +/******/ +/******/ // Flag the module as loaded +/******/ module.l = true; +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/******/ +/******/ // expose the modules object (__webpack_modules__) +/******/ __webpack_require__.m = modules; +/******/ +/******/ // expose the module cache +/******/ __webpack_require__.c = installedModules; +/******/ +/******/ // define getter function for harmony exports +/******/ __webpack_require__.d = function(exports, name, getter) { +/******/ if(!__webpack_require__.o(exports, name)) { +/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter }); +/******/ } +/******/ }; +/******/ +/******/ // define __esModule on exports +/******/ __webpack_require__.r = function(exports) { +/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { +/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); +/******/ } +/******/ Object.defineProperty(exports, '__esModule', { value: true }); +/******/ }; +/******/ +/******/ // create a fake namespace object +/******/ // mode & 1: value is a module id, require it +/******/ // mode & 2: merge all properties of value into the ns +/******/ // mode & 4: return value when already ns object +/******/ // mode & 8|1: behave like require +/******/ __webpack_require__.t = function(value, mode) { +/******/ if(mode & 1) value = __webpack_require__(value); +/******/ if(mode & 8) return value; +/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value; +/******/ var ns = Object.create(null); +/******/ __webpack_require__.r(ns); +/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value }); +/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key)); +/******/ return ns; +/******/ }; +/******/ +/******/ // getDefaultExport function for compatibility with non-harmony modules +/******/ __webpack_require__.n = function(module) { +/******/ var getter = module && module.__esModule ? +/******/ function getDefault() { return module['default']; } : +/******/ function getModuleExports() { return module; }; +/******/ __webpack_require__.d(getter, 'a', getter); +/******/ return getter; +/******/ }; +/******/ +/******/ // Object.prototype.hasOwnProperty.call +/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; +/******/ +/******/ // __webpack_public_path__ +/******/ __webpack_require__.p = ""; +/******/ +/******/ +/******/ // Load entry module and return exports +/******/ return __webpack_require__(__webpack_require__.s = "./app.ts"); +/******/ }) +/************************************************************************/ +/******/ ({ + +/***/ "./app.ts": +/*!****************!*\ + !*** ./app.ts ***! + \****************/ +/*! no static exports found */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; +eval("\nObject.defineProperty(exports, \"__esModule\", { value: true });\nvar myComponent = __webpack_require__(/*! components/myComponent */ \"./common/components/myComponent.ts\");\nconsole.log(myComponent);\n\n\n//# sourceURL=webpack:///./app.ts?"); + +/***/ }), + +/***/ "./common/components/myComponent.ts": +/*!******************************************!*\ + !*** ./common/components/myComponent.ts ***! + \******************************************/ +/*! no static exports found */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; +eval("\nmodule.exports = 'myComponent';\n\n\n//# sourceURL=webpack:///./common/components/myComponent.ts?"); + +/***/ }) + +/******/ }); \ No newline at end of file diff --git a/test/comparison-tests/errorFormatterDiagnostic/expectedOutput-3.6/output.transpiled.txt b/test/comparison-tests/errorFormatterDiagnostic/expectedOutput-3.6/output.transpiled.txt new file mode 100644 index 000000000..52173a13f --- /dev/null +++ b/test/comparison-tests/errorFormatterDiagnostic/expectedOutput-3.6/output.transpiled.txt @@ -0,0 +1,5 @@ + Asset Size Chunks Chunk Names +bundle.js 4.37 KiB main [emitted] main +Entrypoint main = bundle.js +[./app.ts] 156 bytes {main} [built] +[./common/components/myComponent.ts] 46 bytes {main} [built] \ No newline at end of file diff --git a/test/comparison-tests/errorFormatterDiagnostic/expectedOutput-3.6/output.txt b/test/comparison-tests/errorFormatterDiagnostic/expectedOutput-3.6/output.txt new file mode 100644 index 000000000..d2d3e58cd --- /dev/null +++ b/test/comparison-tests/errorFormatterDiagnostic/expectedOutput-3.6/output.txt @@ -0,0 +1,12 @@ + Asset Size Chunks Chunk Names +bundle.js 4.33 KiB main [emitted] main +Entrypoint main = bundle.js +[./app.ts] 120 bytes {main} [built] [1 error] +[./common/components/myComponent.ts] 46 bytes {main} [built] + +ERROR in app.ts +./app.ts +app.ts:2:31 - error TS2307: Cannot find module 'components/myComponent2'. + +2 import myComponent2 = require('components/myComponent2'); +   ~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/test/comparison-tests/errorFormatterDiagnostic/tsconfig.json b/test/comparison-tests/errorFormatterDiagnostic/tsconfig.json new file mode 100644 index 000000000..94d996e96 --- /dev/null +++ b/test/comparison-tests/errorFormatterDiagnostic/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + + } +} \ No newline at end of file diff --git a/test/comparison-tests/errorFormatterDiagnostic/webpack.config.js b/test/comparison-tests/errorFormatterDiagnostic/webpack.config.js new file mode 100644 index 000000000..950764135 --- /dev/null +++ b/test/comparison-tests/errorFormatterDiagnostic/webpack.config.js @@ -0,0 +1,32 @@ +var os = require('os'); +var path = require('path'); +var ts = require('typescript'); + +module.exports = { + mode: 'development', + entry: './app.ts', + output: { + filename: 'bundle.js' + }, + resolve: { + alias: { + components: path.resolve(__dirname, 'common/components') + }, + extensions: ['.ts', '.js'] + }, + module: { + rules: [ + { + test: /\.ts$/, loader: 'ts-loader', options: { + errorFormatter: function customErrorFormatter(error, colors) { + return ts.formatDiagnosticsWithColorAndContext([error.diagnostic], { + getCurrentDirectory: () => __dirname, + getCanonicalFileName: (fileName) => path.normalize(fileName), + getNewLine: () => os.EOL, + }); + } + } + } + ] + } +}