a1exlism
3/3/2018 - 7:50 AM

webpack_Environment_flags

document.write('<h1>Hello World</h1>');

if (__DEV__) {
  document.write(new Date());
}
/* package.json */
{
  /* add --mode in webpack4.x */
  "scripts": {
    "dev": "cross-env DEBUG=true webpack-dev-server --open",
  },
  /* ... */
}
const webpack = require('webpack');

let devFlagPlugin = new webpack.DefinePlugin({
  __DEV__: JSON.stringify(JSON.parse(process.env.DEBUG || 'false'))
});

module.exports = {
  entry: './main.js',
  output: {
    filename: 'bundle.js'
  },
  plugins: [devFlagPlugin]
};