generated from bazel-contrib/rules-template
-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
launcher.js
executable file
·137 lines (123 loc) · 3.33 KB
/
launcher.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const { readFileSync, writeFileSync } = require('fs')
const { pathToFileURL } = require('url')
const { join } = require('path')
const esbuild = require('esbuild')
function getFlag(flag, required = true) {
const argvFlag = process.argv.find((arg) => arg.startsWith(`${flag}=`))
if (!argvFlag) {
if (required) {
console.error(`Expected flag '${flag}' passed to launcher, but not found`)
process.exit(1)
}
return
}
return argvFlag.split('=')[1]
}
function getEsbuildArgs(paramsFilePath) {
try {
return JSON.parse(readFileSync(paramsFilePath, { encoding: 'utf8' }))
} catch (e) {
console.error('Error while reading esbuild flags param file', e)
process.exit(1)
}
}
async function processConfigFile(configFilePath, existingArgs = {}) {
const fullConfigFileUrl = pathToFileURL(join(process.cwd(), configFilePath))
let config
try {
config = await import(fullConfigFileUrl)
} catch (e) {
console.error(
`Error while loading configuration '${fullConfigFileUrl}':\n`,
e
)
process.exit(1)
}
if (!config.default) {
console.error(
`Config file '${configFilePath}' was loaded, but did not export a configuration object as default`
)
process.exit(1)
}
config = config.default
// These keys of the config can not be overriden
const IGNORED_CONFIG_KEYS = [
'bundle',
'entryPoints',
'external',
'metafile',
'outdir',
'outfile',
'preserveSymlinks',
'sourcemap',
'splitting',
'tsconfig',
]
const MERGE_CONFIG_KEYS = ['define']
return Object.entries(config).reduce((prev, [key, value]) => {
if (value === null || value === void 0) {
return prev
}
if (IGNORED_CONFIG_KEYS.includes(key)) {
console.error(
`[WARNING] esbuild configuration property '${key}' from '${configFilePath}' will be ignored and overriden`
)
} else if (
MERGE_CONFIG_KEYS.includes(key) &&
existingArgs.hasOwnProperty(key)
) {
// values from the rule override the config file
// perform a naive merge
if (Array.isArray(value)) {
prev[key] = [...value, ...existingArgs[key]]
} else if (typeof value === 'object') {
prev[key] = {
...value,
...existingArgs[key],
}
} else {
// can't merge
console.error(
`[WARNING] esbuild configuration property '${key}' from '${configFilePath}' could not be merged`
)
}
} else {
prev[key] = value
}
return prev
}, {})
}
if (!process.env.ESBUILD_BINARY_PATH) {
console.error('Expected environment variable ESBUILD_BINARY_PATH to be set')
process.exit(1)
}
async function runOneBuild(args, userArgsFilePath, configFilePath) {
if (userArgsFilePath) {
args = {
...args,
...getEsbuildArgs(userArgsFilePath),
}
}
if (configFilePath) {
const config = await processConfigFile(configFilePath, args)
args = {
...args,
...config,
}
}
try {
const result = await esbuild.build(args)
if (result.metafile) {
const metafile = getFlag('--metafile');
writeFileSync(metafile, JSON.stringify(result.metafile));
}
} catch (e) {
console.error(e)
process.exit(1)
}
}
runOneBuild(
getEsbuildArgs(getFlag('--esbuild_args')),
getFlag('--user_args', false),
getFlag('--config_file', false)
)