-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
111 lines (109 loc) · 3.41 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
import { defineConfig, type ResolvedConfig, type Plugin, type UserConfig } from 'vite'
import { reactVirtualized } from './plugins/reactVirtualized.ts'
import * as path from 'node:path'
import react from '@vitejs/plugin-react-swc'
import packageJSON from './package.json' with { type: 'json' }
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((env) => {
const base: UserConfig = {
plugins: [
reactVirtualized(),
scratchGuiPlugin(),
react()
],
css: {
modules: {
localsConvention: 'camelCaseOnly'
}
},
esbuild: {
define: {
global: 'globalThis',
'process': `{ "env": {} }`
},
target: 'esnext'
},
build: process.env.PLAYGROUND ? {
target: 'esnext',
} :{
target: 'esnext',
lib: {
entry: 'src/mod.tsx',
formats: ['es'],
fileName(format, entryName) {
return 'mod.js'
},
},
minify: false,
rollupOptions: {
external: [
...Object.keys(packageJSON.dependencies),
'react/jsx-runtime', 'react-dom/client', 'react/jsx-dev-runtime', 'react/compiler-runtime'
]
}
},
optimizeDeps: {
esbuildOptions: {
define: {
global: 'globalThis',
'process': `{ "env": {} }`
}
}
}
}
return base
})