forked from rianadon/Cosmos-Keyboards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
137 lines (126 loc) · 4.4 KB
/
vite.config.ts
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
126
127
128
129
130
131
132
133
134
135
136
137
import { sveltekit } from '@sveltejs/kit/vite'
import { existsSync, readFileSync, writeFileSync } from 'fs'
import { basename, dirname, resolve } from 'path'
import UnoCSS from 'unocss/vite'
import { defineConfig, type PluginOption } from 'vite'
const proDir = resolve(__dirname, './src/lib/worker/pro')
const proPatchDir = resolve(__dirname, './src/lib/worker/pro-patch')
const hasPro = existsSync(proDir)
export default defineConfig({
resolve: {
alias: {
'$assets': resolve(__dirname, './src/assets'),
'$target': resolve(__dirname, './target'),
'@pro': hasPro ? proDir : proPatchDir,
},
},
plugins: [
removeFS(),
artoolkit(),
opencv(),
UnoCSS(),
sveltekit(),
mediapipe({
'hands.js': ['Hands', 'VERSION'],
}),
],
ssr: {
noExternal: ['three', '@popperjs/core', '@ar-js-org/artoolkit5-js', '@hpmason/opencv-contrib-wasm'],
},
server: {
watch: {
followSymlinks: false,
ignored: ['.git', 'node_modules', 'venv'],
},
fs: { allow: ['.'] },
proxy: {
'/blog': 'http://localhost:8000/cosmos',
'/docs': 'http://localhost:8000/cosmos',
'/assets': 'http://localhost:8000/cosmos',
'/stylesheets': 'http://localhost:8000/cosmos',
'/javascripts': 'http://localhost:8000/cosmos',
'/livereload': 'http://localhost:8000',
'/search': 'http://localhost:8000/cosmos',
},
},
})
/**
* Add exports to mediapipe.
* Simplified from the vite-plugin-mediapipe npm repo.
*/
function mediapipe(config: Record<string, string[]>): PluginOption {
return {
name: 'mediapipe',
load(id: string) {
const fileName = basename(id)
if (!(fileName in config)) return null
let code = readFileSync(id, 'utf-8')
for (const name of config[fileName]) {
code += `exports.${name} = ${name};`
}
return { code }
},
}
}
// Remove unnecessary references to window in js ar toolkit
// And transform it to es6 exports
function artoolkit(): PluginOption {
return {
name: 'artoolkit',
load(id: string) {
const fileName = basename(id)
if (fileName != 'ARToolkit.js') return null
let code = readFileSync(id, 'utf-8')
code = code.replace('window.FormData', '{}')
code = 'const exports={};' + code + ';export default exports'
return { code }
},
}
}
// Deal with that pesky wasm thing
function opencv(): PluginOption {
return {
name: 'opencv',
load(id: string) {
if (!id.includes('@hpmason/opencv-contrib-wasm')) return null
const fileName = basename(id)
let code = readFileSync(id, 'utf-8')
if (fileName == 'index.js') {
code = code.replace('module.exports = ', 'import cv from "./opencv.js"; const exp =')
code = code.replace("require('./opencv.js')", 'cv')
code += '; export { cv }; export const cvTranslateError = exp.cvTranslateError'
return { code }
} else if (fileName == 'opencv.js') {
code = code.replace('__dirname', '""')
code = code.replace(/require\(/g, 'console.error(')
code = code.replace('ENVIRONMENT_HAS_NODE && !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_WORKER', 'false')
code = code.replace('typeof window === "object"', 'true')
// code = code.replace(/document.currentScript.src/g, 'wasmPath')
code = code.replace(/document\.currentScript/g, 'false')
const binary = readFileSync(dirname(id) + '/opencv.wasm', { encoding: 'base64' })
// code = code.replace('Module = {}', `Module = { wasmBinary: "data:application/octet-stream;base64,${binary}" }`)
code = code.replace('./opencv.wasm', 'data:application/octet-stream;base64,' + binary)
code = code.replace('this', '{}')
// code = code.replace('locateFile(wasmBinaryFile)', 'wasmPath')
// code = "import wasmPath from './opencv.wasm?url';" + code
// code = 'const exports = {};' + code + 'export const cv = exports.cv'
writeFileSync('src/lib/opencv-contrib.js', code)
return { code }
}
return null
},
}
}
/* Remove any fs imports so vite doesn't try anything smart. */
function removeFS(): PluginOption {
return {
name: 'remove fs import',
async transform(code, _id) {
if (code.includes('process.env.FS')) {
return code.replaceAll('import(process.env.FS!)', 'Promise.resolve(false)').replaceAll('!!import.meta.env', 'true')
}
return undefined
},
enforce: 'pre',
}
}