Skip to content

Commit 78dd044

Browse files
authored
Merge pull request #174 from cartant/issue-148
Refactored to avoid inferring rootDir
2 parents c1fc2b9 + a707cae commit 78dd044

File tree

9 files changed

+51
-33
lines changed

9 files changed

+51
-33
lines changed

index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
22

3-
var fs = require('fs');
4-
var log = require('util').debuglog(require('./package').name);
5-
var through = require('through2');
6-
var path = require('path');
3+
var realpath = require('fs.realpath');
4+
var log = require('util').debuglog(require('./package').name);
5+
var through = require('through2');
6+
var path = require('path');
77

88
function tsify(b, opts) {
99
var ts = opts.typescript || require('typescript');
@@ -62,7 +62,7 @@ function tsify(b, opts) {
6262
}
6363
})
6464
.filter(function (file) { return file; })
65-
.map(function (file) { return fs.realpathSync(file); });
65+
.map(function (file) { return realpath.realpathSync(file); });
6666
log('Files from browserify entry points:');
6767
entries.forEach(function (file) { log(' %s', file); });
6868
tsifier.reset();

lib/Host.js

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
'use strict';
22

3-
var commondir = require('commondir');
43
var events = require('events');
54
var fs = require('fs');
6-
var realpath = require('fs.realpath')
5+
var realpath = require('fs.realpath');
76
var log = require('util').debuglog(require('../package').name);
87
var trace = require('util').debuglog(require('../package').name + '-trace');
98
var os = require('os');
109
var path = require('path');
1110
var util = require('util');
1211

1312
module.exports = function (ts) {
14-
function Host(currentDirectory, outputDirectory, languageVersion) {
13+
function Host(currentDirectory, opts) {
1514
this.currentDirectory = this.getCanonicalFileName(path.resolve(currentDirectory));
16-
this.outputDirectory = this.getCanonicalFileName(path.resolve(outputDirectory));
17-
this.languageVersion = languageVersion;
15+
this.outputDirectory = this.getCanonicalFileName(path.resolve(opts.outDir));
16+
this.rootDirectory = this.getCanonicalFileName(path.resolve(opts.rootDir));
17+
this.languageVersion = opts.target;
1818
this.files = {};
1919
this.previousFiles = {};
2020
this.output = {};
@@ -146,18 +146,6 @@ module.exports = function (ts) {
146146
return ts.sys.readFile(filename);
147147
};
148148

149-
Host.prototype._rootDir = function () {
150-
var dirs = [];
151-
for (var filename in this.files) {
152-
if (!Object.hasOwnProperty.call(this.files, filename)) continue;
153-
if (/\.d\.ts$/.test(filename)) continue;
154-
155-
dirs.push(this.getCanonicalFileName(path.dirname(filename)));
156-
}
157-
var result = commondir(this.currentDirectory, dirs);
158-
return this.getCanonicalFileName(result);
159-
};
160-
161149
Host.prototype._rootFilenames = function () {
162150

163151
var rootFilenames = [];
@@ -193,7 +181,7 @@ module.exports = function (ts) {
193181

194182
var sourceCanonical = this._canonical(filename);
195183
var outputRelative = path.relative(
196-
this._rootDir(),
184+
this.rootDirectory,
197185
sourceCanonical
198186
);
199187
var outputCanonical = this.getCanonicalFileName(path.resolve(
@@ -211,7 +199,7 @@ module.exports = function (ts) {
211199
outputCanonical
212200
);
213201
var sourceCanonical = this.getCanonicalFileName(path.resolve(
214-
this._rootDir(),
202+
this.rootDirectory,
215203
outputRelative
216204
));
217205
return sourceCanonical;

lib/Tsifier.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var convert = require('convert-source-map');
44
var events = require('events');
55
var extend = require('util')._extend;
66
var fs = require('fs');
7+
var realpath = require('fs.realpath');
78
var log = require('util').debuglog(require('../package').name);
89
var trace = require('util').debuglog(require('../package').name + '-trace');
910
var path = require('path');
@@ -15,7 +16,7 @@ var util = require('util');
1516
module.exports = function (ts) {
1617
var CompileError = require('./CompileError')(ts);
1718
var Host = require('./Host')(ts);
18-
var currentDirectory = fs.realpathSync(process.cwd());
19+
var currentDirectory = realpath.realpathSync(process.cwd());
1920

2021
var parseJsonConfigFileContent = ts.parseJsonConfigFileContent || ts.parseConfigFile;
2122

@@ -104,8 +105,8 @@ module.exports = function (ts) {
104105
// The output directory needs to be distinct from the input directory to prevent the TS
105106
// compiler from thinking that it might accidentally overwrite source files, which would
106107
// prevent it from outputting e.g. the results of transpiling ES6 JS files with --allowJs.
107-
delete parsed.options.rootDir;
108-
parsed.options.outDir = '__tsify__';
108+
parsed.options.rootDir = path.relative('.', '/');
109+
parsed.options.outDir = path.resolve('/__tsify__');
109110

110111
log('Files from tsconfig parse:');
111112
parsed.fileNames.forEach(function (filename) { log(' %s', filename); });
@@ -125,11 +126,7 @@ module.exports = function (ts) {
125126
self.opts = parsedOptions.options;
126127
self.files = parsedOptions.fileNames;
127128
self.bopts = bopts;
128-
self.host = new Host(
129-
currentDirectory,
130-
this.opts.outDir,
131-
this.opts.target
132-
);
129+
self.host = new Host(currentDirectory, self.opts);
133130

134131
self.host.on('file', function (file, id) {
135132
self.emit('file', file, id);

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"node": ">=0.12"
2828
},
2929
"dependencies": {
30-
"commondir": "^1.0.1",
3130
"convert-source-map": "^1.1.0",
3231
"through2": "^2.0.0",
3332
"tsconfig": "^2.2.0"

test/test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,27 @@ test('non-TS main file', function (t) {
101101
});
102102
});
103103

104+
test('non-TS main file and nested dependencies', function (t) {
105+
106+
// The workaround mentioned in issue #148 - an empty TS file in the root -
107+
// is no longer required.
108+
109+
process.chdir('./test/withJsRootAndNestedDeps');
110+
run({
111+
bOpts: { entries: ['./x.js'] }
112+
}, function (errors, actual) {
113+
expectNoErrors(t, errors);
114+
expectConsoleOutputFromScript(t, actual, [
115+
'hello world',
116+
'222'
117+
]);
118+
expectMappedToken(t, 'nested/y.ts', actual, 'console.log(message)');
119+
expectMappedToken(t, 'nested/twice/z.ts', actual, '111');
120+
process.chdir('../..');
121+
t.end();
122+
});
123+
});
124+
104125
test('with adjacent compiled files', function (t) {
105126
run({
106127
bOpts: { entries: ['./test/withAdjacentCompiledFiles/x.ts'] }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export = fn;
2+
function fn(n: number) {
3+
return n*111;
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export = fn;
2+
function fn(message: string) {
3+
console.log(message);
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

test/withJsRootAndNestedDeps/x.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var y = require('./nested/y');
2+
var z = require('./nested/twice/z');
3+
y('hello world');
4+
y(z(2).toString());

0 commit comments

Comments
 (0)