Allow backend `process.env` to see the runtime env (#748)

Summary:
This is a follow-up to #746, wherein we exposed our fixed `env` to the
backend applications. We now extend that environment so that it can also
access the user’s runtime environment—i.e., the native values of
`process.env`.

(This is in contrast to the frontend bundles `main.js` and especially
`ssr.js`, where this is not and should not be the case: the environment
must be fixed at build time.)

Test Plan:
Add to the top of `async run()` in `src/cli/commands/load.js`:

```js
    console.log(require("../../app/version").VERSION_SHORT);
    console.log(process.env.AT_RUNTIME);
```

Run `yarn backend` and `AT_RUNTIME=wat node bin/sourcecred.js load`.
Ensure that the version number and the string `wat` are both printed.
(Before this patch, the string `undefined` would be printed instead of
`wat`.)

wchargin-branch: backend-extensible-env
This commit is contained in:
William Chargin 2018-08-31 16:34:18 -07:00 committed by GitHub
parent 8009e20e5b
commit f1a6b37524
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 3 deletions

View File

@ -134,11 +134,14 @@ function getClientEnvironment() {
// Stringify all values so we can feed into Webpack's DefinePlugin.
const stringified = {"process.env": {}};
const individuallyStringified = {};
for (const key of Object.keys(raw)) {
stringified["process.env"][key] = JSON.stringify(raw[key]);
const value = JSON.stringify(raw[key]);
stringified["process.env"][key] = value;
individuallyStringified["process.env." + key] = value;
}
return {raw, stringified};
return {raw, stringified, individuallyStringified};
}
module.exports = getClientEnvironment;

View File

@ -74,5 +74,5 @@ module.exports = (outputPath) => ({
},
],
},
plugins: [new webpack.DefinePlugin(env.stringified)],
plugins: [new webpack.DefinePlugin(env.individuallyStringified)],
});