UNPKG

sc-prepare-next

Version:
97 lines 4.19 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.prepareNext = void 0; const app_root_path_1 = require("app-root-path"); const electron_1 = require("electron"); const node_http_1 = require("node:http"); const node_path_1 = require("node:path"); const constants_1 = require("../constants"); const PORT = process.env.PORT || 4444; /** * Starts a development server for the Next.js application. * * @param {any} dir - The directory of the Next.js application. * @param {any} port - The port number to run the development server on. Defaults to $PORT(constant) if not provided. * @return {Promise<void>} A promise that resolves when the development server is started. */ const devServer = (dir, port) => __awaiter(void 0, void 0, void 0, function* () { const next = require("next")({ dev: true, dir }); const requestHandler = next.getRequestHandler(); yield next.prepare(); const server = (0, node_http_1.createServer)(requestHandler); server.listen(port || PORT, () => { electron_1.app.on("before-quit", () => server.close()); }); }); /** * Adjusts the renderer by intercepting the file protocol and modifying the file path. * * @param {any} directory - The directory of the Next.js application. * @return {void} This function does not return a value. */ const adjustRenderer = (directory) => { const paths = ["/_next", "/static"]; const isWindows = process.platform === "win32"; electron_1.protocol.interceptFileProtocol("file", (request, callback) => { let path = request.url.substr(isWindows ? 8 : 7); for (const prefix of paths) { let newPath = path; if (isWindows) { newPath = newPath.substr(2); } if (!newPath.startsWith(prefix)) { continue; } if (isWindows) { newPath = (0, node_path_1.normalize)(newPath); } newPath = (0, node_path_1.join)(directory, "out", newPath); path = newPath; } path = decodeURIComponent(path); callback({ path }); }); }; /** * Prepares the Next.js application for rendering. * * @param {any} directories - The directories containing the Next.js application. Can be a string or an object with production and development properties. * @param {any} port - The port number to run the development server on. Defaults to PORT if not provided. * @throws {Error} If the renderer location is not defined. * @return {Promise<void>} A Promise that resolves when the Next.js application is prepared for rendering. */ const prepareNext = (directories, port) => __awaiter(void 0, void 0, void 0, function* () { if (!directories) { throw new Error("Renderer location not defined"); } if (typeof directories === "string") { directories = { production: directories, development: directories, }; } for (const directory in directories) { if (!{}.hasOwnProperty.call(directories, directory)) { continue; } if (!(0, node_path_1.isAbsolute)(directories[directory])) { directories[directory] = (0, app_root_path_1.resolve)(directories[directory]); } } if (!constants_1.isDev) { adjustRenderer(directories.production); return; } yield devServer(directories.development, port); }); exports.prepareNext = prepareNext; //# sourceMappingURL=index.js.map