Last active
January 28, 2025 14:46
-
-
Save cecilemuller/29e8441914742ba57b06c75d00272b5d to your computer and use it in GitHub Desktop.
Vite Plugin: configureServer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { PluginOption } from "vite"; | |
export function plugin(): PluginOption { | |
return { | |
name: "example", | |
configureServer(server) { | |
// Log all requests | |
server.middlewares.use((req, _res, next) => { | |
console.log(req.url); // full path | |
next(); | |
}); | |
// Log specific requests | |
server.middlewares.use("/subfolder", (req, _res, next) => { | |
console.log(req.originalUrl); // full path | |
console.log(req.url); // subpath inside subfolder | |
next(); | |
}); | |
// Simulate emitFile | |
server.middlewares.use("/example.txt", (req, res) => { | |
console.log(req.url); | |
res.end("HELLO WORLD"); | |
}); | |
// Ensure virtual paths call Vite hooks: | |
// https://bsky.app/profile/wildpeaks.fr/post/3lgqkd27kkk2s | |
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-Fetch-Dest | |
server.middlewares.use("/virtual.fake", (req, _res, next) => { | |
req.headers["sec-fetch-dest"] = "script"; | |
next(); | |
}); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment