@staroverlay/cli
Version:
CLI for StarOverlay related projects.
82 lines (80 loc) • 3.65 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import fs from "fs/promises";
import * as path from "path";
import { createServer } from "vite";
export default class DevServer {
constructor(options) {
this.options = options;
this.vite = null;
}
start(ctx) {
return __awaiter(this, void 0, void 0, function* () {
const vite = yield createServer({
configFile: false,
root: this.options.root,
server: {
middlewareMode: {
server: ctx.server
}
},
appType: "custom"
});
ctx.app.use(vite.middlewares);
ctx.app.get("*", (req, res, next) => __awaiter(this, void 0, void 0, function* () {
const url = req.originalUrl;
try {
// 1. Read index.html
const template = yield fs.readFile(path.resolve(this.options.root, 'index.html'), 'utf-8');
const page = `
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>StarOverlay | Dev Server</title>
<script src="/staroverlay.dom.js"></script>
</head>
<body>
<div id="app">${template}</div>
<script>
injectLibDOM({
backendURL: undefined,
workerURL: "/worker",
widgetToken: "dev_server",
});
</script>
</body>
</html>
`;
// 2. Apply Vite HTML transforms. This injects the Vite HMR client,
// and also applies HTML transforms from Vite plugins, e.g. global
// preambles from @vitejs/plugin-react
const result = yield vite.transformIndexHtml(url, page);
// 6. Send the rendered HTML back.
res.status(200).set({ 'Content-Type': 'text/html' }).end(result);
}
catch (e) {
const error = e;
// If an error is caught, let Vite fix the stack trace so it maps back
// to your actual source code.
vite.ssrFixStacktrace(error);
next(error);
}
}));
this.vite = vite;
});
}
stop() {
var _a;
(_a = this.vite) === null || _a === void 0 ? void 0 : _a.close();
}
}
//# sourceMappingURL=vite.js.map