soapbox/vite.config.ts

129 wiersze
3.2 KiB
TypeScript
Czysty Zwykły widok Historia

/// <reference types="vitest" />
2023-09-19 00:14:23 +00:00
import fs from 'node:fs';
import { fileURLToPath, URL } from 'node:url';
2023-09-13 17:04:17 +00:00
import react from '@vitejs/plugin-react';
import { visualizer } from 'rollup-plugin-visualizer';
2023-09-13 17:04:17 +00:00
import { defineConfig } from 'vite';
2023-09-20 23:50:17 +00:00
import checker from 'vite-plugin-checker';
2023-09-13 17:04:17 +00:00
import compileTime from 'vite-plugin-compile-time';
import { createHtmlPlugin } from 'vite-plugin-html';
2023-09-15 19:37:09 +00:00
import { VitePWA } from 'vite-plugin-pwa';
2023-09-13 17:04:17 +00:00
import vitePluginRequire from 'vite-plugin-require';
import { viteStaticCopy } from 'vite-plugin-static-copy';
export default defineConfig(({ command }) => ({
2023-09-13 17:04:17 +00:00
build: {
assetsDir: 'packs',
assetsInlineLimit: 0,
2023-09-15 00:14:16 +00:00
rollupOptions: {
output: {
assetFileNames: 'packs/assets/[name]-[hash].[ext]',
chunkFileNames: 'packs/js/[name]-[hash].js',
entryFileNames: 'packs/[name]-[hash].js',
},
},
2023-10-07 16:50:42 +00:00
sourcemap: true,
2023-09-13 17:04:17 +00:00
},
2023-09-18 21:57:13 +00:00
assetsInclude: ['**/*.oga'],
2023-09-13 17:24:49 +00:00
server: {
2024-02-11 22:39:45 +00:00
port: Number(process.env.PORT ?? 3036),
2023-09-13 17:24:49 +00:00
},
optimizeDeps: {
exclude: command === 'serve' ? ['@soapbox.pub/wasmboy'] : [],
},
2023-09-13 17:04:17 +00:00
plugins: [
2023-09-20 23:50:17 +00:00
checker({ typescript: true }),
2023-09-13 17:04:17 +00:00
// @ts-ignore
vitePluginRequire.default(),
2023-09-15 19:37:09 +00:00
compileTime(),
2023-09-13 17:04:17 +00:00
createHtmlPlugin({
2023-09-18 22:39:22 +00:00
template: 'index.html',
minify: {
collapseWhitespace: true,
removeComments: false,
},
2023-09-19 00:14:23 +00:00
inject: {
data: {
snippets: readFileContents('custom/snippets.html'),
},
},
2023-09-13 17:04:17 +00:00
}),
react({
// Use React plugin in all *.jsx and *.tsx files
include: '**/*.{jsx,tsx}',
2023-09-13 21:35:41 +00:00
babel: {
configFile: './babel.config.cjs',
},
2023-09-13 17:04:17 +00:00
}),
2023-09-15 19:37:09 +00:00
VitePWA({
injectRegister: null,
strategies: 'injectManifest',
injectManifest: {
injectionPoint: undefined,
plugins: [
// @ts-ignore
compileTime(),
],
},
manifestFilename: 'manifest.json',
manifest: {
name: 'Soapbox',
short_name: 'Soapbox',
description: 'A social media frontend with a focus on custom branding and ease of use.',
},
2023-09-18 21:57:13 +00:00
srcDir: 'src/service-worker',
2023-09-15 19:37:09 +00:00
filename: 'sw.ts',
}),
2023-09-13 17:04:17 +00:00
viteStaticCopy({
targets: [{
2023-09-18 21:57:13 +00:00
src: './node_modules/twemoji/assets/svg/*',
2023-09-13 17:04:17 +00:00
dest: 'packs/emoji/',
2023-09-18 21:59:17 +00:00
}, {
src: './src/instance',
dest: '.',
2023-09-19 00:20:08 +00:00
}, {
src: './custom/instance',
dest: '.',
2023-09-13 17:04:17 +00:00
}],
}),
visualizer({
emitFile: true,
filename: 'report.html',
title: 'Soapbox Bundle',
}),
2024-01-12 23:25:15 +00:00
{
name: 'mock-api',
configureServer(server) {
server.middlewares.use((req, res, next) => {
if (/^\/api\//.test(req.url!)) {
res.statusCode = 404;
res.end('Not Found');
} else {
next();
}
});
},
},
2023-09-13 17:04:17 +00:00
],
resolve: {
alias: [
{ find: 'soapbox', replacement: fileURLToPath(new URL('./src', import.meta.url)) },
2023-09-13 17:04:17 +00:00
],
},
test: {
globals: true,
environment: 'jsdom',
2023-09-18 21:57:13 +00:00
setupFiles: 'src/jest/test-setup.ts',
},
}));
2023-09-19 00:14:23 +00:00
/** Return file as string, or return empty string if the file isn't found. */
function readFileContents(path: string) {
try {
return fs.readFileSync(path, 'utf8');
} catch {
return '';
}
}