seia.js
Version:
Lightweight SSR framework for React Server Components
31 lines (30 loc) • 1.19 kB
JavaScript
import { join } from 'node:path';
import { parentPort, workerData } from 'node:worker_threads';
import { renderToReadableStream } from 'react-server-dom-webpack/server.edge';
import { jsx } from 'react/jsx-runtime';
import { changeExtension } from './utils-path.js';
import { mustParseAnchorId, trimPrefix } from './utils.js';
import './webpack-global.js';
const { anchorId, config: { root, paths: { src, dist, rsc } } } = workerData;
const { path, anchor } = mustParseAnchorId(anchorId);
const componentPath = join(root, dist, rsc, path);
const component = (await import(componentPath))[anchor];
const rs = renderToReadableStream(jsx(component, {}), new Proxy({}, {
get (_, anchorId) {
const { path, anchor } = mustParseAnchorId(anchorId);
const relativePath = '.' + changeExtension(trimPrefix(path, join(root, src)), '.js');
return {
id: relativePath,
name: anchor,
chunks: [
`${relativePath}#${anchor}`
],
async: true
};
}
}));
parentPort?.postMessage(rs, [
rs
]);
// eslint-disable-next-line @typescript-eslint/no-empty-function
parentPort?.on('message', ()=>{});