Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

elysia-vite-server

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

elysia-vite-server

Plugin which start and decorate vite dev server in development and in production mode serve static (if it needed)

latest
Source
npmnpm
Version
0.0.1
Version published
Maintainers
1
Created
Source

elysia-vite-server

Plugin which start and decorate vite dev server in development and in production mode serve static (if it needed)

Usage

Simple

Add index.html and server.ts with:

import { Elysia } from "elysia";
import { vite } from "elysia-vite-server";

new Elysia()
    .use(
        vite({
            static: {
                assets: ".",
                alwaysStatic: false,
                noCache: true,
            },
        })
    )
    .listen(3000, console.log);

[!WARNING] It serve static from root of example!! For education only

Vue SSR example (inspired by bluwy/create-vite-extra/template-ssr-vue)

import { Elysia } from "elysia";
import { vite } from "elysia-vite-server";

const isProduction = process.env.NODE_ENV === "production";
// Cached production assets
const templateHtml = isProduction ? await Bun.file("./index.html").text() : "";
const ssrManifest = isProduction
    ? await Bun.file("./index.html").text()
    : undefined;

new Elysia()
    .use(
        vite({
            static: {
                assets: "./dist/client",
                alwaysStatic: false,
                noCache: true,
            },
        })
    )
    .get("/", async ({ vite, request, set }) => {
        try {
            let template: string | undefined;
            let render: any;
            if (vite) {
                // Always read fresh template in development
                template = await Bun.file("./index.html").text();

                template = await vite.transformIndexHtml(request.url, template);
                render = (await vite.ssrLoadModule("/src/entry-server.js"))
                    .render;
            } else {
                template = templateHtml;
                render = (await import("./dist/server/entry-server.js")).render;
            }

            const rendered = await render(request.url, ssrManifest);

            const html = template
                .replace("<!--app-head-->", rendered.head ?? "")
                .replace("<!--app-html-->", rendered.html ?? "");

            return new Response(html, {
                headers: {
                    "Content-Type": "text/html",
                },
            });
        } catch (e) {
            if (e instanceof Error) {
                vite?.ssrFixStacktrace(e);
                console.log(e.stack);
                set.status = 500;

                return e.stack;
            }
        }
    })
    .listen(3000, console.log);

Options

KeyTypeDefaultDescription
mode?"development" | "production"process.env.NODE_ENV || "development"In development mode it starts vite and in production it just served static (if it needed).
vite?InlineConfigConfigure vite server in development mode.
static?StaticOptions | falseConfigure static plugin options in production mode. Pass false to disable static plugin

Powered by elysia-connect-middleware!

Keywords

elysia

FAQs

Package last updated on 21 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts