@platform/react.ssr
Version:
A lightweight SSR (server-side-rendering) system for react apps bundled with ParcelJS and hosted on S3.
196 lines (195 loc) • 8.65 kB
JavaScript
"use strict";
var _this = this;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var bundler_1 = require("../bundler");
var config_1 = require("../config");
var common_1 = require("./common");
var push = require("./cmd.push");
function run(args) {
if (args === void 0) { args = {}; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var config, _a, endpoint, manifest, pkgPath, pkg, version, _b, bundleDir, isPush, _c, tasks;
var _this = this;
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0:
_a = args.config;
if (_a) return [3, 2];
return [4, config_1.Config.create()];
case 1:
_a = (_d.sent());
_d.label = 2;
case 2:
config = _a;
endpoint = config.s3.endpoint;
return [4, common_1.fs.ensureDir(config.builder.bundles)];
case 3:
_d.sent();
return [4, common_1.fs.ancestor(config.builder.bundles).first('package.json')];
case 4:
pkgPath = _d.sent();
pkg = common_1.npm.pkg(pkgPath);
common_1.log.info.gray(common_1.fs.dirname(pkgPath));
common_1.log.info();
_b = args.version;
if (_b) return [3, 6];
return [4, common_1.npm.prompt.incrementVersion({ path: pkgPath, noChange: true, save: true })];
case 5:
_b = (_d.sent());
_d.label = 6;
case 6:
version = _b;
bundleDir = common_1.fs.resolve(common_1.fs.join(config.builder.bundles, version));
if (!(args.push !== undefined)) return [3, 7];
_c = args.push;
return [3, 9];
case 7: return [4, common_1.cli.prompt.list({ message: 'push to S3', items: ['yes', 'no'] })];
case 8:
_c = (_d.sent()) === 'yes';
_d.label = 9;
case 9:
isPush = _c;
if (isPush && !endpoint) {
throw new Error("The S3 endpoint has not been configured in [ssr.yml].");
}
common_1.log.info();
tasks = common_1.cli
.tasks()
.task('build', function (e) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
return [2, execScript(pkg, e, 'build')];
}); }); })
.task('bundle', function (e) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
return [2, execScript(pkg, e, 'bundle')];
}); }); })
.task('manifest', function (e) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var entries, res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, getEntries(config)];
case 1:
entries = _a.sent();
return [4, bundler_1.bundler.prepare({ bundleDir: bundleDir, entries: entries, silent: true })];
case 2:
res = _a.sent();
manifest = res.manifest;
return [2];
}
});
}); });
return [4, tasks.run({ concurrent: false })];
case 10:
_d.sent();
if (!isPush) return [3, 12];
return [4, push.bundle({ config: config, version: version })];
case 11:
_d.sent();
return [3, 13];
case 12:
if (manifest) {
bundler_1.bundler.log.bundle({ bundleDir: bundleDir, manifest: manifest });
}
_d.label = 13;
case 13:
common_1.log.info();
return [2];
}
});
});
}
exports.run = run;
function execScript(pkg, e, scriptName) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var exists, cwd;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
exists = Boolean(pkg.scripts.bundle);
if (!exists) {
e.error("Package.json does not have a \"" + scriptName + "\" script.");
return [2];
}
cwd = pkg.dir;
return [4, common_1.exec.command("yarn " + scriptName).run({ cwd: cwd, silent: true })];
case 1:
_a.sent();
return [2];
}
});
});
}
var getRootDir = function (source) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var path;
var _this = this;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
path = '';
return [4, common_1.fs.ancestor(source).walk(function (e) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, common_1.fs.readdir(e.dir)];
case 1:
if ((_a.sent()).includes('package.json')) {
return [2, e.stop()];
}
else {
path = e.dir;
}
return [2];
}
});
}); })];
case 1:
_a.sent();
return [2, path];
}
});
}); };
var getEntries = function (config) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var source, sourceRoot, sourcePath, localRoot, localPath, err, res, error_1;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
source = config.builder.entries;
if (!source) {
return [2, []];
}
source = common_1.fs.resolve(source);
return [4, getRootDir(source)];
case 1:
sourceRoot = _a.sent();
sourcePath = source.substring(sourceRoot.length);
localRoot = common_1.fs.resolve("tmp/" + common_1.fs.basename(sourceRoot));
localPath = common_1.fs.join(localRoot, sourcePath);
return [4, common_1.fs.ensureDir(common_1.fs.dirname(localRoot))];
case 2:
_a.sent();
return [4, common_1.fs.remove(localRoot)];
case 3:
_a.sent();
return [4, common_1.fs.copy(sourceRoot, localRoot)];
case 4:
_a.sent();
err = "Failed to load bundle entries at path: " + source + ".";
_a.label = 5;
case 5:
_a.trys.push([5, 6, 7, 9]);
res = require(localPath);
if (Array.isArray(res.default)) {
return [2, res.default];
}
common_1.log.error(err + " Ensure the array is exported as the module default.");
return [2, []];
case 6:
error_1 = _a.sent();
common_1.log.error(err + " " + error_1.message);
return [2, []];
case 7: return [4, common_1.fs.remove(common_1.fs.resolve('tmp'))];
case 8:
_a.sent();
return [7];
case 9: return [2];
}
});
}); };