More work on bundling vite for production

This commit is contained in:
Jesse Wierzbinski 2023-12-06 14:29:26 -10:00
parent c8ffca37b1
commit d62e81977e
No known key found for this signature in database
7 changed files with 48 additions and 18 deletions

3
.gitignore vendored
View file

@ -168,4 +168,5 @@ dist
.yarn/install-state.gz .yarn/install-state.gz
.pnp.\* .pnp.\*
config/config.toml config/config.toml
uploads/ uploads/
pages/dist

View file

@ -89,6 +89,15 @@ bun dev
If your port number is lower than 1024, you may need to run the command as root. If your port number is lower than 1024, you may need to run the command as root.
### Running the Vite server
To start the Vite server, run:
```sh
bun vite:dev
```
This should be run in a separate terminal window. The Vite server is used to serve the frontend assets and to provide hot module reloading.
## Running tests ## Running tests
To run the tests, run: To run the tests, run:

View file

@ -13,6 +13,7 @@ import type { PrismaClientInitializationError } from "@prisma/client/runtime/lib
import { HookTypes, Server } from "~plugins/types"; import { HookTypes, Server } from "~plugins/types";
import { initializeRedisCache } from "@redis"; import { initializeRedisCache } from "@redis";
import { connectMeili } from "@meilisearch"; import { connectMeili } from "@meilisearch";
import routes from "~pages/routes";
const timeAtStart = performance.now(); const timeAtStart = performance.now();
const server = new Server(); const server = new Server();
@ -135,13 +136,30 @@ Bun.serve({
return await file.default(req.clone(), matchedRoute, auth); return await file.default(req.clone(), matchedRoute, auth);
} else { } else {
// Proxy response from Vite at localhost:5173 // Check if path matches Vite routes
const proxy = await fetch( if (
req.url.replace(config.http.base_url, "http://localhost:5173") routes.find(route => route.path === new URL(req.url).pathname)
); ) {
// Proxy response from Vite at localhost:5173
if (proxy.status !== 404) { const proxy = await fetch(
return proxy; req.url.replace(
config.http.base_url,
"http://localhost:5173"
)
);
if (proxy.status !== 404) {
return proxy;
}
}
if (new URL(req.url).pathname.startsWith("/assets")) {
// Serve from pages/dist/assets
return new Response(
// @ts-expect-error Custom Bun extension
Bun.file(`./pages/dist${new URL(req.url).pathname}`)
);
} }
return new Response(undefined, { return new Response(undefined, {

View file

@ -34,7 +34,8 @@
"scripts": { "scripts": {
"dev": "bun run index.ts", "dev": "bun run index.ts",
"vite:dev": "bunx --bun vite pages", "vite:dev": "bunx --bun vite pages",
"start": "bun run index.ts", "vite:build": "bunx --bun vite build pages",
"start": "NODE_ENV=production bun run index.ts & bunx --bun vite preview pages --port 5173",
"migrate-dev": "bun prisma migrate dev", "migrate-dev": "bun prisma migrate dev",
"migrate": "bun prisma migrate deploy", "migrate": "bun prisma migrate deploy",
"lint": "eslint --config .eslintrc.cjs --ext .ts .", "lint": "eslint --config .eslintrc.cjs --ext .ts .",

View file

@ -2,19 +2,12 @@ import { createApp } from "vue";
import "./style.css"; import "./style.css";
import "virtual:uno.css"; import "virtual:uno.css";
import { createRouter, createWebHistory } from "vue-router"; import { createRouter, createWebHistory } from "vue-router";
import Login from "./login.vue";
import App from "./App.vue"; import App from "./App.vue";
import routes from "./routes";
const Home = { template: "<div>Home</div>" };
const routes = [
{ path: "/", component: Home },
{ path: "/oauth/authorize", component: Login },
];
const router = createRouter({ const router = createRouter({
history: createWebHistory(), history: createWebHistory(),
routes, routes: routes,
}); });
const app = createApp(App); const app = createApp(App);

8
pages/routes.ts Normal file
View file

@ -0,0 +1,8 @@
import Login from "./login.vue";
const Home = { template: "<div>Home</div>" };
export default [
{ path: "/", component: Home },
{ path: "/oauth/authorize", component: Login },
];

View file

@ -5,7 +5,7 @@ import vue from "@vitejs/plugin-vue";
export default defineConfig({ export default defineConfig({
base: "/", base: "/",
build: { build: {
outDir: "../vite-dist", outDir: "./dist",
}, },
// main.ts is in pages/ directory // main.ts is in pages/ directory
resolve: { resolve: {