How to match part of the code block through webpack packaging?

eg:


ifisProd{
    //do something for prods

   
}else{
   //do something for others
}

 webpack   

//do something for prods

There is a corresponding plug-in in

gulp, which is via regular matching Filter. No corresponding plug-ins have been found in webpack for the time being! Do you have any recommendations. Thank you!

I know how to judge the environment. The key thing is to remove the code snippets from the non-production environment (when packaging) and reduce the amount of code deployed! So that there is only production-related code in the production environment, and there is no judgment logic code in other environments!


webpack has a simple plug-in tailoring code block that works well. Webpack-dists-loader https://www.npmjs.com/package.


I don't know what you mean. Do you want to do demand loading? If so, it is recommended that you take a look at route cutting


webpack has a plug-in called DefinePlugin


do you mean this configuration in plugins in webpack?

new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),

this is told to webpack when packing. If you want to make your own if judgment, you can set variables when you start the project, such as

.
//npm start cross-env NODE_ENV=development || cross-env NODE_ENV=production

// 
const prod = process.env.NODE_ENV === 'production' ? true : false;

123 production and development environments

Menu