@lidhium/cli
Version:
micro-frontend cli packed with webpack
64 lines (63 loc) • 2.72 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDevServer = createDevServer;
const cors_1 = __importDefault(require("cors"));
const express_1 = __importDefault(require("express"));
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const app = (0, express_1.default)();
function createDevServer(configFile, port = 3000) {
// Enable CORS
app.use((0, cors_1.default)());
// Add logging middleware
app.use((req, res, next) => {
console.log(`${req.method} ${req.url}`);
next();
});
// API endpoint to get lidhium config - This must come BEFORE static file serving
app.get("/api/lidhium-config", (req, res) => {
console.log("API endpoint hit");
try {
// Set proper content type and CORS headers
res.setHeader("Content-Type", "application/json");
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET");
// Read the latest config file
const configPath = path_1.default.join(process.cwd(), "lidhium.config.json");
const latestConfig = JSON.parse(fs_1.default.readFileSync(configPath, "utf8"));
res.json(latestConfig);
}
catch (error) {
console.error("Error in API:", error);
res.status(500).json({ error: "Failed to load lidhium config" });
}
});
// Get the absolute path to the dist directory
const distPath = path_1.default.join(__dirname, "../../dev-tool/dist");
// Serve static files from the build directory
app.use(express_1.default.static(distPath));
// Serve index.html for all other routes (SPA support)
app.get("*", (req, res) => {
console.log("Serving index.html for:", req.url);
const indexPath = path_1.default.join(distPath, "index.html");
console.log("Serving index.html from:", indexPath);
res.sendFile(indexPath);
});
// Watch for config file changes
const configPath = path_1.default.join(process.cwd(), "lidhium.config.json");
fs_1.default.watch(configPath, (eventType, filename) => {
if (eventType === "change") {
console.log("Config file changed, updates will be reflected in next API call");
}
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
console.log(`Access your app at http://localhost:${port}`);
});
return app;
}
// Example usage:
// createDevServer(3000);