@platform/react.ssr
Version:
An SSR (server-side-render) system for react apps bundled with ParcelJS and hosted on S3.
63 lines (62 loc) • 2.13 kB
JavaScript
import { bundler } from '../bundler';
import { Config } from '../config';
import { cli, exec, fs, log, npm } from './common';
import * as pushBundle from './cmd.pushBundle';
export async function run() {
const config = await Config.create();
const { endpoint } = config.s3;
let manifest;
const version = await npm.prompt.incrementVersion({ noChange: true, save: true });
const bundleDir = fs.resolve(fs.join(config.builder.bundles, version));
const push = (await cli.prompt.list({ message: 'push to S3', items: ['yes', 'no'] })) === 'yes';
if (push && !endpoint) {
throw new Error(`The S3 endpoint has not been configured in [ssr.yml].`);
}
log.info();
const tasks = cli
.tasks()
.task('build', async (e) => execScript(e, 'build'))
.task('bundle', async (e) => execScript(e, 'bundle'))
.task('manifest', async (e) => {
const entries = await getEntries(config);
const res = await bundler.prepare({ bundleDir, entries, silent: true });
manifest = res.manifest;
});
await tasks.run({ concurrent: false });
if (push) {
await pushBundle.run({ version });
}
else if (manifest) {
bundler.log.bundle({ bundleDir, manifest });
}
log.info();
}
async function execScript(e, scriptName) {
const pkg = npm.pkg();
const exists = Boolean(pkg.scripts.bundle);
if (!exists) {
e.error(`Package.json does not have a "${scriptName}" script.`);
return;
}
await exec.command(`yarn ${scriptName}`).run({ silent: true });
}
const getEntries = async (config) => {
let path = config.builder.entries;
if (!path) {
return [];
}
path = fs.resolve(path);
const err = `Failed to load bundle entries at path: ${path}.`;
try {
const res = require(path);
if (Array.isArray(res.default)) {
return res.default;
}
log.error(`${err} Ensure the array is exported as the module default.`);
return [];
}
catch (error) {
log.error(`${err} ${error.message}`);
return [];
}
};