guzart/browserify-typescriptifier

Compilation errors are not reported

Closed this issue · 6 comments

smrq commented

If you have any errors in your Typescript, they will be silently ignored and Javascript output will be passed along anyway.

Could you provide a simple example? I looked at the transform source code and it seems to be reporting errors correctly.

If you are using the API you might need to add error handling to your code, take a look at this ticket: browserify/browserify#763

smrq commented

The simplest example I can imagine, using the command line:

foo.ts

import

Command line

C:\Code\test>tsc foo.ts
C:\Code\test\foo.ts(1,1): error TS1008: Unexpected token; 'module, class, interface, enum, import or statement' expected.

C:\Code\test>cat foo.js
cat: foo.js: No such file or directory

C:\Code\test>browserify -t typescriptifier foo.ts > foo.js

C:\Code\test>cat foo.js
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="fu
nction"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Ca
nnot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,functi
on(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports
}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);re
turn s})({1:[function(require,module,exports){

},{}]},{},[1])

Using the TS compiler normally spits out a syntax error and no JS output; browserify with typescriptifier emits an empty browserify wrapper and no visible errors.

Another test, this time with type checker errors instead of syntax errors, and multiple files:

foo.ts

import bar = require('./bar');
console.log(bar());

bar.ts

function bar(x: string) {
    return Math.sqrt(x);
}
export = bar;

Command line

C:\Code\test>tsc foo.ts --module commonjs
C:\Code\test\bar.ts(2,14): error TS2082: Supplied parameters do not match any signature of call target:
        Could not apply type 'number' to argument 1 which is of type 'string'.
C:\Code\test\bar.ts(2,14): error TS2087: Could not select overload for 'call' expression.
C:\Code\test\foo.ts(2,13): error TS2081: Supplied parameters do not match any signature of call target.
C:\Code\test\foo.ts(2,13): error TS2087: Could not select overload for 'call' expression.

C:\Code\test>cat foo.js
var bar = require('./bar');
console.log(bar());

C:\Code\test>cat bar.js
function bar(x) {
    return Math.sqrt(x);
}
module.exports = bar;

C:\Code\test>browserify -t typescriptifier --extension=.ts foo.ts > bundle.js

C:\Code\test>cat bundle.js
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="fu
nction"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Ca
nnot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,functi
on(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports
}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);re
turn s})({1:[function(require,module,exports){
function bar(x) {
    return Math.sqrt(x);
}
module.exports = bar;

},{}],2:[function(require,module,exports){
var bar = require('./bar');
console.log(bar());

},{"./bar":1}]},{},[2])

The output here is basically right, but again there is no indication of the compilation errors.

@smrq Sorry it took me so long, am getting back into it. I think I found a solution but am testing it first on https://github.com/guzart/gulp-typescript-compiler, should get it out for this transform by next week.

any news on this issue?

smrq commented

I ended up writing my own TypeScript plugin for Browserify instead, so I personally haven't looked into this issue any more.

@smrq That's awesome, sorry I couldn't put more work into it, I stopped using typescript in favour of es6.