UNPKG

@curi/static

Version:

Generate static HTML files from Curi routes

143 lines (131 loc) 7.22 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var fsExtra = require('fs-extra'); var path = require('path'); var router = require('@curi/router'); var inMemory = require('@hickory/in-memory'); var interactions = require('@curi/interactions'); /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __awaiter(thisArg, _arguments, P, generator) { 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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __generator(thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } var pathnames = function (config) { var routes = config.routes, pages = config.pages; return pages.map(function (page) { var route = routes.route(page.name); if (!route) { throw new Error("Failed to create pathname for route \"" + page.name + "\" because it does not exist."); } return interactions.pathname(route, page.params); }); }; function staticFiles(config) { return __awaiter(this, void 0, void 0, function () { var pages, _a, routes, _b, routerOptions, _c, render, dir, historyOptions, io, _d, pathname, filename, reusable; return __generator(this, function (_e) { pages = config.pages, _a = config.router, routes = _a.routes, _b = _a.options, routerOptions = _b === void 0 ? {} : _b, _c = config.output, render = _c.render, dir = _c.dir, historyOptions = config.history; io = pathnames({ routes: routes, pages: pages }).map(function (pathname) { return { pathname: pathname, outputPath: path.join(dir, pathname, "index.html") }; }); // if there is a catch all page to be generated, // add it to the input/output array if (config.fallback) { _d = config.fallback, pathname = _d.pathname, filename = _d.filename; io.push({ pathname: pathname, outputPath: path.join(dir, filename) }); } reusable = inMemory.createReusable(historyOptions); return [2 /*return*/, Promise.all(io.map(function (_a) { var pathname = _a.pathname, outputPath = _a.outputPath; return new Promise(function (resolve) { try { // create a new router for each so we don't run into any issues // with overlapping requests var router$1 = router.createRouter(reusable, routes, __assign({}, routerOptions, { // need to emit redirects or will get stuck waiting forever invisibleRedirects: false, history: { location: { url: pathname } } })); router$1.once(function (emitted) { try { var html = render(emitted); fsExtra.outputFile(outputPath, html).then(function () { resolve({ pathname: pathname, success: true }); }); } catch (e) { resolve({ pathname: pathname, success: false, error: e }); } }, { initial: true }); } catch (e) { resolve({ pathname: pathname, success: false, error: e }); } }); }))]; }); }); } exports.pathnames = pathnames; exports.staticFiles = staticFiles;