Skip to content

Commit

Permalink
support js and json config file with same path
Browse files Browse the repository at this point in the history
  • Loading branch information
mocheng committed Feb 13, 2017
1 parent 193bf29 commit 9019f07
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
9 changes: 9 additions & 0 deletions docs/guides/production.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ into your application. One example of the file `_configs/production/config.json
}
```

If dynamic config is preferred, just replace `config.json` with file `config.js`. One example of `config.js` might look like this:

```js
module.exports = {
APP_ENV: process.env.NODE_ENV === "production" ? "prod" : "dev",
MY_GLOBAL_VARIABLE: process.env.NODE_ENV === "production" ? "foo" : "bar"
}
```

To use these config variables inside your application, just use the `config()` function inside `react-server` and
you're all set! This works reliably on both server and client sides of the application--fully isomorphic!
Here's an example:
Expand Down
17 changes: 7 additions & 10 deletions packages/react-server/core/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* Thin wrapper around the environment-specific configuration file
*/
Expand All @@ -12,17 +11,15 @@ if (SERVER_SIDE) {
if (null === config) {

//eslint-disable-next-line no-process-env
if (process.env.REACT_SERVER_CONFIG_SOURCE) {
if (process.env.REACT_SERVER_CONFIGS) {
var path = require('path');
//eslint-disable-next-line no-process-env
config = require(path.join(process.cwd(), process.env.REACT_SERVER_CONFIG_SOURCE));
}
//eslint-disable-next-line no-process-env
else if (process.env.REACT_SERVER_CONFIGS) {
var fs = require("fs");
//eslint-disable-next-line no-process-env
var configFile = fs.readFileSync(process.env.REACT_SERVER_CONFIGS + "/config.json");
config = Object.freeze(JSON.parse(configFile));
var configFilePath = process.env.REACT_SERVER_CONFIGS;

// Node.js tries to load `config.js` file first. If `config.js` doesn't exist, Node.js
// then try to load `config.json`.
var configFilePath = path.join(process.cwd(), configFilePath + "/config");
config = require(configFilePath);
} else {
config = Object.freeze({});
}
Expand Down

0 comments on commit 9019f07

Please sign in to comment.