-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbuild.js
More file actions
240 lines (218 loc) · 5.25 KB
/
build.js
File metadata and controls
240 lines (218 loc) · 5.25 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/bin/env node
import {
appendFile,
readdir,
readFile,
writeFile,
} from 'fs/promises';
import {exec as child_process_exec} from 'child_process';
import cssnano from 'cssnano';
import lui_ssr from 'lui-ssr';
const GCC_COMMAND = './node_modules/.bin/google-closure-compiler --';
const prod = !!process.env.CI;
const {version} = JSON.parse(
await readFile('./package.json', 'utf8')
);
let languages = ['en'];
if (prod) {
const files = await readdir('./locales');
languages = (
files
.filter(name => name.endsWith('.csv'))
.map(name => name.split('.')[0])
);
}
const exec = cmd => (
new Promise(resolve =>
child_process_exec(
cmd,
(...args) => resolve(args)
)
)
);
async function file_replace(path, replacements, charset = 'utf8') {
let content = await readFile(path, charset);
for (const [from, to] of replacements) content = content.replaceAll(from, to);
await writeFile(path, content, charset);
}
async function lang_generate(lang) {
const csv = await readFile(`./locales/${lang}.csv`, 'utf8');
let output = '// generated by build.js, for changes go to locales directory\n\n';
for (const line of csv.split('\n')) {
if (!line) continue;
const index_colon = line.indexOf(': ');
const key = line.substring(0, index_colon);
const value = line.substring(index_colon + 2);
output += `export const locale_${key} = '${value}';\n`;
}
await writeFile(
'./src/etc/locale.js',
output,
'utf8'
);
}
const env_set = async (version, debug, lang, api, api_data) => Promise.all([
writeFile(
'./src/etc/env.js',
`export const VERSION = '${version}';
export const DEBUG = ${debug};
export const LANG = '${lang}';
export const API = '${api}';
export const API_DATA = '${api_data}';
`,
'utf8'
),
lang_generate(lang),
]);
async function build_css() {
const css_raw = await readFile('./src/app.css', 'utf8');
console.log('css...');
const css_minified = '' + await (
cssnano()
.process(
css_raw,
{from: undefined}
)
);
console.log('css done.');
await writeFile(
'./dist/app.css',
css_minified,
'ascii'
);
}
const promises = [];
async function build_js(lang) {
await env_set(
prod ? version : 'dev',
false,
lang,
prod ? '/api/minicraft/' : '//l3p3.de/api/minicraft/',
prod ? '/static/minicraft/' : '//l3p3.de/static/minicraft/'
);
const TMP_FILE = `/tmp/app-${lang}.js`;
console.log(lang + ' js pass 1...');
const gcc_output = (await exec(
GCC_COMMAND +
[
'assume_function_wrapper',
'compilation_level ADVANCED',
'dependency_mode PRUNE',
'entry_point ./src/app.js',
'js ./src',
'js_output_file ' + TMP_FILE,
'create_source_map ' + TMP_FILE + '.map',
'language_in ECMASCRIPT_NEXT',
'language_out ECMASCRIPT6_STRICT',
'module_resolution BROWSER',
'rewrite_polyfills false',
'strict_mode_input',
'use_types_for_optimization',
'warning_level VERBOSE',
]
.join(' --')
))[2].trim();
if (gcc_output) console.log(gcc_output);
console.log(lang + ' js pass 1 done.');
promises.push(
(async () => {
console.log(lang + ' js pass 2...');
await appendFile(
TMP_FILE,
`\n//# sourceMappingURL=app-${lang}.js.map\n`,
'ascii'
);
const gcc_output = (await exec(
GCC_COMMAND +
[
'assume_function_wrapper',
'compilation_level SIMPLE',
'externs ./src/externs.js',
'js ' + TMP_FILE,
`js_output_file ./dist/app-${lang}.js`,
`create_source_map ./dist/app-${lang}.js.map`,
'language_in ECMASCRIPT6_STRICT',
'language_out ECMASCRIPT6_STRICT',
'rewrite_polyfills false',
'strict_mode_input',
'warning_level VERBOSE',
]
.join(' --')
))[2].trim();
if (gcc_output) console.log(gcc_output);
await Promise.all([
appendFile(
`./dist/app-${lang}.js`,
`\n//# sourceMappingURL=app-${lang}.js.map\n`,
'ascii'
),
file_replace(
`./dist/app-${lang}.js.map`,
[
[ // closure compiler internals
'/tmp/src/com/google/',
'//raw.githubusercontent.com/google/closure-compiler/refs/heads/master/src/com/google/'
],
[ // all other paths are mine
'/tmp/',
(
prod
? `../minicraft@${process.env.GITHUB_SHA}/`
: '../'
),
],
]
),
exec(`rm ${TMP_FILE} ${TMP_FILE}.map`),
]);
console.log(lang + ' js pass 2 done.');
})(),
(async () => {
console.log(lang + ' ssr html...');
await writeFile(
`./dist/app-${lang}-ssr.html`,
lui_ssr(
await readFile(TMP_FILE, 'ascii')
)(),
'utf8'
);
console.log(lang + ' ssr html done.');
})()
);
}
if(
!(
await exec(GCC_COMMAND + 'version')
)[1].includes('Version: v202')
) {
throw new Error('google closure compiler required!');
}
await exec('rm ./dist/app*');
console.log(`build ${version} (${prod ? 'production' : 'dev'})...`);
try {
await file_replace(
'./src/etc/lui.js',
[["@type {typeof import('lui/index')}", "@type {Object}"]]
);
for (const lang of languages) await build_js(lang);
promises.push(build_css());
await Promise.all(promises);
}
finally {
if (!prod) {
await Promise.all([
env_set(
'dev',
true,
'en',
'//l3p3.de/api/minicraft/',
'//l3p3.de/static/minicraft/'
),
file_replace(
'./src/etc/lui.js',
[["@type {Object}", "@type {typeof import('lui/index')}"]]
),
]);
}
}
console.log('done.');