@lynx-js/web-core
Version:
This is an internal experimental package, do not use
72 lines • 3.02 kB
JavaScript
import * as vm from 'vm';
import { decodeTemplate } from './decode.js';
import { createElementAPI, } from './elementAPIs/createElementAPI.js';
import { createServerLynx } from './createServerLynx.js';
export function executeTemplate(templateBuffer, initData, globalProps, _initI18nResources, transformVW, transformVH, viewAttributes, transformREM) {
const result = decodeTemplate(templateBuffer, transformVW, transformVH, !!transformREM);
const config = result.config;
const binding = { ssrResult: '' };
const { globalThisAPIs: elementAPIs } = createElementAPI(binding, result.styleInfo, viewAttributes ?? '', {
enableCSSSelector: config['enableCSSSelector'] === 'true',
defaultOverflowVisible: config['defaultOverflowVisible'] === 'true',
defaultDisplayLinear: config['defaultDisplayLinear'] !== 'false', // Default to true if not present or 'true'
transformVW: transformVW,
transformVH: transformVH,
transformREM: !!transformREM,
});
const sandbox = {
module: { exports: {} },
exports: {},
console: console,
// Mock globals to match client environment if needed
setTimeout: setTimeout,
clearTimeout: clearTimeout,
lynx: createServerLynx(globalProps, result.customSections),
__OnLifecycleEvent: () => { },
_ReportError: (err) => {
console.error(err);
},
...elementAPIs,
__QueryComponent: () => {
return null;
},
};
const context = vm.createContext(sandbox);
// Style Info block removed as it is passed to createElementAPI
// Lepus Code
const rootCodeBuf = result.lepusCode['root'];
if (rootCodeBuf) {
const rootCode = new TextDecoder('utf-8').decode(rootCodeBuf);
const isLazy = config['isLazy'] === 'true';
const wrappedCode = `
(function() {
"use strict";
const navigator = undefined;
const postMessage = undefined;
const window = undefined;
${isLazy ? 'module.exports =' : ''}
${rootCode}
})()
`;
// Execute root code
// This execution should trigger the assignment of globalThis.renderPage,
// which in turn triggers our setter, queues the microtask.
vm.runInContext(wrappedCode, context, {
filename: `root`,
});
const renderPageFunction = sandbox['renderPage'];
if (typeof renderPageFunction === 'function') {
const processData = sandbox['processData'];
const processedData = (config['enableJSDataProcessor'] !== 'true'
&& config['enableJSDataProcessor'] !== true)
&& processData
? processData(initData)
: initData;
renderPageFunction(processedData);
elementAPIs.__FlushElementTree();
return binding.ssrResult;
}
}
return undefined;
}
//# sourceMappingURL=deploy.js.map