Remove assert in typescript production build using webpack

Viewed 280

The situation

I'm using TypeScript 3.9 and webpack 4.46.

assert 2.0.0 is imported like this:

import { strict as assert } from 'assert';

and I'm using only the assert function, for example:

assert(i < j);

Requirements

I want to remove assertions in the production build. More specifically:

  • I want the calls to assert completely removed, rather than the assert function being disabled in some way. This is because I want to be able to add assertions anywhere, but know that they will not impact performance in the final build.
  • I want all the assert function definitions to be removed, to reduce the size of the final code.

I assume this is a mainstream requirement, and should be easy. But I can't find a solution.

What I've looked at so far

webpack-unassert-loader

I tried webpack-unassert-loader, but the documented usage did not work:

{
    module: {
        loaders: [
            { test: /\.js$/, loader: "webpack-unassert-loader" }
        ]
    }
}

Webpack doesn't seem to like loaders. I'm guessing the usage documentation is for an earlier webpack?

I tried guessing a different way to configure it (see webpack.config.js, below), but that didn't work (the built code still has assertions in it). Maybe my guess was wrong (I'm no webpack expert).

typescript-plugin-unassert

I looked at typescript-plugin-unassert, but I was not reassured by 2 things:

  • only 7 weekly downloads
  • documentation is unclear. For example:

This plugin eliminates all of assert and assertIsDefined calls, that are introduced at the official page.

What about all the other assert functions? Which 'official page'?

Maybe it works fine, and is exaclty what I'm looking for, but I'm not encouraged.

assert-plus

I looked at assert-plus, but I was discouraged by a comment in https://stackoverflow.com/a/60833168/685715 that says:

when you deactivate the assertion thanks to the environment variable, the function is not removed but just content of the function return void.

If that's right, then it doesn't do what I need.

and so..

I'd be very grateful to anyone who can tell me what is the standard, well trodden path to remove assertions.

webpack.config.js

Here are the guts of my webpack.config.js:

{
    module: {
        rules: [
            {
                test: /\.ts$/,
                use: ['ts-loader'],
                exclude: /node_modules/
            },
            /* I tried this, but it didn't get rid of the assertions:
            {
                test: /\.js$/,
                use: ["webpack-unassert-loader"]
            },
            */
        ]
    },
        resolve: {
            extensions: [ '.ts', '.tsx', ".js", ".json", ".css"]
        },
        optimization: {
            minimize: getMinimize(env),
                minimizer: [new TerserPlugin({
                    terserOptions: {
                        compress: {
                            pure_funcs: [
                                'console.log',
                                'console.info',
                                // I tried this, just as a guess! But it didn't get
                                // rid of the assertions:
                                'assert',
                            ]
                        },
                    }
                })],
        },
}
0 Answers
Related