|
| 1 | +import { defineConfig, Plugin } from "vite"; |
| 2 | +import { resolve, posix } from "path"; |
| 3 | +import fs from "fs"; |
| 4 | +import { NextHandleFunction } from "connect"; |
| 5 | + |
| 6 | +// Automatically set up aliases for monorepo packages. |
| 7 | +// Uses built packages in prod, "source" field in dev. |
| 8 | +function packages(prod: boolean) { |
| 9 | + const alias: Record<string, string> = {}; |
| 10 | + const root = resolve(__dirname, "../packages"); |
| 11 | + for (let name of fs.readdirSync(root)) { |
| 12 | + if (name[0] === ".") continue; |
| 13 | + const p = resolve(root, name, "package.json"); |
| 14 | + const pkg = JSON.parse(fs.readFileSync(p, "utf-8")); |
| 15 | + if (pkg.private) continue; |
| 16 | + const entry = prod ? "." : pkg.source; |
| 17 | + alias[pkg.name] = resolve(root, name, entry); |
| 18 | + } |
| 19 | + return alias; |
| 20 | +} |
| 21 | + |
| 22 | +export default defineConfig(env => ({ |
| 23 | + plugins: [indexPlugin(), multiSpa(["index.html", "cases/**/*.html"])], |
| 24 | + build: { |
| 25 | + polyfillModulePreload: false, |
| 26 | + cssCodeSplit: false, |
| 27 | + }, |
| 28 | + resolve: { |
| 29 | + extensions: [".ts", ".tsx", ".js", ".jsx", ".d.ts"], |
| 30 | + alias: env.mode === "production" ? {} : packages(false), |
| 31 | + }, |
| 32 | +})); |
| 33 | + |
| 34 | +function indexPlugin(): Plugin { |
| 35 | + return { |
| 36 | + name: "index-plugin", |
| 37 | + async transformIndexHtml(html, data) { |
| 38 | + if (data.path === "/index.html") { |
| 39 | + const cases = await getTestCases("cases/**/*.html"); |
| 40 | + return html.replace( |
| 41 | + "{%LIST%}", |
| 42 | + cases.htmlEntries.length > 0 |
| 43 | + ? cases.htmlUrls |
| 44 | + .map(url => `<li><a href="${encodeURI(url)}">${url}</a></li>`) |
| 45 | + .join("\n") |
| 46 | + : "" |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + const name = posix.basename(posix.dirname(data.path)); |
| 51 | + return html.replace("{%TITLE%}", name).replace("{%NAME%}", name); |
| 52 | + }, |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +// Vite plugin to serve and build multiple SPA roots (index.html dirs) |
| 57 | +import glob from "tiny-glob"; |
| 58 | + |
| 59 | +async function getTestCases(entries: string | string[]) { |
| 60 | + let e = await Promise.all([entries].flat().map(x => glob(x))); |
| 61 | + const htmlEntries = Array.from(new Set(e.flat())); |
| 62 | + // sort by length, longest to shortest: |
| 63 | + const htmlUrls = htmlEntries |
| 64 | + .map(x => "/" + x) |
| 65 | + .sort((a, b) => b.length - a.length); |
| 66 | + return { htmlEntries, htmlUrls }; |
| 67 | +} |
| 68 | + |
| 69 | +function multiSpa(entries: string | string[]): Plugin { |
| 70 | + let htmlEntries: string[]; |
| 71 | + let htmlUrls: string[]; |
| 72 | + |
| 73 | + const middleware: NextHandleFunction = (req, res, next) => { |
| 74 | + const url = req.url!; |
| 75 | + // ignore /@x and file extension URLs: |
| 76 | + if (/(^\/@|\.[a-z]+(?:\?.*)?$)/i.test(url)) return next(); |
| 77 | + // match the longest index.html parent path: |
| 78 | + for (let html of htmlUrls) { |
| 79 | + if (!html.endsWith("/index.html")) continue; |
| 80 | + if (!url.startsWith(html.slice(0, -10))) continue; |
| 81 | + req.url = html; |
| 82 | + break; |
| 83 | + } |
| 84 | + next(); |
| 85 | + }; |
| 86 | + |
| 87 | + return { |
| 88 | + name: "multi-spa", |
| 89 | + async config() { |
| 90 | + const cases = await getTestCases(entries); |
| 91 | + htmlEntries = cases.htmlEntries; |
| 92 | + htmlUrls = cases.htmlUrls; |
| 93 | + }, |
| 94 | + buildStart(options) { |
| 95 | + options.input = htmlEntries; |
| 96 | + }, |
| 97 | + configurePreviewServer(server) { |
| 98 | + server.middlewares.use(middleware); |
| 99 | + }, |
| 100 | + configureServer(server) { |
| 101 | + server.middlewares.use(middleware); |
| 102 | + }, |
| 103 | + }; |
| 104 | +} |
0 commit comments