import { defineConfig } from 'vitest/config' import { svelte } from '@sveltejs/vite-plugin-svelte' import { svelteTesting } from '@testing-library/svelte/vite' import { fileURLToPath, URL } from 'node:url' import { readFileSync, readdirSync } from 'node:fs' import type { Plugin } from 'vite' const RUFFLE_DIRECTORY = fileURLToPath( new URL('./node_modules/@ruffle-rs/ruffle/', import.meta.url), ) const RUFFLE_ASSET_PATTERN = /^(?:ruffle\.js|core\.ruffle\..+\.js|.+\.wasm|LICENSE_(?:MIT|APACHE))$/ /** Serve and emit the pinned self-hosted runtime without a third-party CDN. */ function ruffleAssets(): Plugin { const files = readdirSync(RUFFLE_DIRECTORY).filter((name) => RUFFLE_ASSET_PATTERN.test(name)) let building = false return { name: 'plspace-ruffle-assets', configResolved(config) { building = config.command === 'build' }, buildStart() { if (!building) return for (const name of files) { this.emitFile({ type: 'asset', fileName: `ruffle/${name}`, source: readFileSync(`${RUFFLE_DIRECTORY}/${name}`), }) } }, configureServer(server) { server.middlewares.use('/ruffle', (request, response, next) => { const name = decodeURIComponent((request.url ?? '').replace(/^\//, '').split('?')[0]) if (!files.includes(name)) { next() return } response.setHeader( 'Content-Type', name.endsWith('.wasm') ? 'application/wasm' : name.endsWith('.js') ? 'text/javascript' : 'text/plain', ) response.end(readFileSync(`${RUFFLE_DIRECTORY}/${name}`)) }) }, } } // Fully static output: the app talks to a Mastodon/Pleroma server directly from // the browser, so `dist/` can be dropped on any static host (or file://-ish CDN). export default defineConfig({ base: './', plugins: [svelte(), svelteTesting(), ruffleAssets()], resolve: { alias: { $lib: fileURLToPath(new URL('./src/lib', import.meta.url)), $components: fileURLToPath(new URL('./src/components', import.meta.url)), $routes: fileURLToPath(new URL('./src/routes', import.meta.url)), $test: fileURLToPath(new URL('./src/test', import.meta.url)), }, }, build: { target: 'es2022', cssCodeSplit: false, }, test: { environment: 'jsdom', environmentOptions: { jsdom: { url: 'https://plspace.test/' }, }, setupFiles: ['./src/test/setup.ts'], }, })