@platform/react.ssr
Version:
An SSR (server-side-render) system for react apps bundled with ParcelJS and hosted on S3.
89 lines (88 loc) • 3.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
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, size, 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:
size = _a.sent();
return [4, common_1.fs.glob.find(common_1.fs.join(dir, '**'))];
case 2:
files = _a.sent();
files = files.map(function (path) { return path.substring(dir.length + 1); });
entries = (args.entries || [])
.filter(function (entry) { return files.includes(entry.file); })
.map(function (entry) { return renderEntry(entry); });
manifest = {
version: version,
createdAt: common_1.time.now.timestamp,
bytes: size.bytes,
size: size.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;
var _b = renderStatic(function () { return ReactDOMServer.renderToString(args.el); }), html = _b.html, css = _b.css;
return { file: file, id: id, html: html, css: css };
}