react-app-renderer
Version:
61 lines • 2.48 kB
JavaScript
import * as React from 'react';
import * as ReactDOMServer from 'react-dom/server';
import { ChunkExtractor } from '@loadable/server';
import { getRenderApp } from './renderer';
function renderInServer(context, options) {
var appConfig = options.appConfig, _a = options.buildConfig, buildConfig = _a === void 0 ? {} : _a, appLifecycle = options.appLifecycle;
var createBaseApp = appLifecycle.createBaseApp, emitLifeCycles = appLifecycle.emitLifeCycles;
var _b = createBaseApp(appConfig, buildConfig, context), runtime = _b.runtime, modifiedAppConfig = _b.appConfig;
var loadableStatsPath = buildConfig.loadableStatsPath, publicPath = buildConfig.publicPath;
options.appConfig = modifiedAppConfig;
// Emit app launch cycle
emitLifeCycles();
var App = getRenderApp(runtime, options);
if (loadableStatsPath) {
var webExtractor = new ChunkExtractor({
statsFile: loadableStatsPath,
entrypoints: ['index'],
publicPath: publicPath
});
var jsx = webExtractor.collectChunks(React.createElement(App, null));
return {
bundleContent: ReactDOMServer.renderToString(jsx),
loadableComponentExtractor: webExtractor
};
}
else {
return {
bundleContent: ReactDOMServer.renderToString(React.createElement(App, null))
};
}
}
export default function reactAppRendererWithSSR(context, options) {
var cloneOptions = deepClone(options);
var appConfig = (cloneOptions || {}).appConfig;
if (context.enableRouter) {
appConfig.router = appConfig.router || {};
if (appConfig.router.type !== 'browser') {
throw new Error('[SSR]: Only support BrowserRouter when using SSR. You should set the router type to "browser". For more detail, please visit https://ice.work/docs/guide/basic/router');
}
appConfig.router.type = 'static';
}
return renderInServer(context, cloneOptions);
}
function deepClone(config) {
if (typeof config !== 'object' || config === null) {
return config;
}
if (Array.isArray(config)) {
return config.slice();
}
else {
var ret_1 = {};
Object.getOwnPropertyNames(config).forEach(function (key) {
if (Object.prototype.hasOwnProperty.call(config, key)) {
ret_1[key] = deepClone(config[key]);
}
});
return ret_1;
}
}
//# sourceMappingURL=server.js.map