|
| 1 | +import { defineConfig, Plugin } from "vite"; |
| 2 | +import { resolve, posix } from "path"; |
| 3 | +import fs from "fs"; |
| 4 | +import { NextHandleFunction } from "connect"; |
| 5 | +import * as express from "express"; |
| 6 | + |
| 7 | +// Automatically set up aliases for monorepo packages. |
| 8 | +// Uses built packages in prod, "source" field in dev. |
| 9 | +function packages(prod: boolean) { |
| 10 | + const alias: Record<string, string> = {}; |
| 11 | + const root = resolve(__dirname, "../packages"); |
| 12 | + for (let name of fs.readdirSync(root)) { |
| 13 | + if (name[0] === ".") continue; |
| 14 | + const p = resolve(root, name, "package.json"); |
| 15 | + const pkg = JSON.parse(fs.readFileSync(p, "utf-8")); |
| 16 | + if (pkg.private) continue; |
| 17 | + const entry = prod ? "." : pkg.source; |
| 18 | + alias[pkg.name] = resolve(root, name, entry); |
| 19 | + } |
| 20 | + return alias; |
| 21 | +} |
| 22 | + |
| 23 | +export default defineConfig(env => ({ |
| 24 | + plugins: [ |
| 25 | + indexPlugin(), |
| 26 | + multiSpa(["index.html", "results.html", "cases/**/*.html"]), |
| 27 | + ], |
| 28 | + build: { |
| 29 | + polyfillModulePreload: false, |
| 30 | + cssCodeSplit: false, |
| 31 | + }, |
| 32 | + resolve: { |
| 33 | + extensions: [".ts", ".tsx", ".js", ".jsx", ".d.ts"], |
| 34 | + alias: env.mode === "production" ? {} : packages(false), |
| 35 | + }, |
| 36 | +})); |
| 37 | + |
| 38 | +export interface BenchResult { |
| 39 | + url: string; |
| 40 | + time: number; |
| 41 | + memory: number; |
| 42 | +} |
| 43 | + |
| 44 | +function escapeHtml(unsafe: string) { |
| 45 | + return unsafe |
| 46 | + .replace(/&/g, "&") |
| 47 | + .replace(/</g, "<") |
| 48 | + .replace(/>/g, ">") |
| 49 | + .replace(/"/g, """) |
| 50 | + .replace(/'/g, "'"); |
| 51 | +} |
| 52 | + |
| 53 | +function indexPlugin(): Plugin { |
| 54 | + const results = new Map<string, BenchResult>(); |
| 55 | + |
| 56 | + return { |
| 57 | + name: "index-plugin", |
| 58 | + configureServer(server) { |
| 59 | + server.middlewares.use(express.json()); |
| 60 | + server.middlewares.use(async (req, res, next) => { |
| 61 | + if (req.url === "/results") { |
| 62 | + if (req.method === "GET") { |
| 63 | + const cases = await getBenchCases("cases/**/*.html"); |
| 64 | + cases.htmlUrls.forEach(url => { |
| 65 | + if (!results.has(url)) { |
| 66 | + results.set(url, { url, time: 0, memory: 0 }); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + const items = Array.from(results.entries()) |
| 71 | + .sort((a, b) => a[0].localeCompare(b[0])) |
| 72 | + .map(entry => { |
| 73 | + return `<tr> |
| 74 | + <td><a href="${encodeURI(entry[0])}">${escapeHtml(entry[0])}</a></td> |
| 75 | + <td>${entry[1].time.toFixed(2)}ms</td> |
| 76 | + <td>${entry[1].memory}MB</td> |
| 77 | + </tr>`; |
| 78 | + }) |
| 79 | + .join("\n"); |
| 80 | + |
| 81 | + const html = fs |
| 82 | + .readFileSync(resolve(__dirname, "results.html"), "utf-8") |
| 83 | + .replace("{%ITEMS%}", items); |
| 84 | + res.end(html); |
| 85 | + return; |
| 86 | + } else if (req.method === "POST") { |
| 87 | + // @ts-ignore |
| 88 | + const { test, duration, memory } = req.body; |
| 89 | + if ( |
| 90 | + typeof test !== "string" || |
| 91 | + typeof duration !== "number" || |
| 92 | + typeof memory !== "number" |
| 93 | + ) { |
| 94 | + throw new Error("Invalid data"); |
| 95 | + } |
| 96 | + results.set(test, { url: test, time: duration, memory }); |
| 97 | + res.end(); |
| 98 | + return; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + next(); |
| 103 | + }); |
| 104 | + }, |
| 105 | + async transformIndexHtml(html, data) { |
| 106 | + if (data.path === "/index.html") { |
| 107 | + const cases = await getBenchCases("cases/**/*.html"); |
| 108 | + return html.replace( |
| 109 | + "{%LIST%}", |
| 110 | + cases.htmlEntries.length > 0 |
| 111 | + ? cases.htmlUrls |
| 112 | + .map( |
| 113 | + url => |
| 114 | + `<li><a href="${encodeURI(url)}">${escapeHtml( |
| 115 | + url |
| 116 | + )}</a></li>` |
| 117 | + ) |
| 118 | + .join("\n") |
| 119 | + : "" |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + const name = posix.basename(posix.dirname(data.path)); |
| 124 | + return html.replace("{%TITLE%}", name).replace("{%NAME%}", name); |
| 125 | + }, |
| 126 | + }; |
| 127 | +} |
| 128 | + |
| 129 | +// Vite plugin to serve and build multiple SPA roots (index.html dirs) |
| 130 | +import glob from "tiny-glob"; |
| 131 | + |
| 132 | +async function getBenchCases(entries: string | string[]) { |
| 133 | + let e = await Promise.all([entries].flat().map(x => glob(x))); |
| 134 | + const htmlEntries = Array.from(new Set(e.flat())); |
| 135 | + // sort by length, longest to shortest: |
| 136 | + const htmlUrls = htmlEntries |
| 137 | + .map(x => "/" + x) |
| 138 | + .sort((a, b) => b.length - a.length); |
| 139 | + return { htmlEntries, htmlUrls }; |
| 140 | +} |
| 141 | + |
| 142 | +function multiSpa(entries: string | string[]): Plugin { |
| 143 | + let htmlEntries: string[]; |
| 144 | + let htmlUrls: string[]; |
| 145 | + |
| 146 | + const middleware: NextHandleFunction = (req, res, next) => { |
| 147 | + const url = req.url!; |
| 148 | + // ignore /@x and file extension URLs: |
| 149 | + if (/(^\/@|\.[a-z]+(?:\?.*)?$)/i.test(url)) return next(); |
| 150 | + // match the longest index.html parent path: |
| 151 | + for (let html of htmlUrls) { |
| 152 | + if (!html.endsWith("/index.html")) continue; |
| 153 | + if (!url.startsWith(html.slice(0, -10))) continue; |
| 154 | + req.url = html; |
| 155 | + break; |
| 156 | + } |
| 157 | + next(); |
| 158 | + }; |
| 159 | + |
| 160 | + return { |
| 161 | + name: "multi-spa", |
| 162 | + async config() { |
| 163 | + const cases = await getBenchCases(entries); |
| 164 | + htmlEntries = cases.htmlEntries; |
| 165 | + htmlUrls = cases.htmlUrls; |
| 166 | + }, |
| 167 | + buildStart(options) { |
| 168 | + options.input = htmlEntries; |
| 169 | + }, |
| 170 | + configurePreviewServer(server) { |
| 171 | + server.middlewares.use(middleware); |
| 172 | + }, |
| 173 | + configureServer(server) { |
| 174 | + server.middlewares.use(middleware); |
| 175 | + }, |
| 176 | + }; |
| 177 | +} |
0 commit comments