ts-transformer-unassert v2.0.1
ts-transformer-unassert
TypeScript custom transformer that strips assertions in production code. It is intended to be used with ts-simple-assert but other assert libraries may work too.
In other words, it takes this:
import { assert } from "ts-simple-assert";
assert(something === someOtherThingThatShouldBeEqualToSomething);
return assert(false); // Sometimes useful for making the TypeScript compiler happy
// by marking some code that should be unreachable
and turns it into this:
;
void 0;
return void 0;
Yep. They're gone. OK, actually it doesn't remove the comments but you get the picture. void 0
simply means undefined
and it can be removed by your a JavaScript minifier.
Version 1 of ts-transformer-unassert used to completely remove call statements and return statements only. The current version removes all expressions and replaces them with void 0
. Both versions completely remove import statements.
Getting Started
Prerequisites
ts-transformer-unassert requires an up-to-date version of typescript and a task runner capable of running TypeScript custom transformers. It's been only tested with webpack and ts-loader.
Installing
npm install --save-dev ts-transformer-unassert
Using with webpack and ts-loader
Add this at the top of your production webpack.config.js
:
const unassert = require("ts-transformer-unassert").default;
And add the following option to your ts-loader options:
getCustomTransformers: () => ({ before: [unassert] })
The ts-loader readme points to some more info on how to run custom transformers.
Building and Running the tests
Building and running the (extremely simplistic) tests requires a Unix-like shell environment. Clone the repo, install the dependencies, build, and finally run the tests with the following commands:
git clone https://github.com/cyco130/ts-transformer-unassert.git
cd ts-transformer-unassert
npm install
npm run build
npm test
The tests consists of creating a bundle with webpack without using ts-transformer-unassert; running it to make sure that it fails (because of an assert(false);
line in the code); recreating the same bundle, this time using ts-transformer-unassert; and running it to make sure that it doesn't fail anymore (because the assert
call has been stripped).
Authors
- Fatih Aygün - Initial work - cyco130