|
| 1 | +// @ts-check |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import { pathToFileURL } from 'url'; |
| 5 | +import express from 'express'; |
| 6 | +import compression from 'compression'; |
| 7 | +import serveStatic from 'serve-static'; |
| 8 | + |
| 9 | +let port = 3000; |
| 10 | +const args = process.argv.slice(2); |
| 11 | +const portArgPos = args.indexOf('--port') + 1; |
| 12 | +if (portArgPos > 0) { |
| 13 | + port = parseInt(args[portArgPos], 10); |
| 14 | +} |
| 15 | + |
| 16 | +async function createServer(root = process.cwd(), isProd = process.env.NODE_ENV === 'production') { |
| 17 | + const resolve = (p) => path.resolve(root, p); |
| 18 | + |
| 19 | + const indexProd = isProd ? fs.readFileSync(resolve('dist/client/index.html'), 'utf-8') : ''; |
| 20 | + |
| 21 | + const manifest = isProd |
| 22 | + ? // @ts-ignore |
| 23 | + // eslint-disable-next-line node/no-missing-require |
| 24 | + JSON.parse(fs.readFileSync(resolve('dist/client/ssr-manifest.json'), 'utf-8')) |
| 25 | + : {}; |
| 26 | + |
| 27 | + const app = express(); |
| 28 | + |
| 29 | + /** |
| 30 | + * @type {import('vite').ViteDevServer} |
| 31 | + */ |
| 32 | + let vite; |
| 33 | + if (!isProd) { |
| 34 | + const inlineCfg = { |
| 35 | + root, |
| 36 | + server: { |
| 37 | + middlewareMode: true, |
| 38 | + port |
| 39 | + } |
| 40 | + }; |
| 41 | + // @ts-ignore |
| 42 | + vite = await (await import('vite')).createServer(inlineCfg); |
| 43 | + // use vite's connect instance as middleware |
| 44 | + app.use(vite.middlewares); |
| 45 | + } else { |
| 46 | + app.use(compression()); |
| 47 | + app.use( |
| 48 | + serveStatic(resolve('dist/client'), { |
| 49 | + index: false |
| 50 | + }) |
| 51 | + ); |
| 52 | + |
| 53 | + // workaround, dist/server/entry-server.js will be cjs, add a package.json as hint |
| 54 | + const serverPkg = resolve('dist/server/package.json'); |
| 55 | + if (!fs.existsSync(serverPkg)) { |
| 56 | + fs.writeFileSync(serverPkg, JSON.stringify({ type: 'commonjs' }), 'utf-8'); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + app.use('*', async (req, res) => { |
| 61 | + try { |
| 62 | + const url = req.originalUrl; |
| 63 | + |
| 64 | + let template, render; |
| 65 | + if (!isProd) { |
| 66 | + // always read fresh template in dev |
| 67 | + template = fs.readFileSync(resolve('index.html'), 'utf-8'); |
| 68 | + template = await vite.transformIndexHtml(url, template); |
| 69 | + render = (await vite.ssrLoadModule('/src/entry-server.js')).render; |
| 70 | + } else { |
| 71 | + template = indexProd; |
| 72 | + // @ts-ignore |
| 73 | + render = (await import(pathToFileURL(resolve('dist/server/entry-server.js')).href)).render; |
| 74 | + } |
| 75 | + const rendered = await render(req.originalUrl, manifest); |
| 76 | + const appHtml = rendered.html; |
| 77 | + const headElements = rendered.head || ''; |
| 78 | + // TODO what do we do with rendered.css here. find out if emitCss was used and vite took care of it |
| 79 | + const html = template |
| 80 | + .replace(`<!--head-outlet-->`, headElements) |
| 81 | + .replace(`<!--app-outlet-->`, appHtml); |
| 82 | + |
| 83 | + res.status(200).set({ 'Content-Type': 'text/html' }).end(html); |
| 84 | + } catch (e) { |
| 85 | + vite && vite.ssrFixStacktrace(e); |
| 86 | + console.log(e.stack); |
| 87 | + res.status(500).end(e.stack); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + return { app, vite }; |
| 92 | +} |
| 93 | + |
| 94 | +createServer().then(({ app }) => { |
| 95 | + const server = app.listen(port, () => { |
| 96 | + console.log('http://localhost:' + port); |
| 97 | + }); |
| 98 | + const exitProcess = async () => { |
| 99 | + process.off('SIGTERM', exitProcess); |
| 100 | + process.off('SIGINT', exitProcess); |
| 101 | + process.stdin.off('end', exitProcess); |
| 102 | + try { |
| 103 | + await server.close(() => { |
| 104 | + console.log('ssr server closed'); |
| 105 | + }); |
| 106 | + } finally { |
| 107 | + process.exit(0); |
| 108 | + } |
| 109 | + }; |
| 110 | + process.once('SIGTERM', exitProcess); |
| 111 | + process.once('SIGINT', exitProcess); |
| 112 | + process.stdin.on('end', exitProcess); |
| 113 | +}); |
0 commit comments