diff --git a/index.ts b/index.ts index d4bb38fc1..be2b7630f 100644 --- a/index.ts +++ b/index.ts @@ -297,13 +297,24 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): { } // Load initial files (core lib files, any files specified in tsconfig.json) - filesToLoad.forEach(filePath => { - filePath = path.normalize(filePath); - files[filePath] = { - text: fs.readFileSync(filePath, 'utf-8'), - version: 0 - } - }); + let filePath: string; + try { + filesToLoad.forEach(fp => { + filePath = path.normalize(fp); + files[filePath] = { + text: fs.readFileSync(filePath, 'utf-8'), + version: 0 + }; + }); + } + catch (exc) { + let filePathError = `A file specified in tsconfig.json could not be found: ${ filePath }`; + return { error: { + message: filePathError.red, + rawMessage: filePathError, + loaderSource: 'ts-loader' + }}; + } let newLine = compilerOptions.newLine === 0 /* CarriageReturnLineFeed */ ? '\r\n' : @@ -341,11 +352,11 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): { // We either load from memory or from disk fileName = path.normalize(fileName); var file = files[fileName]; - + if (!file) { let text = readFile(fileName); if (text == null) return; - + file = files[fileName] = { version: 0, text } } @@ -465,7 +476,7 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): { pushArray(compilation.errors, formatErrors(errors, instance, {file: filePath})); } }); - + callback(); }); @@ -526,13 +537,13 @@ function loader(contents) { if (!file) { file = instance.files[filePath] = { version: 0 }; } - + if (file.text !== contents) { file.version++; file.text = contents; instance.version++; } - + var outputText: string, sourceMapText: string, diagnostics: typescript.Diagnostic[] = []; if (options.transpileOnly) { @@ -549,11 +560,11 @@ function loader(contents) { } else { let langService = instance.languageService; - + // Make this file dependent on *all* definition files in the program this.clearDependencies(); this.addDependency(filePath); - + let allDefinitionFiles = Object.keys(instance.files).filter(filePath => /\.d\.ts$/.test(filePath)); allDefinitionFiles.forEach(this.addDependency.bind(this)); this._module.meta.tsLoaderDefinitionFileVersions = allDefinitionFiles.map(filePath => filePath+'@'+instance.files[filePath].version); @@ -566,7 +577,7 @@ function loader(contents) { var sourceMapFile = output.outputFiles.filter(file => !!file.name.match(/\.js(x?)\.map$/)).pop(); if (sourceMapFile) { sourceMapText = sourceMapFile.text } - + var declarationFile = output.outputFiles.filter(file => !!file.name.match(/\.d.ts$/)).pop(); if (declarationFile) { this.emitFile(path.relative(this.options.context, declarationFile.name), declarationFile.text); } } diff --git a/test/tsconfigInvalidFile/app.ts b/test/tsconfigInvalidFile/app.ts new file mode 100644 index 000000000..e69de29bb diff --git a/test/tsconfigInvalidFile/expectedOutput-1.6/output.txt b/test/tsconfigInvalidFile/expectedOutput-1.6/output.txt new file mode 100644 index 000000000..32c729263 --- /dev/null +++ b/test/tsconfigInvalidFile/expectedOutput-1.6/output.txt @@ -0,0 +1,3 @@ + +ERROR in ./.test/tsconfigInvalidFile/app.ts +Module build failed: A file specified in tsconfig.json could not be found: i-am-a-file-what-does-not-exist.ts \ No newline at end of file diff --git a/test/tsconfigInvalidFile/expectedOutput-1.7/bundle.transpiled.js b/test/tsconfigInvalidFile/expectedOutput-1.7/bundle.transpiled.js new file mode 100644 index 000000000..53daa020b --- /dev/null +++ b/test/tsconfigInvalidFile/expectedOutput-1.7/bundle.transpiled.js @@ -0,0 +1,50 @@ +/******/ (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] = { +/******/ exports: {}, +/******/ id: moduleId, +/******/ loaded: false +/******/ }; + +/******/ // Execute the module function +/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); + +/******/ // Flag the module as loaded +/******/ module.loaded = 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; + +/******/ // __webpack_public_path__ +/******/ __webpack_require__.p = ""; + +/******/ // Load entry module and return exports +/******/ return __webpack_require__(0); +/******/ }) +/************************************************************************/ +/******/ ([ +/* 0 */ +/***/ function(module, exports) { + + + +/***/ } +/******/ ]); \ No newline at end of file diff --git a/test/tsconfigInvalidFile/expectedOutput-1.7/output.transpiled.txt b/test/tsconfigInvalidFile/expectedOutput-1.7/output.transpiled.txt new file mode 100644 index 000000000..d47d7531c --- /dev/null +++ b/test/tsconfigInvalidFile/expectedOutput-1.7/output.transpiled.txt @@ -0,0 +1,4 @@ + Asset Size Chunks Chunk Names +bundle.js 1.39 kB 0 [emitted] main +chunk {0} bundle.js (main) 0 bytes [rendered] + [0] ./.test/tsconfigInvalidFile/app.ts 0 bytes {0} [built] \ No newline at end of file diff --git a/test/tsconfigInvalidFile/expectedOutput-1.7/output.txt b/test/tsconfigInvalidFile/expectedOutput-1.7/output.txt new file mode 100644 index 000000000..32c729263 --- /dev/null +++ b/test/tsconfigInvalidFile/expectedOutput-1.7/output.txt @@ -0,0 +1,3 @@ + +ERROR in ./.test/tsconfigInvalidFile/app.ts +Module build failed: A file specified in tsconfig.json could not be found: i-am-a-file-what-does-not-exist.ts \ No newline at end of file diff --git a/test/tsconfigInvalidFile/expectedOutput-1.8/output.txt b/test/tsconfigInvalidFile/expectedOutput-1.8/output.txt new file mode 100644 index 000000000..32c729263 --- /dev/null +++ b/test/tsconfigInvalidFile/expectedOutput-1.8/output.txt @@ -0,0 +1,3 @@ + +ERROR in ./.test/tsconfigInvalidFile/app.ts +Module build failed: A file specified in tsconfig.json could not be found: i-am-a-file-what-does-not-exist.ts \ No newline at end of file diff --git a/test/tsconfigInvalidFile/tsconfig.json b/test/tsconfigInvalidFile/tsconfig.json new file mode 100644 index 000000000..835172124 --- /dev/null +++ b/test/tsconfigInvalidFile/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "sourceMap": true + }, + "files": [ + "i-am-a-file-what-does-not-exist.ts", + "app.ts" + ] +} diff --git a/test/tsconfigInvalidFile/webpack.config.js b/test/tsconfigInvalidFile/webpack.config.js new file mode 100644 index 000000000..aae026d84 --- /dev/null +++ b/test/tsconfigInvalidFile/webpack.config.js @@ -0,0 +1,17 @@ +module.exports = { + entry: './app.ts', + output: { + filename: 'bundle.js' + }, + resolve: { + extensions: ['', '.ts', '.js'] + }, + module: { + loaders: [ + { test: /\.ts$/, loader: 'ts-loader' } + ] + } +} + +// for test harness purposes only, you would not need this in a normal project +module.exports.resolveLoader = { alias: { 'ts-loader': require('path').join(__dirname, "../../index.js") } } \ No newline at end of file