Passing environment-dependent variables in webpack

后端 未结 15 2219
醉酒成梦
醉酒成梦 2020-11-22 12:46

I\'m trying to convert an angular app from gulp to webpack. in gulp I use gulp-preprocess to replace some variables in the html page (e.g. database name) depending on the NO

15条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 13:19

    To add to the bunch of answers personally I prefer the following:

    const webpack = require('webpack');
    const prod = process.argv.indexOf('-p') !== -1;
    
    module.exports = {
      ...
      plugins: [
        new webpack.DefinePlugin({
          process: {
            env: {
              NODE_ENV: prod? `"production"`: '"development"'
            }
          }
        }),
        ...
      ]
    };
    

    Using this there is no funky env variable or cross-platform problems (with env vars). All you do is run the normal webpack or webpack -p for dev or production respectively.

    Reference: Github issue

提交回复
热议问题