|
| 1 | +// |
| 2 | +// This file is distributed under the MIT License |
| 3 | +// |
| 4 | +import { |
| 5 | + createStaticHandler, |
| 6 | + createStaticRouter, |
| 7 | + StaticRouterProvider, |
| 8 | +} from 'react-router'; |
| 9 | +import { renderToPipeableStream } from 'react-dom/server'; |
| 10 | +import { finished } from 'node:stream/promises'; |
| 11 | +import React from 'react'; |
| 12 | +import { WritableStreamBuffer } from 'stream-buffers'; |
| 13 | +import { Plugin } from 'vite'; |
| 14 | +import { resolve } from 'path'; |
| 15 | + |
| 16 | +const routesToPaths = (routes, parentRoute = '') => { |
| 17 | + return routes.flatMap(({ path, children }) => { |
| 18 | + let currentPath = `${parentRoute}${path}`; |
| 19 | + |
| 20 | + // Remove special case where the path is two repeating slashes |
| 21 | + currentPath = currentPath.replace(/(\/)+/g, '/'); |
| 22 | + // Remove trailing '*' |
| 23 | + currentPath = currentPath.replace(/\*$/, ''); |
| 24 | + |
| 25 | + if(typeof children !== 'undefined') { |
| 26 | + return routesToPaths(children, currentPath); |
| 27 | + } |
| 28 | + |
| 29 | + return currentPath; |
| 30 | + }); |
| 31 | +}; |
| 32 | + |
| 33 | +const renderHtml = async (path, routes, mainScript) => { |
| 34 | + const { query, dataRoutes } = createStaticHandler(routes); |
| 35 | + |
| 36 | + const url = new URL(path, 'http://localhost/') |
| 37 | + url.search = ''; |
| 38 | + url.hash = ''; |
| 39 | + url.pathname = path; |
| 40 | + |
| 41 | + const context = await query(new Request(url.href, { |
| 42 | + signal: new AbortController().signal, |
| 43 | + })); |
| 44 | + |
| 45 | + // If we got a redirect response, short circuit |
| 46 | + if (context instanceof Response) { |
| 47 | + throw context; |
| 48 | + } |
| 49 | + |
| 50 | + const router = createStaticRouter(dataRoutes, context); |
| 51 | + |
| 52 | + const app = <html lang="en"> |
| 53 | + <head> |
| 54 | + <meta charSet="UTF-8" /> |
| 55 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 56 | + </head> |
| 57 | + <body> |
| 58 | + <div id="root"> |
| 59 | + <React.StrictMode> |
| 60 | + <StaticRouterProvider router={router} context={context} /> |
| 61 | + </React.StrictMode> |
| 62 | + </div> |
| 63 | + <script type="module" src={mainScript}></script> |
| 64 | + </body> |
| 65 | + </html>; |
| 66 | + |
| 67 | + const writableStream = new WritableStreamBuffer(); |
| 68 | + const { pipe } = renderToPipeableStream(app, { |
| 69 | + onError(e) { |
| 70 | + throw e; |
| 71 | + }, |
| 72 | + onAllReady() { |
| 73 | + pipe(writableStream); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + await finished(writableStream); |
| 78 | + return writableStream.getContentsAsString('utf8'); |
| 79 | +} |
| 80 | + |
| 81 | +const fileNameFromPath = (path) => { |
| 82 | + let fileName = `${path}/index.html`; |
| 83 | + |
| 84 | + fileName = fileName.replace(/(\/)+/g, '/'); |
| 85 | + fileName = fileName.replace(/^\//, ''); |
| 86 | + |
| 87 | + fileName = resolve(__dirname, 'src', fileName); |
| 88 | + |
| 89 | + return fileName; |
| 90 | +}; |
| 91 | + |
| 92 | +function ssgPlugin({ routes, mainScript }) : Plugin { |
| 93 | + const paths = routesToPaths(routes); |
| 94 | + |
| 95 | + return { |
| 96 | + name: 'ssg-plugin', |
| 97 | + |
| 98 | + // Add all routes in the `paths` array as chunks |
| 99 | + buildStart() { |
| 100 | + paths.forEach(path => { |
| 101 | + const id = fileNameFromPath(path); |
| 102 | + |
| 103 | + this.emitFile({ |
| 104 | + type: 'chunk', |
| 105 | + id, |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + }, |
| 110 | + |
| 111 | + // resolveId just matches up files that we later want to handle in load |
| 112 | + // without it we don't get to load the files in this plugin |
| 113 | + resolveId(id) { |
| 114 | + const file = paths.find(path => { |
| 115 | + const idFromPath = fileNameFromPath(path); |
| 116 | + return idFromPath === id; |
| 117 | + }); |
| 118 | + |
| 119 | + if(file) { |
| 120 | + return id; |
| 121 | + } |
| 122 | + }, |
| 123 | + |
| 124 | + // build page |
| 125 | + async load( id ) { |
| 126 | + const path = paths.find(path => { |
| 127 | + const idFromPath = fileNameFromPath(path); |
| 128 | + return idFromPath === id; |
| 129 | + }); |
| 130 | + |
| 131 | + if(path) { |
| 132 | + return await renderHtml(path, routes, mainScript); |
| 133 | + } |
| 134 | + |
| 135 | + return null; |
| 136 | + }, |
| 137 | + }; |
| 138 | +} |
| 139 | + |
| 140 | +export default ssgPlugin; |
0 commit comments