简体   繁体   English

为webpack build分离dev和prod配置文件

[英]separated dev and prod config files for webpack build

I'm developing a single page application with vuejs and using webpack to package my app. 我正在使用vuejs开发单页应用程序并使用webpack打包我的应用程序。 I have to two config file those config.dev.js and config.prod.js 我必须有两个配置文件,即config.dev.js和config.prod.js

config.dev.js config.dev.js

{
  "api-url":"http://10.0.0.50:8080"
}

config.prod.js config.prod.js

{
  "api-url":"https://api.myproduction.com"
}

I only want to include config.dev.js and exclude config.prod.js for the development environment package. 我只想包含config.dev.js并为开发环境包排除config.prod.js。 Alike only include config.prod.js and exclude config.dev.js for the production environment package. 同样只包括config.prod.js并排除生产环境包的config.dev.js。 How can I do this? 我怎样才能做到这一点?

You can do that by either having one file that returns this setting depending on NODE_ENV : 你可以通过一个文件根据NODE_ENV返回这个设置来NODE_ENVNODE_ENV

config.js config.js

module.exports = process.env.NODE_ENV === 'production'
  ? require('./config.prod.js')
  : require('./config.dev.js')

Then in your code you just require config.js and it will do the trick. 然后在你的代码中你只需要config.js就可以了。 Remember that for this to work you need to run webpack in production mode: either with the flag webpack -p or setting NODE_ENV=production webpack . 请记住,要使其工作,您需要在生产模式下运行webpack:使用标志webpack -p或设置NODE_ENV=production webpack

I also would recommend using this in combination with DefinePlugin https://webpack.js.org/plugins/define-plugin/#components/sidebar/sidebar.jsx 我也建议将它与DefinePlugin结合使用https://webpack.js.org/plugins/define-plugin/#components/sidebar/sidebar.jsx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM