Skip to content

Refactored to avoid inferring rootDir #174

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

Merged
merged 3 commits into from
Aug 7, 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
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

var fs = require('fs');
var log = require('util').debuglog(require('./package').name);
var through = require('through2');
var path = require('path');
var realpath = require('fs.realpath');
var log = require('util').debuglog(require('./package').name);
var through = require('through2');
var path = require('path');

function tsify(b, opts) {
var ts = opts.typescript || require('typescript');
Expand Down Expand Up @@ -62,7 +62,7 @@ function tsify(b, opts) {
}
})
.filter(function (file) { return file; })
.map(function (file) { return fs.realpathSync(file); });
.map(function (file) { return realpath.realpathSync(file); });
log('Files from browserify entry points:');
entries.forEach(function (file) { log(' %s', file); });
tsifier.reset();
Expand Down
26 changes: 7 additions & 19 deletions lib/Host.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
'use strict';

var commondir = require('commondir');
var events = require('events');
var fs = require('fs');
var realpath = require('fs.realpath')
var realpath = require('fs.realpath');
var log = require('util').debuglog(require('../package').name);
var trace = require('util').debuglog(require('../package').name + '-trace');
var os = require('os');
var path = require('path');
var util = require('util');

module.exports = function (ts) {
function Host(currentDirectory, outputDirectory, languageVersion) {
function Host(currentDirectory, opts) {
this.currentDirectory = this.getCanonicalFileName(path.resolve(currentDirectory));
this.outputDirectory = this.getCanonicalFileName(path.resolve(outputDirectory));
this.languageVersion = languageVersion;
this.outputDirectory = this.getCanonicalFileName(path.resolve(opts.outDir));
this.rootDirectory = this.getCanonicalFileName(path.resolve(opts.rootDir));
this.languageVersion = opts.target;
this.files = {};
this.previousFiles = {};
this.output = {};
Expand Down Expand Up @@ -146,18 +146,6 @@ module.exports = function (ts) {
return ts.sys.readFile(filename);
};

Host.prototype._rootDir = function () {
var dirs = [];
for (var filename in this.files) {
if (!Object.hasOwnProperty.call(this.files, filename)) continue;
if (/\.d\.ts$/.test(filename)) continue;

dirs.push(this.getCanonicalFileName(path.dirname(filename)));
}
var result = commondir(this.currentDirectory, dirs);
return this.getCanonicalFileName(result);
};

Host.prototype._rootFilenames = function () {

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

var sourceCanonical = this._canonical(filename);
var outputRelative = path.relative(
this._rootDir(),
this.rootDirectory,
sourceCanonical
);
var outputCanonical = this.getCanonicalFileName(path.resolve(
Expand All @@ -211,7 +199,7 @@ module.exports = function (ts) {
outputCanonical
);
var sourceCanonical = this.getCanonicalFileName(path.resolve(
this._rootDir(),
this.rootDirectory,
outputRelative
));
return sourceCanonical;
Expand Down
13 changes: 5 additions & 8 deletions lib/Tsifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var convert = require('convert-source-map');
var events = require('events');
var extend = require('util')._extend;
var fs = require('fs');
var realpath = require('fs.realpath');
var log = require('util').debuglog(require('../package').name);
var trace = require('util').debuglog(require('../package').name + '-trace');
var path = require('path');
Expand All @@ -15,7 +16,7 @@ var util = require('util');
module.exports = function (ts) {
var CompileError = require('./CompileError')(ts);
var Host = require('./Host')(ts);
var currentDirectory = fs.realpathSync(process.cwd());
var currentDirectory = realpath.realpathSync(process.cwd());

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

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

log('Files from tsconfig parse:');
parsed.fileNames.forEach(function (filename) { log(' %s', filename); });
Expand All @@ -125,11 +126,7 @@ module.exports = function (ts) {
self.opts = parsedOptions.options;
self.files = parsedOptions.fileNames;
self.bopts = bopts;
self.host = new Host(
currentDirectory,
this.opts.outDir,
this.opts.target
);
self.host = new Host(currentDirectory, self.opts);

self.host.on('file', function (file, id) {
self.emit('file', file, id);
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"node": ">=0.12"
},
"dependencies": {
"commondir": "^1.0.1",
"convert-source-map": "^1.1.0",
"through2": "^2.0.0",
"tsconfig": "^2.2.0"
Expand Down
21 changes: 21 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,27 @@ test('non-TS main file', function (t) {
});
});

test('non-TS main file and nested dependencies', function (t) {

// The workaround mentioned in issue #148 - an empty TS file in the root -
// is no longer required.

process.chdir('./test/withJsRootAndNestedDeps');
run({
bOpts: { entries: ['./x.js'] }
}, function (errors, actual) {
expectNoErrors(t, errors);
expectConsoleOutputFromScript(t, actual, [
'hello world',
'222'
]);
expectMappedToken(t, 'nested/y.ts', actual, 'console.log(message)');
expectMappedToken(t, 'nested/twice/z.ts', actual, '111');
process.chdir('../..');
t.end();
});
});

test('with adjacent compiled files', function (t) {
run({
bOpts: { entries: ['./test/withAdjacentCompiledFiles/x.ts'] }
Expand Down
4 changes: 4 additions & 0 deletions test/withJsRootAndNestedDeps/nested/twice/z.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export = fn;
function fn(n: number) {
return n*111;
}
4 changes: 4 additions & 0 deletions test/withJsRootAndNestedDeps/nested/y.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export = fn;
function fn(message: string) {
console.log(message);
}
1 change: 1 addition & 0 deletions test/withJsRootAndNestedDeps/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
4 changes: 4 additions & 0 deletions test/withJsRootAndNestedDeps/x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var y = require('./nested/y');
var z = require('./nested/twice/z');
y('hello world');
y(z(2).toString());