-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvite.config.ts
More file actions
125 lines (115 loc) · 3.91 KB
/
Copy pathvite.config.ts
File metadata and controls
125 lines (115 loc) · 3.91 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
import { defineConfig } from 'vite';
import { crx } from '@crxjs/vite-plugin';
import manifest from './manifest.json';
import { resolve } from 'path';
import fs from 'fs';
/**
* Chrome extension offscreen docs are not cross-origin isolated, so ONNX Runtime
* must use the asyncify (single-threaded) WASM backend — not the threaded build.
*/
const ONNX_WASM_FILES = [
'ort-wasm-simd-threaded.asyncify.wasm',
'ort-wasm-simd-threaded.asyncify.mjs',
] as const;
function wasmPlugin() {
return {
name: 'wasm-plugin',
configureServer(server: any) {
server.middlewares.use('/wasm', (req: any, res: any, next: any) => {
if (req.url) {
const fileName = req.url.split('?')[0].replace(/^\//, '');
if (!ONNX_WASM_FILES.includes(fileName as (typeof ONNX_WASM_FILES)[number])) {
next();
return;
}
const filePath = resolve(__dirname, 'node_modules/onnxruntime-web/dist', fileName);
if (fs.existsSync(filePath)) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Cross-Origin-Resource-Policy', 'cross-origin');
if (filePath.endsWith('.wasm')) res.setHeader('Content-Type', 'application/wasm');
if (filePath.endsWith('.mjs')) res.setHeader('Content-Type', 'application/javascript');
res.end(fs.readFileSync(filePath));
return;
}
}
next();
});
},
writeBundle() {
const srcDir = resolve(__dirname, 'node_modules/onnxruntime-web/dist');
const destDir = resolve(__dirname, 'dist/wasm');
if (fs.existsSync(destDir)) fs.rmSync(destDir, { recursive: true, force: true });
fs.mkdirSync(destDir, { recursive: true });
if (fs.existsSync(srcDir)) {
for (const file of ONNX_WASM_FILES) {
const src = resolve(srcDir, file);
if (fs.existsSync(src)) {
fs.copyFileSync(src, resolve(destDir, file));
}
}
}
slimDistBundle(resolve(__dirname, 'dist'));
},
closeBundle() {
slimDistBundle(resolve(__dirname, 'dist'));
},
};
}
function slimDistBundle(distDir: string) {
const assetsDir = resolve(distDir, 'assets');
if (fs.existsSync(assetsDir)) {
for (const file of fs.readdirSync(assetsDir)) {
const fullPath = resolve(assetsDir, file);
if (!fs.statSync(fullPath).isFile()) continue;
if (file.includes('ort-wasm') && (file.endsWith('.wasm') || file.endsWith('.mjs'))) {
fs.unlinkSync(fullPath);
}
if (/^arcrawls-gif\d*\.gif$/i.test(file)) {
fs.unlinkSync(fullPath);
}
}
}
// Remove duplicate macOS Finder copy files across dist (e.g., " 2", " 3", " 5")
const removeDuplicates = (dir: string) => {
if (!fs.existsSync(dir)) return;
for (const item of fs.readdirSync(dir)) {
const fullPath = resolve(dir, item);
if (/\s\d+/i.test(item) || item.toLowerCase().includes('copy')) {
fs.rmSync(fullPath, { recursive: true, force: true });
continue;
}
if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
removeDuplicates(fullPath);
}
}
};
removeDuplicates(distDir);
}
export default defineConfig({
plugins: [
crx({ manifest }),
wasmPlugin(),
],
build: {
emptyOutDir: true,
modulePreload: false,
assetsInlineLimit: 0,
chunkSizeWarningLimit: 1000,
rollupOptions: {
input: {
main_world: resolve(__dirname, 'src/content/main_world.ts'),
offscreen: resolve(__dirname, 'src/offscreen/offscreen.html'),
onboarding: resolve(__dirname, 'onboarding/onboarding.html'),
},
output: {
entryFileNames: (chunkInfo) => {
if (chunkInfo.name === 'main_world') {
return 'main_world.js';
}
return 'assets/[name]-[hash].js';
},
},
external: ['fs', 'path', 'url', 'sharp', 'onnxruntime-node'],
},
},
});