histoire
Version:
A fast story book powered by Vite
42 lines (41 loc) • 1.29 kB
JavaScript
import http from 'node:http';
import connect from 'connect';
import sirv from 'sirv';
export async function startPreview(port, ctx) {
const app = connect();
app.use(ctx.resolvedViteConfig.base, sirv(ctx.config.outDir, {
dev: true,
etag: true,
single: true,
}));
let finalPort = port ?? 6006;
const httpServer = http.createServer(app);
return new Promise((resolve, reject) => {
function onError(e) {
if (e.code === 'EADDRINUSE') {
httpServer.listen(++finalPort);
}
else {
reject(e);
}
}
httpServer.on('error', onError);
httpServer.listen(finalPort, () => {
httpServer.off('error', onError);
const baseUrl = `http://localhost:${finalPort}${ctx.resolvedViteConfig.base}`;
resolve({
baseUrl,
close: () => new Promise((resolve, reject) => {
httpServer.close((err) => {
if (err) {
reject(err);
}
else {
resolve();
}
});
}),
});
});
});
}