UNPKG

@platform/react.ssr

Version:

A lightweight SSR (server-side-rendering) system for react apps bundled with ParcelJS and hosted on S3.

97 lines (96 loc) 4.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.prepare = void 0; var tslib_1 = require("tslib"); var ReactDOMServer = require("react-dom/server"); var common_1 = require("../common"); var logger = require("./bundler.log"); var renderStatic = require('glamor/server').renderStatic; function prepare(args) { return tslib_1.__awaiter(this, void 0, void 0, function () { var entries, bundleDir, path, manifest; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: entries = args.entries; bundleDir = common_1.fs.resolve(args.bundleDir); return [4, common_1.fs.pathExists(bundleDir)]; case 1: if (!(_a.sent())) { throw new Error("Cannot prepare, the directory does not exist. " + bundleDir); } path = common_1.fs.join(bundleDir, common_1.constants.PATH.BUNDLE_MANIFEST); return [4, bundleManifest.write({ path: path, entries: entries })]; case 2: manifest = _a.sent(); if (!args.silent) { logger.bundle({ bundleDir: bundleDir, manifest: manifest }); } return [2, { bundleDir: bundleDir, manifest: manifest }]; } }); }); } exports.prepare = prepare; var bundleManifest = { create: function (args) { return tslib_1.__awaiter(this, void 0, void 0, function () { var dir, version, dirSize, paths, files, entries, manifest; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: dir = common_1.fs.dirname(args.path); version = common_1.fs.basename(dir); return [4, common_1.fs.size.dir(dir)]; case 1: dirSize = _a.sent(); return [4, common_1.fs.glob.find(common_1.fs.join(dir, '**'))]; case 2: paths = _a.sent(); files = paths.map(function (path) { var size = dirSize.files.find(function (item) { return item.path === path; }); var file = { path: path.substring(dir.length + 1), bytes: size ? size.bytes : -1, }; return file; }); entries = (args.entries || []) .filter(function (entry) { return files.some(function (file) { return file.path === entry.file; }); }) .map(function (entry) { return renderEntry(entry); }); manifest = { version: version, createdAt: common_1.time.now.timestamp, bytes: dirSize.bytes, size: dirSize.toString(), files: files, entries: entries, }; return [2, manifest]; } }); }); }, write: function (args) { return tslib_1.__awaiter(this, void 0, void 0, function () { var manifest, yaml; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, bundleManifest.create(args)]; case 1: manifest = _a.sent(); yaml = common_1.jsYaml.safeDump(manifest); return [4, common_1.fs.writeFile(args.path, yaml)]; case 2: _a.sent(); return [2, manifest]; } }); }); }, }; function renderEntry(args) { var _a = args.id, id = _a === void 0 ? 'root' : _a, file = args.file, el = args.el; var _b = renderStatic(function () { return ReactDOMServer.renderToString(el); }), html = _b.html, css = _b.css; return { file: file, id: id, html: html, css: css }; }