Skip to content

Commit 8c3ac6c

Browse files
authored
Merge pull request #173 from cartant/issue-172
Added typescript@next work-around for node_modules. Closes #172.
2 parents aba47ab + d204483 commit 8c3ac6c

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

lib/Host.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var trace = require('util').debuglog(require('../package').name + '-trace');
88
var os = require('os');
99
var path = require('path');
1010
var util = require('util');
11+
var semver = require('semver');
1112

1213
module.exports = function (ts) {
1314
function Host(currentDirectory, opts) {
@@ -81,7 +82,8 @@ module.exports = function (ts) {
8182
contents: text,
8283
ts: file,
8384
root: root,
84-
version: version
85+
version: version,
86+
nodeModule: /\/node_modules\/(?!typescript\/)/.test(canonical)
8587
};
8688
this.emit('file', canonical, relative);
8789

@@ -158,6 +160,34 @@ module.exports = function (ts) {
158160
return rootFilenames;
159161
}
160162

163+
Host.prototype._nodeModuleFilenames = function () {
164+
165+
var nodeModuleFilenames = [];
166+
167+
for (var filename in this.files) {
168+
if (!Object.hasOwnProperty.call(this.files, filename)) continue;
169+
if (!this.files[filename].nodeModule) continue;
170+
nodeModuleFilenames.push(filename);
171+
}
172+
return nodeModuleFilenames;
173+
}
174+
175+
Host.prototype._compile = function (opts) {
176+
177+
var rootFilenames = this._rootFilenames();
178+
var nodeModuleFilenames = [];
179+
180+
log('Compiling files:');
181+
rootFilenames.forEach(function (file) { log(' %s', file); });
182+
183+
if (semver.gte(ts.version, '2.0.0')) {
184+
ts.createProgram(rootFilenames, opts, this);
185+
nodeModuleFilenames = this._nodeModuleFilenames();
186+
log(' + %d file(s) found in node_modules', nodeModuleFilenames.length);
187+
}
188+
return ts.createProgram(rootFilenames.concat(nodeModuleFilenames), opts, this);
189+
}
190+
161191
Host.prototype._output = function (filename) {
162192

163193
var outputCanonical = this._inferOutputCanonical(filename);

lib/Tsifier.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,9 @@ module.exports = function (ts) {
157157

158158
Tsifier.prototype.compile = function () {
159159
var self = this;
160-
var rootFilenames = self.host._rootFilenames();
161-
162-
log('Compiling files:');
163-
rootFilenames.forEach(function (file) { log(' %s', file); });
164160

165161
var createProgram_t0 = time.start();
166-
var program = ts.createProgram(rootFilenames, self.opts, self.host);
162+
var program = self.host._compile(self.opts);
167163
time.stop(createProgram_t0, 'createProgram');
168164

169165
var syntaxDiagnostics = self.checkSyntax(program);

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
},
2929
"dependencies": {
3030
"convert-source-map": "^1.1.0",
31+
"fs.realpath": "^1.0.0",
32+
"semver": "^5.1.0",
3133
"through2": "^2.0.0",
32-
"tsconfig": "^2.2.0",
33-
"fs.realpath": "^1.0.0"
34+
"tsconfig": "^2.2.0"
3435
},
3536
"devDependencies": {
3637
"babel-preset-react": "^6.5.0",
@@ -40,7 +41,6 @@
4041
"event-stream": "^3.3.1",
4142
"fs-extra": "^0.30.0",
4243
"node-foo": "^0.2.3",
43-
"semver": "^5.1.0",
4444
"source-map": "^0.5.3",
4545
"tape": "^4.0.0",
4646
"test-peer-range": "^1.0.1",

0 commit comments

Comments
 (0)