(url2: string)
| 4 | import type { ViteDevServer } from "vite"; |
| 5 | |
| 6 | export function realtimeDiff(url2: string) { |
| 7 | return { |
| 8 | name: "vite-plugin-realtime-diff", |
| 9 | configureServer(server: ViteDevServer) { |
| 10 | server.middlewares.use((req, res, next) => { |
| 11 | if (!req.url) { |
| 12 | next(); |
| 13 | return; |
| 14 | } |
| 15 | |
| 16 | const { query } = parse(req.url, true); |
| 17 | let url1 = `http://${req.headers.host}${req.url}`; |
| 18 | const pathname = req.url as string; |
| 19 | url2 = new URL(url2).origin + pathname; |
| 20 | |
| 21 | if (query._diff && query._diff === "true") { |
| 22 | // Remove the _diff query param from the URL |
| 23 | url1 = url1.replace("?_diff=true", ""); |
| 24 | url2 = url2.replace("?_diff=true", ""); |
| 25 | |
| 26 | const widthKey = (query.width as string) || "100%"; |
| 27 | const iframeWidth = getIframeWidth(widthKey); |
| 28 | |
| 29 | if (!isValidUrl(url1) || !isValidUrl(url2)) { |
| 30 | res.statusCode = 400; |
| 31 | res.end("Please provide valid URLs in the plugin options."); |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | res.setHeader("Content-Type", "text/html"); |
| 36 | res.end(generateHtmlTemplate(url1, url2, iframeWidth, widthKey)); |
| 37 | } else { |
| 38 | next(); |
| 39 | } |
| 40 | }); |
| 41 | }, |
| 42 | }; |
| 43 | } |
nothing calls this directly
no outgoing calls
no test coverage detected