@platform/react.ssr
Version:
An SSR (server-side-render) system for react apps bundled with ParcelJS and hosted on S3.
118 lines (117 loc) • 5.12 kB
JavaScript
;
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 pushBundle = require("./cmd.pushBundle");
function run() {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var config, endpoint, manifest, version, bundleDir, push, tasks;
var _this = this;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, config_1.Config.create()];
case 1:
config = _a.sent();
endpoint = config.s3.endpoint;
return [4, common_1.npm.prompt.incrementVersion({ noChange: true, save: true })];
case 2:
version = _a.sent();
bundleDir = common_1.fs.resolve(common_1.fs.join(config.builder.bundles, version));
return [4, common_1.cli.prompt.list({ message: 'push to S3', items: ['yes', 'no'] })];
case 3:
push = (_a.sent()) === 'yes';
if (push && !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(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(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 4:
_a.sent();
if (!push) return [3, 6];
return [4, pushBundle.run({ version: version })];
case 5:
_a.sent();
return [3, 7];
case 6:
if (manifest) {
bundler_1.bundler.log.bundle({ bundleDir: bundleDir, manifest: manifest });
}
_a.label = 7;
case 7:
common_1.log.info();
return [2];
}
});
});
}
exports.run = run;
function execScript(e, scriptName) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var pkg, exists;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
pkg = common_1.npm.pkg();
exists = Boolean(pkg.scripts.bundle);
if (!exists) {
e.error("Package.json does not have a \"" + scriptName + "\" script.");
return [2];
}
return [4, common_1.exec.command("yarn " + scriptName).run({ silent: true })];
case 1:
_a.sent();
return [2];
}
});
});
}
var getEntries = function (config) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var path, err, res;
return tslib_1.__generator(this, function (_a) {
path = config.builder.entries;
if (!path) {
return [2, []];
}
path = common_1.fs.resolve(path);
err = "Failed to load bundle entries at path: " + path + ".";
try {
res = require(path);
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, []];
}
catch (error) {
common_1.log.error(err + " " + error.message);
return [2, []];
}
return [2];
});
}); };