Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack: How to exclude code from production bundle

How do I exclude specific lines of typescript code from a webpack bundle during build-time?

For example, I have this line of code in app.ts (nodejs application):

const thisShouldNotBeInProductionBundleJustInDevBundle = 'aaaaaaa';

When I build my app using webpack configuration, I want to exclude this code.

like image 362
Jon Sud Avatar asked Jul 13 '26 16:07

Jon Sud


1 Answers

In webpack version 4 you are able to set mode: 'production' in your webpack config. (https://webpack.js.org/concepts/mode/)

So in your source code you can use the following:

if (process.env.NODE_ENV === 'development') {
    const thisShouldNotBeInProductionBundleJustInDevBundle = 'aaaaaaa';
    ...
}

In conclusion all code inside if and ifs themself will be automatically removed during building your bundle

like image 130
Eugene Shilin Avatar answered Jul 15 '26 04:07

Eugene Shilin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!