-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathbundle.ts
More file actions
149 lines (130 loc) · 4.44 KB
/
bundle.ts
File metadata and controls
149 lines (130 loc) · 4.44 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
import fs from 'fs';
import path from 'path';
import { minify } from 'terser';
import { nth_identifier } from './identifier.js';
const define = {
'process.env.SECURE_ORIGIN': JSON.stringify(process.env.SECURE_ORIGIN ?? 'false'),
// original key, used 2003-2010
'process.env.LOGIN_RSAE': JSON.stringify(process.env.LOGIN_RSAE ?? '58778699976184461502525193738213253649000149147835990136706041084440742975821'),
'process.env.LOGIN_RSAN': JSON.stringify(process.env.LOGIN_RSAN ?? '7162900525229798032761816791230527296329313291232324290237849263501208207972894053929065636522363163621000728841182238772712427862772219676577293600221789'),
'process.env.BUILD_TIME': JSON.stringify(new Date().toISOString())
};
// ----
type BunOutput = {
source: string;
sourcemap: string;
}
async function bunBuild(entry: string, external: string[] = [], minify = true, drop: string[] = []): Promise<BunOutput> {
const build = await Bun.build({
entrypoints: [entry],
sourcemap: 'external',
define,
external,
minify,
drop,
});
if (!build.success) {
build.logs.forEach((x: any) => console.log(x));
process.exit(1);
}
return {
source: await build.outputs[0].text(),
sourcemap: build.outputs[0].sourcemap ? await build.outputs[0].sourcemap.text() : ''
};
}
async function applyTerser(script: BunOutput): Promise<boolean> {
const mini = await minify(script.source, {
sourceMap: {
content: script.sourcemap
},
toplevel: true,
// format: {
// beautify: true
// },
compress: {
ecma: 2020
},
mangle: {
nth_identifier: nth_identifier,
properties: {
reserved: [
// stdlib
'willReadFrequently',
'usedJSHeapSize',
// wasm
// must be callable:
'_abort_js',
'emscripten_resize_heap',
'fd_close',
'fd_seek',
'fd_write',
// must be an object:
'env',
'wasi_snapshot_preview1',
// is not an object:
'instance',
// is not a function:
'emscripten_stack_init',
'emscripten_stack_get_end',
'__wasm_call_ctors',
// imports:
'HEAPU8',
// exports:
'_emscripten_stack_restore',
'_emscripten_stack_alloc',
'emscripten_stack_get_current',
'memory',
'_malloc',
'malloc',
'_free',
'free',
'_realloc',
'realloc',
'__indirect_function_table',
'_tsf_load_memory',
'tsf_load_memory',
'_tsf_close',
'tsf_close',
'_tsf_reset',
'tsf_reset',
'_tsf_set_output',
'tsf_set_output',
'_tsf_channel_set_bank_preset',
'tsf_channel_set_bank_preset',
'_tml_load_memory',
'tml_load_memory',
'_midi_render',
'midi_render',
'setValue',
'getValue',
'calledRun'
]
}
}
});
script.source = mini.code ?? '';
script.sourcemap = mini.map?.toString() ?? '';
return true;
}
// ----
if (!fs.existsSync('out')) {
fs.mkdirSync('out');
}
fs.copyFileSync('src/3rdparty/tinymidipcm/tinymidipcm.wasm', 'out/tinymidipcm.wasm');
const args = process.argv.slice(2);
const prod = args[0] !== 'dev';
const entrypoints = [
'src/client/Client.ts',
'src/mapview/MapView.ts'
];
for (const file of entrypoints) {
const output = path.basename(file).replace('.ts', '.js').toLowerCase();
const script = await bunBuild(file, [], prod, prod ? ['console'] : []);
if (script) {
if (prod) {
await applyTerser(script);
}
fs.writeFileSync(`out/${output}`, script.source);
fs.writeFileSync(`out/${output}.map`, script.sourcemap);
}
}