Skip to content

Commit 6af81a1

Browse files
committed
Make invalid files in tsconfig.json super obvious when ts-loader errors
1 parent 2c197df commit 6af81a1

File tree

9 files changed

+115
-15
lines changed

9 files changed

+115
-15
lines changed

index.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,24 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): {
297297
}
298298

299299
// Load initial files (core lib files, any files specified in tsconfig.json)
300-
filesToLoad.forEach(filePath => {
301-
filePath = path.normalize(filePath);
302-
files[filePath] = {
303-
text: fs.readFileSync(filePath, 'utf-8'),
304-
version: 0
305-
}
306-
});
300+
let filePath: string;
301+
try {
302+
filesToLoad.forEach(fp => {
303+
filePath = path.normalize(fp);
304+
files[filePath] = {
305+
text: fs.readFileSync(filePath, 'utf-8'),
306+
version: 0
307+
};
308+
});
309+
}
310+
catch (exc) {
311+
let filePathError = `A file specified in tsconfig.json could not be found: ${ filePath }`;
312+
return { error: {
313+
message: filePathError.red,
314+
rawMessage: filePathError,
315+
loaderSource: 'ts-loader'
316+
}};
317+
}
307318

308319
let newLine =
309320
compilerOptions.newLine === 0 /* CarriageReturnLineFeed */ ? '\r\n' :
@@ -341,11 +352,11 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): {
341352
// We either load from memory or from disk
342353
fileName = path.normalize(fileName);
343354
var file = files[fileName];
344-
355+
345356
if (!file) {
346357
let text = readFile(fileName);
347358
if (text == null) return;
348-
359+
349360
file = files[fileName] = { version: 0, text }
350361
}
351362

@@ -465,7 +476,7 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): {
465476
pushArray(compilation.errors, formatErrors(errors, instance, {file: filePath}));
466477
}
467478
});
468-
479+
469480
callback();
470481
});
471482

@@ -526,13 +537,13 @@ function loader(contents) {
526537
if (!file) {
527538
file = instance.files[filePath] = <TSFile>{ version: 0 };
528539
}
529-
540+
530541
if (file.text !== contents) {
531542
file.version++;
532543
file.text = contents;
533544
instance.version++;
534545
}
535-
546+
536547
var outputText: string, sourceMapText: string, diagnostics: typescript.Diagnostic[] = [];
537548

538549
if (options.transpileOnly) {
@@ -549,11 +560,11 @@ function loader(contents) {
549560
}
550561
else {
551562
let langService = instance.languageService;
552-
563+
553564
// Make this file dependent on *all* definition files in the program
554565
this.clearDependencies();
555566
this.addDependency(filePath);
556-
567+
557568
let allDefinitionFiles = Object.keys(instance.files).filter(filePath => /\.d\.ts$/.test(filePath));
558569
allDefinitionFiles.forEach(this.addDependency.bind(this));
559570
this._module.meta.tsLoaderDefinitionFileVersions = allDefinitionFiles.map(filePath => filePath+'@'+instance.files[filePath].version);
@@ -566,7 +577,7 @@ function loader(contents) {
566577

567578
var sourceMapFile = output.outputFiles.filter(file => !!file.name.match(/\.js(x?)\.map$/)).pop();
568579
if (sourceMapFile) { sourceMapText = sourceMapFile.text }
569-
580+
570581
var declarationFile = output.outputFiles.filter(file => !!file.name.match(/\.d.ts$/)).pop();
571582
if (declarationFile) { this.emitFile(path.relative(this.options.context, declarationFile.name), declarationFile.text); }
572583
}

test/tsconfigInvalidFile/app.ts

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
ERROR in ./.test/tsconfigInvalidFile/app.ts
3+
Module build failed: A file specified in tsconfig.json could not be found: i-am-a-file-what-does-not-exist.ts
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
/******/ exports: {},
15+
/******/ id: moduleId,
16+
/******/ loaded: false
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.loaded = 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+
/******/ // __webpack_public_path__
37+
/******/ __webpack_require__.p = "";
38+
39+
/******/ // Load entry module and return exports
40+
/******/ return __webpack_require__(0);
41+
/******/ })
42+
/************************************************************************/
43+
/******/ ([
44+
/* 0 */
45+
/***/ function(module, exports) {
46+
47+
48+
49+
/***/ }
50+
/******/ ]);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Asset Size Chunks Chunk Names
2+
bundle.js 1.39 kB 0 [emitted] main
3+
chunk {0} bundle.js (main) 0 bytes [rendered]
4+
[0] ./.test/tsconfigInvalidFile/app.ts 0 bytes {0} [built]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
ERROR in ./.test/tsconfigInvalidFile/app.ts
3+
Module build failed: A file specified in tsconfig.json could not be found: i-am-a-file-what-does-not-exist.ts
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
ERROR in ./.test/tsconfigInvalidFile/app.ts
3+
Module build failed: A file specified in tsconfig.json could not be found: i-am-a-file-what-does-not-exist.ts
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"sourceMap": true
4+
},
5+
"files": [
6+
"i-am-a-file-what-does-not-exist.ts",
7+
"app.ts"
8+
]
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
entry: './app.ts',
3+
output: {
4+
filename: 'bundle.js'
5+
},
6+
resolve: {
7+
extensions: ['', '.ts', '.js']
8+
},
9+
module: {
10+
loaders: [
11+
{ test: /\.ts$/, loader: 'ts-loader' }
12+
]
13+
}
14+
}
15+
16+
// for test harness purposes only, you would not need this in a normal project
17+
module.exports.resolveLoader = { alias: { 'ts-loader': require('path').join(__dirname, "../../index.js") } }

0 commit comments

Comments
 (0)