feat(cli-serve): serve custom snapshots (#283)

This commit is contained in:
Miralem Drek
2020-01-29 14:07:45 +01:00
committed by GitHub
parent 970d33c8c8
commit 10a96ae069
4 changed files with 16 additions and 2 deletions

View File

@@ -256,6 +256,7 @@ const Cell = forwardRef(({ corona, model, initialSnOptions, initialError, onMoun
clonedLayout = (await state.sn.component.setSnapshotData(clonedLayout)) || clonedLayout;
}
return {
// TODO - this snapshot format needs to be documented and governed
key: String(+Date.now()),
meta: {
language: translator.language(),

View File

@@ -50,6 +50,9 @@ function snapshooter({ snapshotUrl, chrome = {} } = {}) {
getStoredSnapshot(id) {
return snapshots[id];
},
getStoredSnapshots() {
return Object.keys(snapshots).map(key => snapshots[key]);
},
storeSnapshot(snapshot) {
if (!snapshot) {
throw new Error('Empty snapshot');

View File

@@ -50,5 +50,9 @@ module.exports = function router({ base, snapshotUrl, snapshooter }) {
}
});
r.get('/snapshots', (req, res) => {
res.json(snapshooter.getStoredSnapshots());
});
return r;
};

View File

@@ -6,7 +6,7 @@ const express = require('express');
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const snapshooter = require('@nebula.js/snapshooter');
const snapshooterFn = require('@nebula.js/snapshooter');
const snapshotRouter = require('./snapshot-router');
module.exports = async ({
@@ -27,10 +27,16 @@ module.exports = async ({
const snapshotRoute = '/njs';
const snapshooter = snapshooterFn({ snapshotUrl: `http://${host}:${port}/eRender.html` });
(serveConfig.snapshots || []).forEach(s => {
snapshooter.storeSnapshot(s);
});
const snapRouter = snapshotRouter({
base: `http://${host}:${port}${snapshotRoute}`,
snapshotUrl: `http://${host}:${port}/eRender.html`,
snapshooter: snapshooter({ snapshotUrl: `http://${host}:${port}/eRender.html` }),
snapshooter,
});
const themes = serveConfig.themes || [];