-
Notifications
You must be signed in to change notification settings - Fork 2
/
rollup.config.js
75 lines (61 loc) · 1.72 KB
/
rollup.config.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
import {readFileSync} from 'node:fs'
import path from 'node:path'
import {gzipSync} from 'node:zlib'
import typescript from '@rollup/plugin-typescript'
import {defineConfig} from 'rollup'
const SI_WASM = '\0wasm'
const R_WASM = /\.wasm(\?gzip)?$/
export default defineConfig({
input: ['src/main.ts', 'src/gzipped.ts'],
output: {
dir: 'dist',
format: 'module',
chunkFileNames: '[name].js'
},
plugins: [
typescript({
sourceMap: true,
tsconfig: 'tsconfig.rollup.json'
}),
// simple plugin to load wasm binary
{
name: 'wasm',
resolveId(si_specifier, p_importer) {
if (R_WASM.test(si_specifier)) {
// @ts-expect-error bc rollup plugin typescript is broken?!
return SI_WASM + path.resolve(path.dirname(p_importer), si_specifier)
}
return null
},
load(p_file) {
// ignore non-wasm files
if (!R_WASM.test(p_file)) return null
// prep export contents
let sb64_contents
p_file = p_file.slice(SI_WASM.length)
if (p_file.endsWith('?gzip')) {
// compressed
p_file = p_file.replace(/\?gzip$/, '')
const gzipped = gzipSync(readFileSync(p_file))
sb64_contents = btoa(
Array.from(gzipped)
.map(xb => String.fromCharCode(xb))
.join('')
)
} else {
// raw
// load wasm as base64-encoded string
sb64_contents = readFileSync(p_file, 'base64')
}
// create module for rollup
const sx_out = `
const sb64_data = ${JSON.stringify(sb64_contents)};
export default sb64_data;
`
return {
code: sx_out
}
}
}
]
})