Skip to content

📄 Serve static files during local development

License

Notifications You must be signed in to change notification settings

reifiedbeans/vite-plugin-serve-static

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Feb 4, 2025
15c6d35 Â· Feb 4, 2025

History

38 Commits
Dec 14, 2024
Nov 17, 2024
Dec 23, 2023
Dec 24, 2023
Dec 23, 2023
Dec 23, 2023
Dec 24, 2023
Feb 13, 2024
May 22, 2024
Feb 4, 2025
Dec 14, 2024
Dec 23, 2023
Dec 24, 2023
Dec 23, 2023

Repository files navigation

vite-plugin-serve-static

npm vite license: MIT

A simple Vite plugin for serving arbitrary static files that aren't in your public directory.

// vite.config.ts
import path from "path";
import { defineConfig } from "vite";
import serveStatic from "vite-plugin-serve-static";

const serveStaticPlugin = serveStatic([
  {
    pattern: /^\/metadata\.json/,
    resolve: "./metadata.json",
  },
  {
    pattern: /^\/dog-photos\/.*/,
    resolve: ([match]) => path.join("..", "dog-photos", match),
  },
  {
    pattern: /^\/author-photos\/(.*)/,
    resolve: (groups) => path.join("..", "authors", groups[1]) + ".jpg",
  },
]);

export default defineConfig({
  plugins: [serveStaticPlugin],
});

Config

The configuration is defined as an array of objects defining which patterns to intercept and how to resolve them.

Each pattern is defined as a regular expression. The resolve property can either be a string containing the path to a single file or a function that returns a string given the result of executing the pattern against the request path.

License

Licensed under the MIT License.