One of the requested features in TypeScript is support for the kind of compilation constants that C#, for example, provides, where you can say
#if(DEBUG)
MessageBox.Show(...);
#endif
TypeScript doesn't support this directly, but the JavaScript minifier UglifyJS does. Here I've posted some sample code and the Gruntfile used to build it, along with the output for the two Uglify builds, one with the DEBUG
constant set to true
, and the other to false
. Where it is set to true, the if
statement is removed but its body is kept. Where it is set to false
, the if
statement and its body are completely removed.
There are also some sourcemap shenanigans in the sample code because I didn't remove them from the project I copied the Gruntfile from. They're irrelevant to this specific example, but show how Uglify can take an input sourcemap (i.e. the one generated by TypeScript) and use it to make its output sourcemap point to the .ts
files, which is pretty neat. It means you can do your dev testing and debugging using the minified code (recommended to avoid minification-related errors) and still be stepping through your TypeScript code (as long as you're using Chrome Dev Tools with Source Map support switched on, but of course you are.)