-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbuild.ts
More file actions
79 lines (75 loc) · 3.45 KB
/
build.ts
File metadata and controls
79 lines (75 loc) · 3.45 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
import child from 'node:child_process';
import path from 'node:path';
import zlib from 'node:zlib';
import { encode } from 'base16384';
import esbuild from 'esbuild';
import { chunk } from 'lodash';
import { fs, Logger } from '@hydrooj/utils';
const logger = new Logger('build');
logger.info('Building...');
function encodeBinary(a: Buffer) {
const file = zlib.gzipSync(a, { level: 9 });
return chunk([...encode(file)], 1000).map((i) => String.fromCodePoint(...i)).join('');
}
function size(a: string) {
return `${Math.floor((Buffer.from(a).length / 1024 / 1024) * 10) / 10}MB`;
}
const nopMap = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIiJdLCJtYXBwaW5ncyI6IkEifQ==';
(async () => {
fs.ensureDirSync(path.resolve(process.cwd(), 'dist'));
const res = await esbuild.build({
platform: 'node',
bundle: true,
outdir: path.join(process.cwd(), 'dist'),
splitting: false,
write: false,
target: 'node16',
tsconfig: path.resolve(process.cwd(), 'tsconfig.json'),
minify: !process.argv.includes('--debug'),
entryPoints: [path.resolve(process.cwd(), 'packages/server/index.ts')],
charset: 'utf8',
sourcemap: process.argv.includes('--debug') ? 'inline' : false,
metafile: true,
external: ['mongodb', 'bson', 'moment-timezone', 'moment'],
plugins: [{
name: 'base16384',
setup(b) {
b.onLoad({ filter: /\.(frontend|ttf|wasm|sh)$/, namespace: 'file' }, (t) => {
const contents = `module.exports = ${process.argv.includes('--use-readfile')
? `require('fs').readFileSync('${t.path}')`
: `"${encodeBinary(fs.readFileSync(t.path))}"`};\n${nopMap}`;
console.log(t.path, size(contents));
return {
contents,
loader: 'tsx',
};
});
b.onLoad({ filter: /node_modules.*\.[jt]sx?$/ }, async (t) => ({
contents: `${await fs.readFile(t.path, 'utf-8')}\n${nopMap}`,
loader: 'default',
}));
},
}],
alias: {
ws: `${path.dirname(require.resolve('ws/package.json'))}/index.js`,
saslprep: path.resolve(__dirname, 'saslprep.js'),
},
});
if (res.errors.length) console.error(res.errors);
if (res.warnings.length) console.warn(res.warnings);
logger.info(`Resource Size: ${size(res.outputFiles[0].text)}`);
fs.writeFileSync(path.resolve(process.cwd(), 'dist/xcpc-tools.js'), res.outputFiles[0].text);
fs.writeFileSync(path.resolve(process.cwd(), 'dist/metafile.json'), JSON.stringify(res.metafile));
logger.info('Saved to dist/xcpc-tools.js');
if (!process.env.SEA) return;
fs.writeFileSync(path.resolve(process.cwd(), 'dist/sea-config.json'), JSON.stringify({
main: 'xcpc-tools.js',
output: 'sea-prep.blob',
}));
child.execSync('node --experimental-sea-config sea-config.json', { cwd: path.resolve(process.cwd(), 'dist') });
fs.copyFileSync(path.resolve(process.cwd(), 'nanode-v22.x-icu_none-v8_opts-lto-x64'), path.resolve(process.cwd(), 'dist/nanode'));
child.execSync(
'npx postject nanode NODE_SEA_BLOB sea-prep.blob --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2',
{ cwd: path.resolve(process.cwd(), 'dist') },
);
})();