Webpack,es6 dynamically selects import according to the environment

for example, when I am using vconsole. Use npm to install the js file import in my entry. I use it in a development environment, but in a production environment I don"t want to package vconsole into my js file.

I can selectively initialize vconsole in the form of environment variables. But as long as the import, the js will be packaged into the final js file.

May.08,2021

you can consider this. Although it will still be packaged, it will only be downloaded if DEBUG is true.

if (process.env.NODE_ENV === "production") {
  vconsole = function mock() { // do nothing };
} else {
  vconsole = require("vconsole");
}
Menu