Skip to content

Commit 5e2d1f6

Browse files
committed
🌱
0 parents  commit 5e2d1f6

File tree

6 files changed

+120
-0
lines changed

6 files changed

+120
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Heroku JS Runtime Env
2+
=====================
3+
Make single-page javascript apps (SPA) honor runtime environment variables.
4+
5+
Normally javascript apps are compiled into a bundle before being deployed. During this compile phase, environment variables may be embedded in the javascript bundle, such as with [Webpack DefinePlugin](https://webpack.github.io/docs/list-of-plugins.html#defineplugin). If you're hosting on a [12-factor](https://12factor.net) platform like [Heroku](https://www.heroku.com), these embedded values may go stale when setting new [config vars](https://devcenter.heroku.com/articles/config-vars), promoting through a [pipeline](https://devcenter.heroku.com/articles/pipelines), or [rolling back](https://devcenter.heroku.com/articles/releases#rollback) to a previous release.
6+
7+
In coordination with the build process, this tiny module provides a mechanism to inject runtime environment variables into a javascript bundle without recompiling.
8+
9+
Usage
10+
-----
11+
12+
🚧 🚧 🚧

index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Capture environment as module variable to allow testing.
2+
var originalEnv = process.env;
3+
// This Mustache template tag should be rendered/replaced with the environment in production.
4+
var templatedEnv = '{{REACT_APP_VARS_AS_JSON}}';
5+
6+
7+
// A function returning the runtime environment, so that
8+
// JSON parsing & errors occur at runtime instead of load time.
9+
function runtimeEnv() {
10+
var env;
11+
12+
if (originalEnv.NODE_ENV === 'production') {
13+
try {
14+
env = JSON.parse(templatedEnv);
15+
} catch(error) {
16+
throw new Error(
17+
'Runtime env vars could not be parsed. '+
18+
'Content of `REACT_APP_VARS_AS_JSON` is `'+templatedEnv+'` '+
19+
'Ensure `node_modules/@mars/heroku-js-runtime-env/index.js` '+
20+
'is rendered as a Mustache template.'
21+
);
22+
}
23+
24+
} else {
25+
env = originalEnv;
26+
}
27+
28+
return env;
29+
}
30+
31+
module.exports = runtimeEnv;

package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "@mars/heroku-js-runtime-env",
3+
"version": "1.0.0",
4+
"description": "Runtime env var support for static javascript apps on Heroku",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/mars/heroku-js-runtime-env.git"
12+
},
13+
"keywords": [
14+
"heroku",
15+
"environment",
16+
"variable",
17+
"env",
18+
"var",
19+
"config",
20+
"runtime",
21+
"javascript"
22+
],
23+
"author": "Mars Hall",
24+
"license": "MIT",
25+
"bugs": {
26+
"url": "https://github.com/mars/heroku-js-runtime-env/issues"
27+
},
28+
"homepage": "https://github.com/mars/heroku-js-runtime-env#readme",
29+
"devDependencies": {
30+
"mocha": "^3.1.2",
31+
"rewire": "^2.5.2",
32+
"should": "^11.1.1"
33+
}
34+
}

test/development-test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var rewire = require('rewire')
2+
var should = require('should')
3+
4+
describe('in development', function() {
5+
var runtimeEnv
6+
7+
beforeEach(function() {
8+
runtimeEnv = rewire('../')
9+
runtimeEnv.__set__('originalEnv', { NODE_ENV: 'development'})
10+
});
11+
12+
it('contains value of `process.env`', function() {
13+
runtimeEnv().should.be.an.instanceOf(Object).and.have.property('NODE_ENV')
14+
})
15+
16+
})

test/production-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var rewire = require('rewire')
2+
var should = require('should')
3+
4+
describe('in production', function() {
5+
var runtimeEnv
6+
7+
beforeEach(function() {
8+
runtimeEnv = rewire('../')
9+
runtimeEnv.__set__('originalEnv', { NODE_ENV: 'production'})
10+
})
11+
12+
it('throws error when Mustache tag is not rendered', function() {
13+
runtimeEnv.should.throw(/Runtime env vars could not be parsed/)
14+
})
15+
16+
describe('with Mustache template replacements', function() {
17+
beforeEach(function() {
18+
runtimeEnv.__set__('templatedEnv', '{ "REACT_APP_USER": "🦄"}')
19+
})
20+
21+
it('contains value of the runtime environment', function() {
22+
runtimeEnv().should.be.an.instanceOf(Object).and.have.property('REACT_APP_USER', '🦄')
23+
})
24+
})
25+
26+
})

0 commit comments

Comments
 (0)