1
1
/* eslint-disable import/no-dynamic-require */
2
2
import path from 'path' ;
3
+ import zlib from 'zlib' ;
4
+ import { encode } from 'base16384' ;
3
5
import esbuild from 'esbuild' ;
6
+ import { chunk } from 'lodash' ;
4
7
import { fs , Logger } from '@hydrooj/utils' ;
5
8
6
9
const logger = new Logger ( 'build' ) ;
7
10
logger . info ( 'Building...' ) ;
11
+
12
+ function encodeBinary ( a : Buffer ) {
13
+ const file = zlib . gzipSync ( a ) ;
14
+ return chunk ( [ ...encode ( file ) ] , 1000 ) . map ( ( i ) => String . fromCodePoint ( ...i ) ) . join ( '' ) ;
15
+ }
16
+
17
+ const nopMap = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIiJdLCJtYXBwaW5ncyI6IkEifQ==' ;
18
+
8
19
( async ( ) => {
9
20
fs . ensureDirSync ( path . resolve ( process . cwd ( ) , 'dist' ) ) ;
10
21
const res = await esbuild . build ( {
@@ -14,15 +25,26 @@ logger.info('Building...');
14
25
splitting : false ,
15
26
write : false ,
16
27
tsconfig : path . resolve ( process . cwd ( ) , 'tsconfig.json' ) ,
17
- minify : false ,
28
+ minify : true ,
18
29
entryPoints : [ path . resolve ( process . cwd ( ) , 'packages/server/index.ts' ) ] ,
19
30
charset : 'utf8' ,
20
31
sourcemap : 'inline' ,
21
- loader : {
22
- '.frontend' : 'base64' ,
23
- '.ttf' : 'base64' ,
24
- '.wasm' : 'base64' ,
25
- } ,
32
+ plugins : [ {
33
+ name : 'base16384' ,
34
+ setup ( b ) {
35
+ b . onLoad ( { filter : / \. ( f r o n t e n d | t t f | w a s m ) $ / , namespace : 'file' } , ( t ) => {
36
+ const file = fs . readFileSync ( path . join ( t . path ) ) ;
37
+ return {
38
+ contents : `module.exports = "${ encodeBinary ( file ) } ";\n${ nopMap } ` ,
39
+ loader : 'tsx' ,
40
+ } ;
41
+ } ) ;
42
+ b . onLoad ( { filter : / n o d e _ m o d u l e s \/ .+ \. j s $ / } , ( t ) => ( {
43
+ contents : `${ fs . readFileSync ( t . path , 'utf8' ) } \n${ nopMap } ` ,
44
+ loader : 'default' ,
45
+ } ) ) ;
46
+ } ,
47
+ } ] ,
26
48
alias : {
27
49
ws : `${ path . dirname ( require . resolve ( 'ws/package.json' ) ) } /index.js` ,
28
50
saslprep : path . resolve ( __dirname , 'saslprep.js' ) ,
0 commit comments