Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
111
rated 0 times [  118] [ 7]  / answers: 1 / hits: 16115  / 7 Years ago, sat, august 19, 2017, 12:00:00

I'm using Babel and Webpack to generate ES5 code from ES6. There are some validations that is used to reduce the mistakes i do while coding.



class Logger {
/**
* @param {LogModel} info
* {LogTypes} type
* {String} message
* {Date} date
*/
static log(info) {
if(info instanceof LogModel)
throw new Error(not a instance of LogModel);

notify(info);
}
}


In log function, I validate whether the argument is a instance of LogModel class. This is just to prevent mistakes. I don't want that if condition to be in the production because too many if condition going to slow the application. Is it possible to generate development release with validations and production release without those validations with Babel and Webpack?


More From » webpack

 Answers
7

It appears the other answers are outdated. With webpack 4, you can set mode: 'production' in your webpack config.



In your code, check for development mode like this:



if (process.env.NODE_ENV === 'development') {
if(info instanceof LogModel)
throw new Error(not a instance of LogModel);
}


When webpack creates a bundle with mode: 'production', all the code inside these if clauses, along with the if clauses themselves, will be automatically removed from the bundle.



There is no need to use the define plugin explicitly (it is used by webpack “under the hood”), and it's not necessary to use something like webpack-unassert-loader or webpack-strip-block mentioned in other answers.



Check out this little demo repo I have made to try this out: https://github.com/pahund/webpack-devprod-experiment


[#56711] Wednesday, August 16, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
triston

Total Points: 545
Total Questions: 88
Total Answers: 94

Location: Lebanon
Member since Sun, Aug 2, 2020
4 Years ago
;