This repository has been archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 216
/
build.config.js
117 lines (92 loc) · 2.58 KB
/
build.config.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* BUILD MODE (prod / dev)
* output: env.PROD_MODE
*/
const isDevelopment = () =>
process.env.NODE_ENV === 'development';
const isProduction = () => !isDevelopment()
const prodModeEntry = isProduction()
// ------
/**
* SENTRY KEY
* defined by `circle.yml`
*/
const getSentryKey = () =>
process.env.SENTRY_KEY;
const sentryKeyEntry = getSentryKey()
// ------
/**
* TARGET BUILD (these configs will turn features on/off, and produce different manifests)
* CROSS-BROWSER COMPATIBILITY (and other builds)
* We use different build configurations depending on browser (or other builds, like canary).
* For example, browsers have different support for properties on manifest.json
* We also inject environment variables into the code, to toggle feature support.
* [see src/build.ts]
*/
// versions we produce
const BUILD = {
FIREFOX: {
GTM_ENABLED: false, // google tag manager
SENTRY_ENABLED: false, // remote error logging service
},
CHROME: {
GTM_ENABLED: true,
SENTRY_ENABLED: true,
},
CANARY: {
GTM_ENABLED: false,
SENTRY_ENABLED: true,
},
}
// grab target build parameter (passed as command arg)
const getBuildRequestRaw = () =>
process.env.BUILD || ''
// target BUILD parameter is case insensitive (default chrome)
const getTargetBuildName = () =>
Object.keys(BUILD)
.find(buildName => buildName == getBuildRequestRaw().toUpperCase())
|| 'CHROME'
const targetEntry = getTargetBuildName()
const buildEntries = BUILD[getTargetBuildName()]
// ------
/**
* ENTRIES (environment values for build)
*/
/**
* This is a map of strings to primitives
* representing the environment variables for the build.
*/
const entries = {
PROD_MODE: prodModeEntry,
TARGET: targetEntry,
SENTRY_KEY: sentryKeyEntry,
...buildEntries,
};
const getEntries = () => entries
// ------
/**
* MANIFEST FILES (these are merged together to generate final `manifest.json`
*/
const getManifestFiles = () =>
[
// base manifest file
'manifest/base.manifest.json',
// each build can extend the base manifest with a file of this form
`manifest/${getTargetBuildName().toLowerCase()}.manifest.json`,
]
// ------
/**
* utils
*/
// general util for stringifying all values in an object.
// this is used to inject env into code via DefinePlugin in webpack.config.js
const stringifyValues = (obj) =>
Object.keys(obj)
.reduce((out, k) =>
Object.assign({}, out, { [k]: JSON.stringify(obj[k]) }), {})
// ------
module.exports = {
entries: getEntries,
manifestFiles: getManifestFiles,
stringifyValues: stringifyValues,
}