-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathvite.config.ts
More file actions
105 lines (94 loc) · 2.97 KB
/
vite.config.ts
File metadata and controls
105 lines (94 loc) · 2.97 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
import NodeFS from 'node:fs/promises'
import NodePath from 'node:path'
import NodeChildProcess from 'node:child_process'
import NodeProcess from 'node:process'
import { cloudflare } from '@cloudflare/vite-plugin'
import { defineConfig, loadEnv, type Plugin } from 'vite'
const exists = async (path: string) =>
NodeFS.stat(path)
.then(() => true)
.catch(() => false)
const commitSha =
NodeChildProcess.execSync('git rev-parse --short HEAD').toString().trim() ||
NodeProcess.env.CF_PAGES_COMMIT_SHA?.slice(0, 7)
export default defineConfig((config) => {
const env = loadEnv(config.mode, process.cwd(), '')
return {
resolve: {
tsconfigPaths: true,
},
plugins: [cloudflare(), processIcons(), copyAssetsPlugin()],
define: {
__BUILD_VERSION__: JSON.stringify(commitSha ?? Date.now().toString()),
},
// Serve files from 'data' directory as static assets in dev mode
// This matches wrangler.json's assets.directory setting
publicDir: 'data',
server: {
port: Number(env.PORT ?? 3_000),
allowedHosts: config.mode === 'development' ? true : undefined,
},
}
})
/** copies ./data to dist/tokenlist/data */
function copyAssetsPlugin(): Plugin {
return {
name: 'copy-assets',
apply: 'build',
applyToEnvironment(environment) {
return environment.name === 'tokenlist'
},
enforce: 'post',
async closeBundle() {
const cwd = process.cwd()
// Copy data directory to dist
const src = NodePath.resolve(cwd, 'data')
const dest = NodePath.resolve(cwd, 'dist/tokenlist/data')
if (await exists(dest)) await NodeFS.rm(dest, { recursive: true })
await NodeFS.cp(src, dest, { recursive: true })
console.log('Copied data/ to dist/tokenlist/data/')
// Small delay to ensure Cloudflare plugin has finished writing
await new Promise((r) => setTimeout(r, 100))
// Patch wrangler.json to include assets config
const wranglerPath = NodePath.resolve(cwd, 'dist/tokenlist/wrangler.json')
if (await exists(wranglerPath)) {
const wranglerJson = JSON.parse(
await NodeFS.readFile(wranglerPath, 'utf-8'),
) as Record<string, unknown>
wranglerJson.assets = {
directory: 'data',
binding: 'ASSETS',
run_worker_first: true,
}
await NodeFS.writeFile(wranglerPath, JSON.stringify(wranglerJson))
console.info('Patched wrangler.json with assets config')
// Verify the write
const verifyContent = await NodeFS.readFile(wranglerPath, 'utf-8')
console.info(
'Verify assets in wrangler.json:',
verifyContent.includes('"assets"'),
)
}
},
}
}
// runs `node ./scripts/svg2g.ts` before build
function processIcons(): Plugin {
return {
name: 'process-icons',
apply: 'build',
enforce: 'pre',
async buildStart() {
try {
console.log('Processing SVG icons...')
NodeChildProcess.execSync('node ./scripts/svg2g.ts', {
stdio: 'inherit',
})
console.log('SVG icons processed successfully.')
} catch (error) {
console.error('Error processing SVG icons:', error)
throw error
}
},
}
}