-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
43 lines (35 loc) · 1.18 KB
/
client.js
File metadata and controls
43 lines (35 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env node
const fs = require("fs");
const { parse } = require("node-html-parser");
const path = require("path");
// @TODO Add a configuration file that allows for setting up and configuring
// environment variable libraries.
require("dotenv").config();
const prefix = "REACT_APP_";
// @TODO Add configuration file for build directory path
const buildDir = path.resolve("build/");
const file = path.join(buildDir, "index.html");
const html = fs.readFileSync(file);
const root = parse(html);
const head = root.querySelector("head");
head.insertAdjacentHTML(
"beforeend",
(function () {
const script = [];
const env = [];
Object.entries(process.env || {})
.filter(([key]) => key.startsWith(prefix))
.forEach(([_key, value]) => {
const key = _key.substr(prefix.length);
script.push(`const ${key} = "${value}";`);
env.push(`"${key}": ${["`${", key, "}`"].join("")}`);
});
script.push(`window["$$client-variables"] = { ${env.join(",")} };`);
return [
'<script id="react-client-variables" type="text/javascript">',
script.join(""),
"</script>",
].join("");
})()
);
fs.writeFileSync(file, root.toString());