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:
;assertsomething === someOtherThingThatShouldBeEqualToSomething;return assertfalse; // 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
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.
npm install --save-dev ts-transformer-unassert
Using with webpack and ts-loader
Add this at the top of your production
const unassert = default;
And add the following option to your ts-loader options:
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.gitcd ts-transformer-unassertnpm installnpm run buildnpm 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).
- Fatih Aygün - Initial work - cyco130