-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgatsby-node.js
51 lines (42 loc) · 1.38 KB
/
gatsby-node.js
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
44
45
46
47
48
49
50
51
const fs = require("fs")
exports.onCreateWebpackConfig = async (
{ actions, plugins, reporter },
{ envFolderPath = "env/", customEnvFolderPath}
) => {
const activeEnv =
process.env.BUILD_ENV || process.env.NODE_ENV || "development"
const theme = process.env.THEME || null
const basePath = customEnvFolderPath ?? `${process.cwd()}/${envFolderPath}`
async function getEnvs(fileName) {
const envsPromise = require(`${basePath}${fileName}`)
const envs = await Promise.resolve(envsPromise)
return envs
}
const envMessage = `Using ${activeEnv} environment`
const themeMessage = theme ? ` and ${theme} theme` : ``
reporter.info(envMessage + themeMessage)
const envs = await getEnvs(`${activeEnv}.js`)
const themeEnvs = envs[theme]
const commonEnvs = await getEnvs(`index.js`)
const themeCommonEnvs = commonEnvs[theme]
const mergedEnvs = Object.assign(
{},
commonEnvs,
themeCommonEnvs,
envs,
themeEnvs
)
const envKeys = Object.keys(mergedEnvs).reduce((acc, envKey) => {
acc[envKey] = JSON.stringify(mergedEnvs[envKey])
return acc
}, {})
fs.writeFileSync(
`${__dirname}/cachedVariables.json`,
JSON.stringify(mergedEnvs)
)
reporter.success("created cachedVariables.json")
actions.setWebpackConfig({
plugins: [plugins.define(envKeys)]
})
reporter.success("making environment variables available as globals")
}