1
0
mirror of https://github.com/spacebarchat/client.git synced 2024-11-24 11:22:29 +01:00
client/vite.config.ts

132 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-09-06 20:55:23 +02:00
import replace from "@rollup/plugin-replace";
import react from "@vitejs/plugin-react";
2023-10-03 23:13:34 +02:00
import fs, { readFileSync } from "fs";
2023-09-07 17:25:35 +02:00
import { internalIpV4 } from "internal-ip";
2023-10-03 23:13:34 +02:00
import path, { resolve } from "path";
import type { Plugin } from "vite";
2023-09-06 20:55:23 +02:00
import { defineConfig } from "vite";
2023-09-15 04:08:01 +02:00
import { chunkSplitPlugin } from "vite-plugin-chunk-split";
import cleanPlugin from "vite-plugin-clean";
import progress from "vite-plugin-progress";
2023-09-06 20:55:23 +02:00
import svgr from "vite-plugin-svgr";
function getGitRevision() {
try {
const rev = readFileSync(".git/HEAD").toString().trim();
if (rev.indexOf(":") === -1) {
return rev;
}
return readFileSync(`.git/${rev.substring(5)}`)
.toString()
.trim();
} catch (err) {
console.error("Failed to get Git revision.");
return "?";
}
}
function getGitBranch() {
try {
const rev = readFileSync(".git/HEAD").toString().trim();
if (rev.indexOf(":") === -1) {
return "DETACHED";
}
return rev.split("/").pop();
} catch (err) {
console.error("Failed to get Git branch.");
return "?";
}
}
function getVersion() {
return JSON.parse(readFileSync("package.json").toString()).version;
}
2024-02-28 04:33:54 +01:00
const mobile = !!/android|ios/.exec(process.env.TAURI_ENV_PLATFORM);
2023-09-07 17:25:35 +02:00
// https://vitejs.dev/config/
export default defineConfig(async () => ({
2023-09-06 20:55:23 +02:00
plugins: [
2023-09-15 18:20:16 +02:00
cleanPlugin(),
2023-10-03 23:13:34 +02:00
reactVirtualized(),
2023-09-06 20:55:23 +02:00
react(),
svgr(),
2023-09-15 04:08:01 +02:00
chunkSplitPlugin({
strategy: "unbundle",
}),
progress(),
2023-09-06 20:55:23 +02:00
replace({
__GIT_REVISION__: getGitRevision(),
__GIT_BRANCH__: getGitBranch(),
__APP_VERSION__: getVersion(),
preventAssignment: true,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) as any,
],
2023-09-07 17:25:35 +02:00
// Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build`
//
// 1. prevent vite from obscuring rust errors
clearScreen: false,
// 2. tauri expects a fixed port, fail if that port is not available
server: {
host: mobile ? "0.0.0.0" : false,
port: 1420,
hmr: mobile
? {
protocol: "ws",
host: await internalIpV4(),
port: 1421,
2024-02-28 04:40:34 +01:00
}
2023-09-07 17:25:35 +02:00
: undefined,
strictPort: true,
},
2023-09-15 18:20:16 +02:00
2023-09-07 17:25:35 +02:00
// 3. to make use of `TAURI_DEBUG` and other env variables
// https://tauri.studio/v1/api/config#buildconfig.beforedevcommand
envPrefix: ["VITE_", "TAURI_"],
2023-09-06 20:55:23 +02:00
build: {
2024-02-28 04:33:54 +01:00
outDir: "dist",
2023-09-06 20:55:23 +02:00
sourcemap: true,
rollupOptions: {
input: {
main: resolve(__dirname, "index.html"),
},
output: {
2023-09-15 18:20:16 +02:00
entryFileNames: "asset/[hash:20].js",
chunkFileNames: "asset/[hash:20].js",
assetFileNames: "asset/[hash:20].[ext]",
2023-09-06 20:55:23 +02:00
},
},
},
2023-09-07 17:25:35 +02:00
}));
2023-10-03 23:13:34 +02:00
const WRONG_CODE = `import { bpfrpt_proptype_WindowScroller } from "../WindowScroller.js";`;
export function reactVirtualized(): Plugin {
return {
name: "flat:react-virtualized",
// Note: we cannot use the `transform` hook here
// because libraries are pre-bundled in vite directly,
// plugins aren't able to hack that step currently.
// so instead we manually edit the file in node_modules.
// all we need is to find the timing before pre-bundling.
configResolved() {
const file = path.join(
"node_modules",
"react-virtualized",
"dist",
"es",
"WindowScroller",
"utils",
"onScroll.js",
);
const code = fs.readFileSync(file, "utf-8");
const modified = code.replace(WRONG_CODE, "");
fs.writeFileSync(file, modified);
},
};
}