// 探针:SSR 渲染 PanelBlocks(分页块)验证真实渲染行为。 import { readFileSync } from 'node:fs' import { registerHooks } from 'node:module' import { parse, compileScript, compileTemplate } from '@vue/compiler-sfc' import { createSSRApp, h } from 'vue' import { renderToString } from '@vue/server-renderer' const root = new URL('../../../../../src/modules/cockpit/', import.meta.url) registerHooks({ load(url, context, nextLoad) { if (url.endsWith('.json')) return { format: 'module', shortCircuit: true, source: `export default ${readFileSync(new URL(url), 'utf8')}` } if (url.endsWith('.vue')) { const { descriptor } = parse(readFileSync(new URL(url), 'utf8'), { filename: new URL(url).pathname }) const script = compileScript(descriptor, { id: 'probe', genDefaultAs: '__sfc__' }) const template = compileTemplate({ source: descriptor.template.content, filename: new URL(url).pathname, id: 'probe', ssr: true, compilerOptions: { bindingMetadata: script.bindings }, }) return { format: 'module', shortCircuit: true, source: `${script.content}\n${template.code}\n__sfc__.ssrRender = ssrRender; export default __sfc__;` } } return nextLoad(url, context) }, }) const { default: PanelBlocks } = await import(new URL('components/PanelBlocks.vue', root)) const { operationPanel, managementPanel, toOperatingInformation, toManagementInformation } = await import(new URL('domain/infrastructurePanels.js', root)) const { section } = await import(new URL('domain/sectionState.js', root)) const payload = JSON.parse(readFileSync(new URL('../t51/oi-unfiltered.json', import.meta.url), 'utf8')).data const data = toOperatingInformation(payload) const view = operationPanel(data, section('success', { data, loaded: true }), { scope: 'all' }) const html = await renderToString(createSSRApp({ render: () => h(PanelBlocks, { view }) })) console.log('cards:', (html.match(/data-item="/g) || []).length) console.log('pager:', /data-pager/.test(html), 'state:', (html.match(/data-page-state>([^<]*) ({ district: i % 3, industry: 'GAS', facilityCount: 1, equipmentCount: 1 })) }) const mv = managementPanel(m, section('success', { data: m, loaded: true })) const mh = await renderToString(createSSRApp({ render: () => h(PanelBlocks, { view: mv }) })) console.log('mgmt cards:', (mh.match(/data-item="/g) || []).length, 'mgmt pager:', /data-pager/.test(mh))