Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

前端代码里如何做到配置变量的值根据不同环境切换? #28

Open
JesseZhao1990 opened this issue Jan 4, 2018 · 0 comments

Comments

@JesseZhao1990
Copy link
Owner

使用场景

思考一下,如果一些配置,在项目中多处使用,如果有修改,仅改此一处就ok。是不是省心省力?
如果有一些配置,在开发环境和发布环境不一样,怎么办?在发布前修改代码?是不是闹心?
设计配置文件大概解决如上两个问题。

规范

  • 1.开发环境和发布环境一样的配置,写到base.js中
  • 2.开发环境的配置,写到dev.js中
  • 3.发布环境的配置,写到build.js中

原理解释

通过webpack的alias,开发和发布时,加载不同文件来解决开发和发布两个环境的配置不一样的问题


  resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      'cfg':process.env.NODE_ENV === 'production'
      ? path.join(__dirname, '..', 'src/config/build.js')
      : path.join(__dirname, '..', 'src/config/dev.js')   
    }
  }

使用方法

import cfg from 'cfg';
console.log(cfg.XXX);

@JesseZhao1990 JesseZhao1990 changed the title 前端代码里如何做到配置变量的值根本不同环境切换? 前端代码里如何做到配置变量的值根据不同环境切换? Jan 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant