Conditional build based on environment using Webpack

前端 未结 9 674
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 21:40

I have some things for development - e.g mocks which I would like to not bloat my distributed build file with.

In RequireJS you can pass a config in a plugin file an

9条回答
  •  甜味超标
    2020-11-30 22:24

    I've struggled with setting env in my webpack configs. What I usually want is to set env so that it can be reached inside webpack.config.js, postcss.config.js and inside the entry point application itself (index.js usually). I hope that my findings can help someone.

    The solution that I've come up with is to pass in --env production or --env development, and then set mode inside webpack.config.js. However, that doesn't help me with making env accessible where I want it (see above), so I also need to set process.env.NODE_ENV explicitly, as recommended here. Most relevant part that I have in webpack.config.js follow below.

    ...
    module.exports = mode => {
      process.env.NODE_ENV = mode;
    
      if (mode === "production") {
        return merge(commonConfig, productionConfig, { mode });
      }
      return merge(commonConfig, developmentConfig, { mode });
    };
    

提交回复
热议问题