-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
91 lines (89 loc) · 2.78 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
import { defineConfig, ResolvedConfig, type Plugin } from 'vite'
import { reactVirtualized } from './plugins/reactVirtualized.ts'
import * as path from 'node:path'
import react from '@vitejs/plugin-react-swc'
const scratchGuiPlugin = (): Plugin => {
let resolvedConfig!: ResolvedConfig
return {
name: 'vite-plugin-scratch',
async configResolved(config) {
resolvedConfig = config
},
async resolveId(source, importer, options) {
if (!importer) {
return
}
if (source.startsWith('!arraybuffer-loader!')) {
const resolved = path.join(importer, '..', source.replace(/^!arraybuffer-loader!/, ''))
return `arraybuffer:${encodeURIComponent(resolved)}`
} else if (source.startsWith('!raw-loader!')) {
const resolved = path.join(importer, '..', source.replace(/^!raw-loader!/, ''))
return `raw:${encodeURIComponent(resolved)}`
} else if (source.startsWith('!base64-loader!')) {
const resolved = path.join(importer, '..', source.replace(/^!base64-loader!/, ''))
return `base64:${encodeURIComponent(resolved)}`
}
},
async load(id, options) {
const [prefix, b64] = id.split(':')
if (!(prefix === 'arraybuffer' || prefix === 'raw' || prefix === 'base64')) {
return
}
const resolvedPath = decodeURIComponent(b64).replace(/\?.*$/, '')
const targetUrl = `/${path.relative(resolvedConfig.root, resolvedPath).replace(/\\/g, '/')}`
if (prefix === 'arraybuffer') {
return {
code: `import url from '${targetUrl}'\nexport default await fetch(url).then(res => res.arrayBuffer())`
}
} else if (prefix === 'raw') {
return {
code: `import url from '${targetUrl}'
export default await fetch(url).then(res => res.text())`
}
} else if (prefix === 'base64') {
return {
code: `import url from '${targetUrl}'
const blob = await fetch(url).then(res => res.blob())
const reader = new FileReader()
const promise = new Promise((resolve, reject) => {
// dataurl to base64
reader.onload = () => resolve(reader.result.split(',')[1])
reader.onerror = reject
})
reader.readAsDataURL(blob)
export default await promise`
}
}
},
}
}
export default defineConfig({
plugins: [
reactVirtualized(),
scratchGuiPlugin(),
react()
],
css: {
modules: {
localsConvention: 'camelCaseOnly'
}
},
esbuild: {
define: {
global: 'globalThis',
'process': `{ "env": {} }`
},
target: 'esnext'
},
build: {
target: 'esnext'
},
optimizeDeps: {
esbuildOptions: {
define: {
global: 'globalThis',
'process': `{ "env": {} }`
}
}
}
})