diff --git a/index.ts b/index.ts index 026a11282..d4bb38fc1 100644 --- a/index.ts +++ b/index.ts @@ -313,12 +313,19 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): { // make a (sync) resolver that follows webpack's rules let resolver = makeResolver(loader.options); - var moduleResolutionHost = { - fileExists: (fileName: string) => { return servicesHost.getScriptSnapshot(fileName) !== undefined; }, - readFile: (fileName: string) => { - let snapshot = servicesHost.getScriptSnapshot(fileName); - return snapshot && snapshot.getText(0, snapshot.getLength()); + var readFile = function(fileName) { + fileName = path.normalize(fileName); + try { + return fs.readFileSync(fileName, {encoding: 'utf8'}) + } + catch (e) { + return; } + } + + var moduleResolutionHost = { + fileExists: (fileName: string) => readFile(fileName) !== undefined, + readFile: (fileName: string) => readFile(fileName) }; // Create the TypeScript language service @@ -334,17 +341,12 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): { // We either load from memory or from disk fileName = path.normalize(fileName); var file = files[fileName]; - + if (!file) { - try { - file = files[fileName] = { - version: 0, - text: fs.readFileSync(fileName, {encoding: 'utf8'}) - } - } - catch (e) { - return; - } + let text = readFile(fileName); + if (text == null) return; + + file = files[fileName] = { version: 0, text } } return compiler.ScriptSnapshot.fromString(file.text); diff --git a/package.json b/package.json index 8f095cc3b..24ec78916 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "build": "tsc", - "test": "mocha --reporter spec test/run.js", + "test": "npm link ./test/testLib && mocha --reporter spec test/run.js", "prepublish": "npm run build" }, "repository": { diff --git a/test/npmLink/app.ts b/test/npmLink/app.ts new file mode 100644 index 000000000..23ca1f896 --- /dev/null +++ b/test/npmLink/app.ts @@ -0,0 +1,3 @@ +import lib from 'lib/foo'; + +console.log(lib); \ No newline at end of file diff --git a/test/npmLink/expectedOutput-1.6/bundle.js b/test/npmLink/expectedOutput-1.6/bundle.js new file mode 100644 index 000000000..57408d27c --- /dev/null +++ b/test/npmLink/expectedOutput-1.6/bundle.js @@ -0,0 +1,60 @@ +/******/ (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, __webpack_require__) { + + var foo_1 = __webpack_require__(1); + console.log(foo_1["default"]); + + +/***/ }, +/* 1 */ +/***/ function(module, exports) { + + exports.__esModule = true; + exports["default"] = 'foo'; + + +/***/ } +/******/ ]); \ No newline at end of file diff --git a/test/npmLink/expectedOutput-1.6/output.txt b/test/npmLink/expectedOutput-1.6/output.txt new file mode 100644 index 000000000..27dc3f444 --- /dev/null +++ b/test/npmLink/expectedOutput-1.6/output.txt @@ -0,0 +1,5 @@ + Asset Size Chunks Chunk Names +bundle.js 1.59 kB 0 [emitted] main +chunk {0} bundle.js (main) 118 bytes [rendered] + [0] ./.test/npmLink/app.ts 63 bytes {0} [built] + [1] ./test/testLib/foo.ts 55 bytes {0} [built] \ No newline at end of file diff --git a/test/npmLink/expectedOutput-1.7/bundle.js b/test/npmLink/expectedOutput-1.7/bundle.js new file mode 100644 index 000000000..57408d27c --- /dev/null +++ b/test/npmLink/expectedOutput-1.7/bundle.js @@ -0,0 +1,60 @@ +/******/ (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, __webpack_require__) { + + var foo_1 = __webpack_require__(1); + console.log(foo_1["default"]); + + +/***/ }, +/* 1 */ +/***/ function(module, exports) { + + exports.__esModule = true; + exports["default"] = 'foo'; + + +/***/ } +/******/ ]); \ No newline at end of file diff --git a/test/npmLink/expectedOutput-1.7/output.txt b/test/npmLink/expectedOutput-1.7/output.txt new file mode 100644 index 000000000..27dc3f444 --- /dev/null +++ b/test/npmLink/expectedOutput-1.7/output.txt @@ -0,0 +1,5 @@ + Asset Size Chunks Chunk Names +bundle.js 1.59 kB 0 [emitted] main +chunk {0} bundle.js (main) 118 bytes [rendered] + [0] ./.test/npmLink/app.ts 63 bytes {0} [built] + [1] ./test/testLib/foo.ts 55 bytes {0} [built] \ No newline at end of file diff --git a/test/npmLink/expectedOutput-1.8/bundle.js b/test/npmLink/expectedOutput-1.8/bundle.js new file mode 100644 index 000000000..5f43846f5 --- /dev/null +++ b/test/npmLink/expectedOutput-1.8/bundle.js @@ -0,0 +1,62 @@ +/******/ (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, __webpack_require__) { + + "use strict"; + var foo_1 = __webpack_require__(1); + console.log(foo_1["default"]); + + +/***/ }, +/* 1 */ +/***/ function(module, exports) { + + "use strict"; + exports.__esModule = true; + exports["default"] = 'foo'; + + +/***/ } +/******/ ]); \ No newline at end of file diff --git a/test/npmLink/expectedOutput-1.8/output.txt b/test/npmLink/expectedOutput-1.8/output.txt new file mode 100644 index 000000000..ec351c279 --- /dev/null +++ b/test/npmLink/expectedOutput-1.8/output.txt @@ -0,0 +1,5 @@ + Asset Size Chunks Chunk Names +bundle.js 1.62 kB 0 [emitted] main +chunk {0} bundle.js (main) 146 bytes [rendered] + [0] ./.test/npmLink/app.ts 77 bytes {0} [built] + [1] ./test/testLib/foo.ts 69 bytes {0} [built] \ No newline at end of file diff --git a/test/npmLink/tsconfig.json b/test/npmLink/tsconfig.json new file mode 100644 index 000000000..fa51fe925 --- /dev/null +++ b/test/npmLink/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "module": "commonjs" + }, + "files": [] +} \ No newline at end of file diff --git a/test/npmLink/webpack.config.js b/test/npmLink/webpack.config.js new file mode 100644 index 000000000..bb75bffbb --- /dev/null +++ b/test/npmLink/webpack.config.js @@ -0,0 +1,19 @@ +var path = require('path') + +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 diff --git a/test/run.js b/test/run.js index dcfac86d4..f1f825a23 100644 --- a/test/run.js +++ b/test/run.js @@ -33,6 +33,8 @@ fs.readdirSync(__dirname).forEach(function(test) { var testPath = path.join(__dirname, test); if (fs.statSync(testPath).isDirectory()) { + if (test == 'testLib') return; + if (test == 'issue81' && semver.lt(typescript.version, '1.7.0-0')) return; describe(test, function() { diff --git a/test/testLib/foo.ts b/test/testLib/foo.ts new file mode 100644 index 000000000..5a549c58c --- /dev/null +++ b/test/testLib/foo.ts @@ -0,0 +1 @@ +export default 'foo'; \ No newline at end of file diff --git a/test/testLib/package.json b/test/testLib/package.json new file mode 100644 index 000000000..efed2eff7 --- /dev/null +++ b/test/testLib/package.json @@ -0,0 +1,5 @@ +{ + "name": "lib", + "version": "0.0.1", + "description": "Dummy lib for npm link testing for ts-loader" +}