简体   繁体   English

Webpack React:有条件地加载json配置文件

[英]Webpack React: Conditionally load json config files

I have WebPack React project which I'm testing on my " staging " server. 我有WebPack React项目,正在“ 暂存 ”服务器上进行测试。 Now its time to release it on " production " server. 现在是时候在“ 生产 ”服务器上发布它了。

I'm using server.json file which consists with server info such as api keys, api address, and so on. 我正在使用server.json文件,其中包含服务器信息,例如api密钥,api地址等。

What I want is to use different server.json for "production" and "staging". 我想要的是对“生产”和“登台”使用不同的server.json。 And when I use production-server.json, there would be no traces of staging-server.json in my bundle. 而且,当我使用production-server.json时,我的捆绑包中没有 staging-server.json的痕迹

src
- config
-- config.js
-- production-server.json
-- staging-server.json

maybe something like: yarn build-staging, yarn build-production 大概是这样的:纱线定型,纱线定型

You should use environment variables and webpack's DefinePlugin . 您应该使用环境变量和webpack的DefinePlugin Additionally, you can use node-config to automatically load a json configuration file based on your NODE_ENV . 另外,您可以使用node-config根据NODE_ENV自动加载json配置文件。

package.json 的package.json

"scripts": {
  "build:dev": "NODE_ENV=development start-something",
  "build": "NODE_ENV=production start-something"
}

project config structure 项目配置结构

config
  default.json
    { "api": "https://api.mysite.com/v1" }
  staging.json
    { "api": "http://localhost:8000/v1" }

webpack config webpack配置

// node-config will load your staging.json or default.json file here
// depending on what NODE_ENV is
const config = require('config');

plugins: [
  // inject window.CONFIG into your app
  new webpack.DefinePlugin({
    CONFIG: JSON.stringify(config)
  })
]

Then in your react code you will have access to environment-specific config 然后,在您的react代码中,您将可以访问特定于环境的配置

componentDidMount() {
  // in prod: https://api.mysite.com/v1/user/some-user-id
  // in staging: http://localhost:8000/v1/user/some-user-id
  return axios(`${CONFIG.api}/user/${this.props.userId}`).then(whatever...)
}

If you're on windows use cross-env to set your environment variable. 如果您使用的是Windows,请使用cross-env设置环境变量。

Using node-config isn't the only way to do this, there are several, but I find it pretty easy, unless you're working with electron. 使用node-config不是唯一的方法,有几种方法,但是我发现这很容易,除非您正在使用电子。

edit 编辑

Since node-config uses nodejs it is typically used in front end projects in conjunction with webpack. 由于node-config使用nodejs,因此通常将其与webpack一起用于前端项目。 If you are unable to to integrate it with webpack you don't need to use node-config at all, I would do something like this: 如果您无法将其与webpack集成,则完全不需要使用node-config ,我将执行以下操作:

project structure 项目结构

config
  default.json
  development.json
  test.json
  index.js
src
  ...etc

config files 配置文件

// default.json, typically used for production
{
  "api": "https://api.mysite.com/v1"
}

// development.json
{
  "api": "http://localhost:8000/v1"
}

// index.js
// get process.env via babel-plugin-transform-inline-environment-variables
import production from './default.json';
import development from './development.json';
const { NODE_ENV: env } = process.env;

const config = {
  production,
  development
};

export default config[env];

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

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