UNPKG

code-workshop-kit

Version:
70 lines 3.08 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.missingIndexHtmlPlugin = void 0; const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); function missingIndexHtmlPlugin(cfg) { return { name: 'missing-index-html-plugin', serve(ctx) { var _a; let rewrittenBody = ctx.body; if (ctx.path.endsWith('/index.html') || ctx.path.endsWith('/')) { const normalizedPath = ctx.path.endsWith('/') ? `${ctx.path}index.html` : ctx.path; // Case 1: root index.html if ((cfg.absoluteDir.split(process.cwd())[1] === path_1.default.dirname(normalizedPath) || path_1.default.dirname(normalizedPath) === '/') && !fs_1.default.existsSync(path_1.default.resolve(cfg.absoluteDir, 'index.html'))) { rewrittenBody = ` <head> <style> html, body { padding: 0; margin: 0; } </style> </head> <body></body> `; } else if ( // Case 2: participant root index.html path_1.default.posix.resolve(`${cfg.absoluteDir.split(process.cwd())[1]}/`, 'participants') === path_1.default.dirname(path_1.default.dirname(normalizedPath))) { const participantFolder = path_1.default.basename(path_1.default.dirname(decodeURI(normalizedPath))); /** * For basic frontend with iframes, don't insert anything. * * Also don't insert anything if the file already exists * Checking for 404 status won't work, as this plugin is ran before static file middleware */ if (!(cfg.target === 'frontend' && ((_a = cfg.targetOptions) === null || _a === void 0 ? void 0 : _a.mode) === 'iframe') && !fs_1.default.existsSync(path_1.default.resolve(cfg.absoluteDir, 'participants', participantFolder, 'index.html'))) { rewrittenBody = ` <head> <style> html, body { margin: 0; padding: 0; } </style> </head> <body> <script type="module"> import { setCapsule } from 'code-workshop-kit'; setCapsule('${participantFolder}', { target: '${cfg.target}' }); </script> </body> `; } } } return rewrittenBody; }, }; } exports.missingIndexHtmlPlugin = missingIndexHtmlPlugin; //# sourceMappingURL=missing-index-html-plugin.js.map