Skip to content

Instantly share code, notes, and snippets.

@cecilemuller
Last active January 28, 2025 16:37
Show Gist options
  • Save cecilemuller/a8ef2715c334dd80286b54a5780fb88d to your computer and use it in GitHub Desktop.
Save cecilemuller/a8ef2715c334dd80286b54a5780fb88d to your computer and use it in GitHub Desktop.
Vite Plugin: emit an asset using `emitFile` and `configureServer`
import type { PluginOption } from "vite";
export function plugin(): PluginOption {
return [
{
apply: "serve",
name: "example-serve",
configureServer(server) {
server.middlewares.use("/subfolder/example.txt", (_req, res) => {
res.appendHeader("Content-Type", "text/plain");
res.end("Generated by vite dev");
});
}
},
{
apply: "build",
name: "example-build",
generateBundle() {
this.emitFile({
type: "asset",
fileName: "subfolder/example.txt",
source: "Generated by vite build",
});
}
}
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment