-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathvite.config.ts
More file actions
140 lines (132 loc) · 4.2 KB
/
Copy pathvite.config.ts
File metadata and controls
140 lines (132 loc) · 4.2 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import path from 'node:path'
import { createConfig } from '@arianrhodsandlot/vite-plus-config'
import devServer, { defaultOptions } from '@hono/vite-dev-server'
import { reactRouter } from '@react-router/dev/vite'
import tailwindcss from '@tailwindcss/vite'
import { defaults, noop } from 'es-toolkit/compat'
import { $, execaNode } from 'execa'
import fs from 'fs-extra'
import { DateTime } from 'luxon'
import devtoolsJson from 'vite-plugin-devtools-json'
import { defineConfig, type Plugin, type UserConfig } from 'vite-plus'
import { getTargetRuntime, logServerInfo, prepareWranglerConfig } from './scripts/utils.ts'
import { getDirectories } from './src/constants/env.ts'
defaults(process.env, {
RETROASSEMBLY_BUILD_TIME_VITE_BUILD_TIME: DateTime.now().setZone('utc').toISO(),
RETROASSEMBLY_BUILD_TIME_VITE_VERSION: await getVersion(),
RETROASSEMBLY_RUN_TIME_PORT: '8000',
})
async function getVersion() {
return getEnvVersion() ?? (await getGitDescription()) ?? ''
}
function getEnvVersion() {
const envNames = ['WORKERS_CI_COMMIT_SHA']
for (const envName of envNames) {
if (process.env[envName]) {
return process.env[envName].slice(0, 7)
}
}
}
async function getGitDescription() {
try {
const {
stdout: [revision],
} = await $({ lines: true })`git describe --tags`
return revision
} catch {}
}
function serverInfo() {
const plugin: Plugin = {
configureServer(server) {
const { httpServer } = server
server.printUrls = noop
server.bindCLIShortcuts = noop
server.config.logger.info = noop
httpServer?.on('listening', () => {
const address = httpServer?.address()
if (address && typeof address === 'object') {
logServerInfo('localhost', address.port, true)
}
})
},
name: 'log-server-info',
}
return plugin
}
export default defineConfig(async (env) => {
const envPort = process.env.RETROASSEMBLY_RUN_TIME_PORT || process.env.PORT
const port = envPort ? Math.trunc(Number(envPort)) || 8000 : 8000
const plugins = [tailwindcss({ optimize: false }), reactRouter(), [devtoolsJson()], serverInfo()]
const config: UserConfig = {
build: { chunkSizeWarningLimit: 1024 },
clearScreen: false,
envPrefix: 'RETROASSEMBLY_BUILD_TIME_VITE_',
pack: {
clean: false,
entry: ['scripts/serve.ts', 'src/server/node.ts'],
outDir: 'dist',
suppressWarnings: [/module level directive may not be preserved/iu],
},
plugins: plugins as UserConfig['plugins'],
server: {
allowedHosts: true,
hmr: { overlay: true },
host: true,
open: true,
port,
},
staged: {
'pnpm-lock.yaml': 'node --run=check-lockfile',
},
}
if (getTargetRuntime() === 'workerd') {
if (env.command === 'serve') {
await $`wrangler d1 migrations apply retroassembly_library`
}
await prepareWranglerConfig()
const { cloudflare } = await import('@cloudflare/vite-plugin')
plugins.push(cloudflare({ viteEnvironment: { name: 'ssr' } }))
config.resolve = {
alias: {
'@entry.server.tsx': path.resolve(
'node_modules',
'react-router-hono-fullstack-template',
'app',
'entry.server.tsx',
),
},
}
} else {
if (env.command === 'serve') {
const { storageDirectory } = getDirectories()
await fs.ensureDir(storageDirectory)
await execaNode`./src/utils/server/migration/initalization.ts`
const serverAdapterPlugin = devServer({
entry: path.resolve('src', 'server', 'node-dev.ts'),
exclude: [
...defaultOptions.exclude,
/^\/src\/.+/u,
'/.well-known/appspecific/com.chrome.devtools.json',
/\?(?:inline|url|no-inline|raw|import(?:&(?:inline|url|no-inline|raw))*)$/u,
],
injectClientScript: false,
})
delete serverAdapterPlugin.handleHotUpdate
plugins.push([serverAdapterPlugin])
}
config.resolve = {
alias: {
'@entry.server.tsx': path.resolve(
'node_modules',
'@react-router',
'dev',
'dist',
'config',
'defaults',
'entry.server.node.tsx',
),
},
}
}
return createConfig(config)
})