Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support for npm linking dependencies #141

Merged
merged 1 commit into from
Jan 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 3 additions & 0 deletions test/npmLink/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import lib from 'lib/foo';

console.log(lib);
60 changes: 60 additions & 0 deletions test/npmLink/expectedOutput-1.6/bundle.js
Original file line number Diff line number Diff line change
@@ -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';


/***/ }
/******/ ]);
5 changes: 5 additions & 0 deletions test/npmLink/expectedOutput-1.6/output.txt
Original file line number Diff line number Diff line change
@@ -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]
60 changes: 60 additions & 0 deletions test/npmLink/expectedOutput-1.7/bundle.js
Original file line number Diff line number Diff line change
@@ -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';


/***/ }
/******/ ]);
5 changes: 5 additions & 0 deletions test/npmLink/expectedOutput-1.7/output.txt
Original file line number Diff line number Diff line change
@@ -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]
62 changes: 62 additions & 0 deletions test/npmLink/expectedOutput-1.8/bundle.js
Original file line number Diff line number Diff line change
@@ -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';


/***/ }
/******/ ]);
5 changes: 5 additions & 0 deletions test/npmLink/expectedOutput-1.8/output.txt
Original file line number Diff line number Diff line change
@@ -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]
6 changes: 6 additions & 0 deletions test/npmLink/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"module": "commonjs"
},
"files": []
}
19 changes: 19 additions & 0 deletions test/npmLink/webpack.config.js
Original file line number Diff line number Diff line change
@@ -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") } }
2 changes: 2 additions & 0 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions test/testLib/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'foo';
5 changes: 5 additions & 0 deletions test/testLib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "lib",
"version": "0.0.1",
"description": "Dummy lib for npm link testing for ts-loader"
}