npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite-surf

v0.0.7

Published

A Vite plugin that adds Express server, React Router and SSR.

Downloads

476

Readme

Vite Surf

A Vite plugin that adds Express server, React Router and SSR.

Note: This is package is under development!

Setup

Create Vite app from boilerplate:

npm create vite@latest my-react-app -- --template react-ts
cd my-react-app

Install dependencies:

npm install vite-surf react-router-dom express sirv
npm install -D @types/express

Configure vite.config.ts:

import { defineConfig } from 'vite'
import react from "@vitejs/plugin-react";
+import surfPlugin, { virtualClientEntry } from "vite-surf/plugin";

// https://vite.dev/config/
 export default defineConfig({
-  plugins: [react()],
-})
+  build: {
+    rollupOptions: {
+      input: [virtualClientEntry],
+    },
+  },
+  plugins: [surfPlugin(), react()],
+});

Configure package.json scripts:

   "scripts": {
     "dev": "vite",
-    "build": "tsc -b && vite build",
+    "build:client": "vite build --manifest --outDir dist/client",
+    "build:server": "vite build --ssr src/server.ts --outDir dist/server",
+    "build": "npm run build:client && npm run build:server",
     "lint": "eslint .",
-    "preview": "vite preview"
+    "start": "node ./dist/server/server.js"
   },

Remove some files, and add some new ones:

    node_modules/
    public/
    src/
        assets/
            react.svg
-       App.css
-       App.tsx
+       Document.tsx
        index.css
-       main.tsx
+       routes.tsx
+       server.ts
        vite-env.d.ts
    .gitignore
    README.md
    eslint.config.js
-   index.html
    package-lock.json
    package.json
    tsconfig.app.json
    tsconfig.json
    tsconfig.node.json
    vite.config.ts

src/Document.tsx

import { ReactNode } from "react";
import { Links, Scripts } from "vite-surf";

export default function Document({ children }: { children: ReactNode }) {
  return (
    <html lang="en">
      <head>
        <meta charSet="UTF-8" />
        <link rel="icon" type="image/svg+xml" href="/vite.svg" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Vite + React + TS + SSR</title>
        <Links />
      </head>
      <body>
        <div id="root">{children}</div>
        <Scripts />
      </body>
    </html>
  );
}

src/server.ts

import express from "express";
import { surfMiddleware } from "vite-surf";
import Document from "./Document";
import routes from "./routes";
import sirv from "sirv";
import manifest from "../dist/client/.vite/manifest.json";

const app = express();

app.get("/test", (_req, res) => {
  res.send("hello world");
});

app.use(sirv("./dist/client"));
app.use(surfMiddleware({ Document, routes, manifest }));

app.listen(3000);

src/routes.tsx

import { Link, Route, Routes } from "react-router-dom";
import "./index.css";

export default (
  <Routes>
    <Route
      path="/"
      element={
        <div>
          Home <Link to="/about">About</Link>
        </div>
      }
    />
    <Route
      path="/about"
      element={
        <div>
          About <Link to="/">home</Link>
        </div>
      }
    />
  </Routes>
);

Development

npm run dev

Production

npm run build
npm start