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

roadhog+dva中环境变量的配置 #8

Closed
javanli opened this issue Jul 6, 2020 · 0 comments
Closed

roadhog+dva中环境变量的配置 #8

javanli opened this issue Jul 6, 2020 · 0 comments

Comments

@javanli
Copy link
Owner

javanli commented Jul 6, 2020

有时候我们在代码里需要根据环境变量来决定一些逻辑。常见的比如,在测试环境访问的后端url跟正式环境是不一样的。

不依赖框架的话,应当是基于webpack的define-plugin实现。如文档中所示的:

new webpack.DefinePlugin({
  PRODUCTION: JSON.stringify(true),
  VERSION: JSON.stringify('5fa3b9'),
  BROWSER_SUPPORTS_HTML5: true,
  TWO: '1+1',
  'typeof window': JSON.stringify('object')
});

回到dva。
dva核心是个基于redux封装的数据流方案,也可以当成一个轻量级框架。从框架的角度来讲,它其实很轻很轻,几乎没怎么管数据流之外的事情,只是简单地集成了少许几个库形成一个框架。

roadhog是个服务于框架的命令行工具,主要就是提供dev、build 和 test 等命令,屏蔽了webpack的复杂配置,提供了自己的相对简单的配置能力。

显然,这里的变量配置应当由roadhog来处理。不要像我当初一样觉得dva是个框架就应该有相关功能_(:зゝ∠)_

善用搜索引擎,从roadhog文档 - define相关讨论容易找到方案。roadhog提供了define选项做DefinePlugin的事情。
具体使用:编辑.webpacrc.js

export default {
    define: {
        'process.env': {},
        'process.env.NODE_ENV': process.env.NODE_ENV,
        'process.env.API_ENV': process.env.API_ENV,
    },
}

@javanli javanli closed this as completed Sep 6, 2022
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