diablo2-web/vite.config.ts

80 lines
2.1 KiB
TypeScript

import { existsSync, readdirSync, statSync } from 'node:fs'
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { defineConfig } from 'vite'
const __dirname = fileURLToPath(new URL('.', import.meta.url))
function discoverPluginHtmlEntries(rootDir: string): Record<string, string> {
const pluginsDir = resolve(rootDir, 'plugins')
if (!existsSync(pluginsDir)) return {}
const entries: Record<string, string> = {}
for (const name of readdirSync(pluginsDir).sort()) {
const pluginDir = resolve(pluginsDir, name)
if (!statSync(pluginDir).isDirectory()) continue
const htmlPath = resolve(pluginDir, 'index.html')
if (existsSync(htmlPath)) {
entries[`plugin-${name}`] = htmlPath
}
}
return entries
}
/**
* Vite configuration for the Diablo II web client (`play.html` and `plugins/<id>/index.html`).
*/
export default defineConfig({
plugins: [
{
name: 'd2web-sw-dev-alias',
configureServer(server) {
server.middlewares.use((req, _res, next) => {
if (req.url && /^\/(?:diablo2\/)?sw\.js(?:\?.*)?$/.test(req.url)) {
req.url = '/src/client/sw/sw.ts'
}
next()
})
},
},
{
name: 'd2-root-to-play-html',
configureServer(server) {
server.middlewares.use((req, _res, next) => {
if (req.url === '/' || req.url?.startsWith('/?')) {
req.url = `/play.html${req.url.slice(1)}`
}
next()
})
},
},
],
server: {
host: '127.0.0.1',
port: 5173,
strictPort: true,
proxy: {
'/d2net': {
target: 'wss://www.laiseek.xyz',
ws: true,
changeOrigin: true,
secure: true,
},
},
},
build: {
target: 'es2022',
sourcemap: true,
rollupOptions: {
input: {
play: resolve(__dirname, 'play.html'),
...discoverPluginHtmlEntries(__dirname),
sw: resolve(__dirname, 'src/client/sw/sw.ts'),
},
output: {
entryFileNames: (chunkInfo) =>
chunkInfo.name === 'sw' ? 'sw.js' : 'assets/[name]-[hash].js',
},
},
},
})