Skip to content

Commit ea8dfd7

Browse files
authored
Merge pull request #701 from freeman/reportFiles
Report diagnostics only on certain files [WIP]
2 parents 6d55769 + 92a73e2 commit ea8dfd7

File tree

18 files changed

+662
-19
lines changed

18 files changed

+662
-19
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,21 @@ messages are emitted via webpack which is not affected by this flag.
245245
You can squelch certain TypeScript errors by specifying an array of diagnostic
246246
codes to ignore.
247247
248+
#### reportFiles *(string[]) (default=[])*
249+
250+
Only report errors on files matching these glob patterns.
251+
252+
```javascript
253+
// in webpack.config.js
254+
{
255+
test: /\.ts$/,
256+
loader: 'ts-loader',
257+
options: { reportFiles: ['src/**/*.{ts,tsx}', '!src/skip.ts'] }
258+
}
259+
```
260+
261+
This can be useful when certain types definitions have errors that are not fatal to your application.
262+
248263
#### compiler *(string) (default='typescript')*
249264
250265
Allows use of TypeScript compilers other than the official one. Should be

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@
3939
"chalk": "^2.3.0",
4040
"enhanced-resolve": "^3.0.0",
4141
"loader-utils": "^1.0.2",
42+
"micromatch": "^3.1.4",
4243
"semver": "^5.0.1"
4344
},
4445
"devDependencies": {
46+
"@types/micromatch": "^3.1.0",
4547
"@types/semver": "^5.4.0",
4648
"babel": "^6.0.0",
4749
"babel-core": "^6.0.0",

src/after-compile.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ function provideCompilerOptionDiagnosticErrorsToWebpack(
6868
program.getOptionsDiagnostics() :
6969
languageService!.getCompilerOptionsDiagnostics(),
7070
loaderOptions, instance.colors, compiler,
71-
{ file: configFilePath || 'tsconfig.json' }));
71+
{ file: configFilePath || 'tsconfig.json' },
72+
compilation.compiler.context));
7273
}
7374
}
7475

@@ -162,13 +163,17 @@ function provideErrorsToWebpack(
162163
removeTSLoaderErrors(module.errors);
163164

164165
// append errors
165-
const formattedErrors = formatErrors(errors, loaderOptions, instance.colors, compiler, { module });
166+
const formattedErrors = formatErrors(errors, loaderOptions,
167+
instance.colors, compiler, { module },
168+
compilation.compiler.context);
166169
registerWebpackErrors(module.errors, formattedErrors);
167170
registerWebpackErrors(compilation.errors, formattedErrors);
168171
});
169172
} else {
170173
// otherwise it's a more generic error
171-
registerWebpackErrors(compilation.errors, formatErrors(errors, loaderOptions, instance.colors, compiler, { file: filePath }));
174+
registerWebpackErrors(compilation.errors, formatErrors(errors,
175+
loaderOptions, instance.colors, compiler, { file: filePath },
176+
compilation.compiler.context));
172177
}
173178
});
174179
}

src/config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export function getConfigFile(
4040
);
4141

4242
if (configFile.error !== undefined) {
43-
configFileError = formatErrors([configFile.error], loaderOptions, colors, compiler, { file: configFilePath })[0];
43+
configFileError = formatErrors([configFile.error], loaderOptions,
44+
colors, compiler, { file: configFilePath }, loader.context)[0];
4445
}
4546
} else {
4647
if (compilerCompatible) { log.logInfo(compilerDetailsLogMessage); }

src/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function getLoaderOptions(loader: Webpack) {
111111
}
112112

113113
type ValidLoaderOptions = keyof LoaderOptions;
114-
const validLoaderOptions: ValidLoaderOptions[] = ['silent', 'logLevel', 'logInfoToStdOut', 'instance', 'compiler', 'contextAsConfigBasePath', 'configFile', 'transpileOnly', 'ignoreDiagnostics', 'errorFormatter', 'colors', 'compilerOptions', 'appendTsSuffixTo', 'appendTsxSuffixTo', 'entryFileCannotBeJs' /* DEPRECATED */, 'onlyCompileBundledFiles', 'happyPackMode', 'getCustomTransformers', 'experimentalWatchApi'];
114+
const validLoaderOptions: ValidLoaderOptions[] = ['silent', 'logLevel', 'logInfoToStdOut', 'instance', 'compiler', 'contextAsConfigBasePath', 'configFile', 'transpileOnly', 'ignoreDiagnostics', 'errorFormatter', 'colors', 'compilerOptions', 'appendTsSuffixTo', 'appendTsxSuffixTo', 'entryFileCannotBeJs' /* DEPRECATED */, 'onlyCompileBundledFiles', 'happyPackMode', 'getCustomTransformers', 'reportFiles', 'experimentalWatchApi'];
115115

116116
/**
117117
* Validate the supplied loader options.
@@ -149,6 +149,7 @@ function makeLoaderOptions(instanceName: string, configFileOptions: Partial<Load
149149
happyPackMode: false,
150150
colors: true,
151151
onlyCompileBundledFiles: false,
152+
reportFiles: [],
152153
// When the watch API usage stabilises look to remove this option and make watch usage the default behaviour when available
153154
experimentalWatchApi: false
154155
} as Partial<LoaderOptions>, configFileOptions, loaderOptions);
@@ -273,7 +274,9 @@ function getTranspilationEmit(
273274
if (!instance.loaderOptions.happyPackMode) {
274275
registerWebpackErrors(
275276
loader._module.errors,
276-
formatErrors(diagnostics, instance.loaderOptions, instance.colors, instance.compiler, { module: loader._module })
277+
formatErrors(diagnostics, instance.loaderOptions, instance.colors,
278+
instance.compiler, { module: loader._module },
279+
loader.context)
277280
);
278281
}
279282

src/instances.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ function successfulTypeScriptInstance(
8787
const configParseResult = getConfigParseResult(compiler, configFile, basePath);
8888

8989
if (configParseResult.errors.length > 0 && !loaderOptions.happyPackMode) {
90-
const errors = formatErrors(configParseResult.errors, loaderOptions, colors, compiler, { file: configFilePath });
90+
const errors = formatErrors(configParseResult.errors, loaderOptions, colors,
91+
compiler, { file: configFilePath }, loader.context);
9192

9293
registerWebpackErrors(loader._module.errors, errors);
9394

@@ -110,7 +111,8 @@ function successfulTypeScriptInstance(
110111
const diagnostics = program.getOptionsDiagnostics();
111112
registerWebpackErrors(
112113
loader._module.errors,
113-
formatErrors(diagnostics, loaderOptions, colors, compiler!, {file: configFilePath || 'tsconfig.json'}));
114+
formatErrors(diagnostics, loaderOptions, colors, compiler!,
115+
{file: configFilePath || 'tsconfig.json'}, loader.context));
114116
}
115117

116118
const instance: TSInstance = {

src/interfaces.ts

+1
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ export interface LoaderOptions {
282282
contextAsConfigBasePath: boolean;
283283
transpileOnly: boolean;
284284
ignoreDiagnostics: number[];
285+
reportFiles: string[];
285286
errorFormatter: (message: ErrorInfo, colors: Chalk) => string;
286287
onlyCompileBundledFiles: boolean;
287288
colors: boolean;

src/utils.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as typescript from 'typescript';
22
import * as path from 'path';
33
import * as fs from 'fs';
44
import { Chalk } from 'chalk';
5+
import * as micromatch from 'micromatch';
56

67
import constants = require('./constants');
78
import {
@@ -49,12 +50,25 @@ export function formatErrors(
4950
loaderOptions: LoaderOptions,
5051
colors: Chalk,
5152
compiler: typeof typescript,
52-
merge?: { file?: string; module?: WebpackModule }
53+
merge: { file?: string; module?: WebpackModule },
54+
context: string
5355
): WebpackError[] {
5456

5557
return diagnostics
5658
? diagnostics
57-
.filter(diagnostic => loaderOptions.ignoreDiagnostics.indexOf(diagnostic.code) === -1)
59+
.filter(diagnostic => {
60+
if (loaderOptions.ignoreDiagnostics.indexOf(diagnostic.code) !== -1) {
61+
return false;
62+
}
63+
if (loaderOptions.reportFiles.length > 0 && diagnostic.file) {
64+
const relativeFileName = path.relative(context, diagnostic.file.fileName);
65+
const matchResult = micromatch([relativeFileName], loaderOptions.reportFiles);
66+
if (matchResult.length === 0) {
67+
return false;
68+
}
69+
}
70+
return true;
71+
})
5872
.map<WebpackError>(diagnostic => {
5973
const file = diagnostic.file;
6074
const position = file === undefined ? undefined : file.getLineAndCharacterOfPosition(diagnostic.start!);
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './skip';
2+
export let a: number
3+
a = '10'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/******/ (function(modules) { // webpackBootstrap
2+
/******/ // The module cache
3+
/******/ var installedModules = {};
4+
/******/
5+
/******/ // The require function
6+
/******/ function __webpack_require__(moduleId) {
7+
/******/
8+
/******/ // Check if module is in cache
9+
/******/ if(installedModules[moduleId]) {
10+
/******/ return installedModules[moduleId].exports;
11+
/******/ }
12+
/******/ // Create a new module (and put it into the cache)
13+
/******/ var module = installedModules[moduleId] = {
14+
/******/ i: moduleId,
15+
/******/ l: false,
16+
/******/ exports: {}
17+
/******/ };
18+
/******/
19+
/******/ // Execute the module function
20+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
21+
/******/
22+
/******/ // Flag the module as loaded
23+
/******/ module.l = true;
24+
/******/
25+
/******/ // Return the exports of the module
26+
/******/ return module.exports;
27+
/******/ }
28+
/******/
29+
/******/
30+
/******/ // expose the modules object (__webpack_modules__)
31+
/******/ __webpack_require__.m = modules;
32+
/******/
33+
/******/ // expose the module cache
34+
/******/ __webpack_require__.c = installedModules;
35+
/******/
36+
/******/ // define getter function for harmony exports
37+
/******/ __webpack_require__.d = function(exports, name, getter) {
38+
/******/ if(!__webpack_require__.o(exports, name)) {
39+
/******/ Object.defineProperty(exports, name, {
40+
/******/ configurable: false,
41+
/******/ enumerable: true,
42+
/******/ get: getter
43+
/******/ });
44+
/******/ }
45+
/******/ };
46+
/******/
47+
/******/ // getDefaultExport function for compatibility with non-harmony modules
48+
/******/ __webpack_require__.n = function(module) {
49+
/******/ var getter = module && module.__esModule ?
50+
/******/ function getDefault() { return module['default']; } :
51+
/******/ function getModuleExports() { return module; };
52+
/******/ __webpack_require__.d(getter, 'a', getter);
53+
/******/ return getter;
54+
/******/ };
55+
/******/
56+
/******/ // Object.prototype.hasOwnProperty.call
57+
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
58+
/******/
59+
/******/ // __webpack_public_path__
60+
/******/ __webpack_require__.p = "";
61+
/******/
62+
/******/ // Load entry module and return exports
63+
/******/ return __webpack_require__(__webpack_require__.s = 0);
64+
/******/ })
65+
/************************************************************************/
66+
/******/ ([
67+
/* 0 */
68+
/***/ (function(module, exports, __webpack_require__) {
69+
70+
"use strict";
71+
72+
exports.__esModule = true;
73+
__webpack_require__(1);
74+
exports.a = '10';
75+
76+
77+
/***/ }),
78+
/* 1 */
79+
/***/ (function(module, exports, __webpack_require__) {
80+
81+
"use strict";
82+
83+
exports.__esModule = true;
84+
exports.a = '10';
85+
86+
87+
/***/ })
88+
/******/ ]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Asset Size Chunks Chunk Names
2+
bundle.js 2.71 kB 0 [emitted] main
3+
[0] ./.test/reportFiles/app.ts 78 bytes {0} [built]
4+
[1] ./.test/reportFiles/skip.ts 59 bytes {0} [built]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Asset Size Chunks Chunk Names
2+
bundle.js 2.71 kB 0 [emitted] main
3+
[0] ./.test/reportFiles/app.ts 78 bytes {0} [built] [1 error]
4+
[1] ./.test/reportFiles/skip.ts 59 bytes {0} [built]
5+
6+
ERROR in ./.test/reportFiles/app.ts
7+
[tsl] ERROR in app.ts(3,1)
8+
 TS2322: Type '"10"' is not assignable to type 'number'.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export let a: number
2+
a = '10'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"compilerOptions": {
3+
}
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
entry: './app.ts',
3+
output: {
4+
filename: 'bundle.js'
5+
},
6+
resolve: {
7+
extensions: ['.ts', 'tsx', '.js']
8+
},
9+
module: {
10+
rules: [
11+
{
12+
test: /\.tsx?$/, loader: 'ts-loader', options: {
13+
reportFiles: [ '**/*.ts', '!skip.ts' ]
14+
}
15+
}
16+
]
17+
}
18+
}

test/comparison-tests/validateLoaderOptionNames/expectedOutput-2.6/bundle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
/* 0 */
6868
/***/ (function(module, exports) {
6969

70-
throw new Error("Module build failed: Error: ts-loader was supplied with an unexpected loader option: notRealOption\n\nPlease take a look at the options you are supplying; the following are valid options:\nsilent / logLevel / logInfoToStdOut / instance / compiler / configFile / transpileOnly / ignoreDiagnostics / errorFormatter / colors / compilerOptions / appendTsSuffixTo / appendTsxSuffixTo / entryFileCannotBeJs / onlyCompileBundledFiles / happyPackMode / getCustomTransformers\n\n at validateLoaderOptions (C:/source/ts-loader/dist/index.js:92:19)\n at getLoaderOptions (C:/source/ts-loader/dist/index.js:75:5)\n at Object.loader (C:/source/ts-loader/dist/index.js:23:19)");
70+
throw new Error("Module build failed: Error: ts-loader was supplied with an unexpected loader option: notRealOption\n\nPlease take a look at the options you are supplying; the following are valid options:\nsilent / logLevel / logInfoToStdOut / instance / compiler / contextAsConfigBasePath / configFile / transpileOnly / ignoreDiagnostics / errorFormatter / colors / compilerOptions / appendTsSuffixTo / appendTsxSuffixTo / entryFileCannotBeJs / onlyCompileBundledFiles / happyPackMode / getCustomTransformers / reportFiles / experimentalWatchApi\n\n at validateLoaderOptions (C:/source/ts-loader/dist/index.js:92:19)\n at getLoaderOptions (C:/source/ts-loader/dist/index.js:75:5)\n at Object.loader (C:/source/ts-loader/dist/index.js:23:19)");
7171

7272
/***/ })
7373
/******/ ]);

test/comparison-tests/validateLoaderOptionNames/expectedOutput-2.6/output.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ERROR in ./.test/validateLoaderOptionNames/app.ts
66
Module build failed: Error: ts-loader was supplied with an unexpected loader option: notRealOption
77

88
Please take a look at the options you are supplying; the following are valid options:
9-
silent / logLevel / logInfoToStdOut / instance / compiler / configFile / transpileOnly / ignoreDiagnostics / errorFormatter / colors / compilerOptions / appendTsSuffixTo / appendTsxSuffixTo / entryFileCannotBeJs / onlyCompileBundledFiles / happyPackMode / getCustomTransformers
9+
silent / logLevel / logInfoToStdOut / instance / compiler / contextAsConfigBasePath / configFile / transpileOnly / ignoreDiagnostics / errorFormatter / colors / compilerOptions / appendTsSuffixTo / appendTsxSuffixTo / entryFileCannotBeJs / onlyCompileBundledFiles / happyPackMode / getCustomTransformers / reportFiles / experimentalWatchApi
1010

1111
at validateLoaderOptions (dist\index.js:92:19)
1212
at getLoaderOptions (dist\index.js:75:5)

0 commit comments

Comments
 (0)