generated from qq15725/starter-ts
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrollup.config.mjs
85 lines (81 loc) · 2.12 KB
/
rollup.config.mjs
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
// @ts-check
import { defineConfig } from 'rollup'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import copy from 'rollup-plugin-copy'
import dts from 'rollup-plugin-dts'
import esbuild from 'rollup-plugin-esbuild'
import fs from 'fs-extra'
const entries = [
'src/index.ts',
]
export default defineConfig([
{
input: entries,
external: [
'onnxruntime-web',
],
output: {
dir: 'dist',
format: 'esm',
entryFileNames: '[name].mjs',
chunkFileNames: (f) => {
return 'chunks/[name].mjs'
},
},
plugins: [
esbuild(),
nodeResolve(),
commonjs(),
wasmPlugin(),
onnxPlugin(),
],
},
{
input: entries,
output: {
dir: 'dist',
format: 'esm',
chunkFileNames: 'types/[name].d.mts',
entryFileNames: f => `${f.name.replace(/src[\\\/]/, '')}.d.mts`,
},
plugins: [
dts({
respectExternal: true,
}),
copy({
targets: [
{ src: './models/u2netp.onnx', dest: 'dist' },
{ src: './node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.wasm', dest: 'dist' },
{ src: './node_modules/onnxruntime-web/dist/ort-wasm-simd.wasm', dest: 'dist' },
{ src: './node_modules/onnxruntime-web/dist/ort-wasm-threaded.wasm', dest: 'dist' },
{ src: './node_modules/onnxruntime-web/dist/ort-wasm.wasm', dest: 'dist' },
],
}),
],
},
])
export function wasmPlugin() {
return {
name: 'wasm',
async load(id) {
if (!id.endsWith('.wasm'))
return
const binary = await fs.readFile(id)
const base64 = binary.toString('base64')
return `export default Uint8Array.from(atob(${JSON.stringify(base64)}), c => c.charCodeAt(0))`
},
}
}
export function onnxPlugin() {
return {
name: 'onnx',
async load(id) {
if (!id.endsWith('.onnx'))
return
const binary = await fs.readFile(id)
const base64 = binary.toString('base64')
return `export default Uint8Array.from(atob(${JSON.stringify(base64)}), c => c.charCodeAt(0))`
},
}
}