Skip to content

Commit 812c555

Browse files
committed
fix(lint): add node:process import in blog index.ts
1 parent 4a38dba commit 812c555

4 files changed

Lines changed: 72 additions & 4 deletions

File tree

packages/blog/src/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { Plugin } from 'vite';
1111
import type { LessBlogOptions } from './types.ts';
1212
import { initBlogData } from './blog-data.ts';
1313
import { createLogger } from '@lessjs/core/logger';
14+
import process from 'node:process';
1415

1516
const log = createLogger('blog');
1617

@@ -57,6 +58,25 @@ export function lessBlog(options?: LessBlogOptions): Plugin {
5758
log.info(
5859
`${postCount} post(s) found in ${contentDir}, base path: ${basePath}`,
5960
);
61+
62+
// Write blog options to .less/ so Phase 3 (build-ssg) can re-initialize
63+
// the blog data store in its own Vite SSR server instance.
64+
// Phase 1 and Phase 3 use different Vite instances with separate module graphs,
65+
// so the blog data store populated here is not visible in Phase 3.
66+
try {
67+
const { mkdirSync, writeFileSync } = await import('node:fs');
68+
const { join } = await import('node:path');
69+
const root = process.cwd();
70+
const lessDir = join(root, '.less');
71+
mkdirSync(lessDir, { recursive: true });
72+
writeFileSync(
73+
join(lessDir, 'blog-options.json'),
74+
JSON.stringify({ contentDir, basePath }),
75+
'utf-8',
76+
);
77+
} catch {
78+
// Non-fatal — dynamic route expansion will be skipped if options missing
79+
}
6080
},
6181

6282
config() {

packages/core/src/build-context.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export class LessBuildContext {
4242
*/
4343
userResolveAlias: Record<string, string> | Alias[] | null = null;
4444

45+
/** Blog plugin options (contentDir, basePath) for SSG dynamic route expansion */
46+
blogOptions: { contentDir?: string; basePath?: string } | null = null;
47+
4548
/** Resolved framework options with defaults applied */
4649
readonly options: FrameworkOptions;
4750

packages/core/src/build.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ export function buildPlugin(options: FrameworkOptions = {}, ctx?: LessBuildConte
110110
// upgradeStrategy controls when island modules are imported.
111111
// It is an upgrade timing hint, not a client render runtime.
112112
upgradeStrategy: options.island?.upgradeStrategy || 'lazy',
113+
// Blog plugin options for Phase 3 dynamic route expansion
114+
blogOptions: ctx?.blogOptions || null,
113115
};
114116
const metadataPath = join(lessTmpDir, 'build-metadata.json');
115117
writeFileSync(metadataPath, JSON.stringify(metadata, null, 2), 'utf-8');

packages/core/src/cli/build-ssg.ts

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,52 @@ async function buildSSG(options: BuildSSGOptions = {}): Promise<void> {
273273
(r) => r.type === 'page' && !r.special && r.path.includes(':'),
274274
);
275275

276+
log.info(
277+
`Dynamic routes found: ${dynamicRoutes.length}${
278+
dynamicRoutes.length > 0 ? ` (${dynamicRoutes.map((r) => r.path).join(', ')})` : ''
279+
}`,
280+
);
281+
276282
if (dynamicRoutes.length > 0) {
277-
const { renderDSD } = await import('../render-dsd.js');
278-
const { wrapInDocument } = await import('../ssr-handler.js');
283+
// Use server.ssrLoadModule() to ensure adapter registration is visible.
284+
// Direct import() creates a separate module graph where
285+
// installLitAdapter()'s registerAdapter() call is not visible.
286+
const renderDsdMod = await server.ssrLoadModule(
287+
'@lessjs/core/render-dsd',
288+
) as Record<string, unknown>;
289+
const ssrHandlerMod = await server.ssrLoadModule(
290+
'@lessjs/core/less-runtime',
291+
) as Record<string, unknown>;
292+
const renderDSDFn = renderDsdMod.renderDSD as typeof import('../render-dsd.js').renderDSD;
293+
const wrapInDocumentFn = ssrHandlerMod
294+
.wrapInDocument as typeof import('../ssr-handler.js').wrapInDocument;
295+
296+
// Initialize @lessjs/blog data store if present.
297+
// The blog plugin's buildStart() ran in Phase 1's Vite instance,
298+
// but Phase 3 creates a fresh SSR server with its own module graph.
299+
// We need to re-initialize so getPosts() returns data.
300+
let blogOptions: { contentDir?: string; basePath?: string } | undefined;
301+
try {
302+
const blogOptsPath = join(root, '.less', 'blog-options.json');
303+
if (existsSync(blogOptsPath)) {
304+
const raw = readFileSync(blogOptsPath, 'utf-8');
305+
blogOptions = JSON.parse(raw);
306+
}
307+
} catch {
308+
// Non-fatal
309+
}
310+
311+
try {
312+
const blogModule = await server.ssrLoadModule('@lessjs/blog') as Record<string, unknown>;
313+
if (typeof blogModule.initBlogData === 'function') {
314+
await (blogModule.initBlogData as (opts?: unknown) => Promise<unknown>)(blogOptions);
315+
const postCount = (blogModule.getPosts as () => unknown[])().length;
316+
log.info(`Blog data store initialized: ${postCount} post(s) for SSG dynamic routes`);
317+
}
318+
} catch {
319+
// @lessjs/blog not available — non-fatal, dynamic routes may not use it
320+
log.debug('@lessjs/blog not found — skipping blog data initialization');
321+
}
279322

280323
for (const route of dynamicRoutes) {
281324
const paramNames = [...route.path.matchAll(/:([^/]+)/g)].map((m) => m[1]);
@@ -328,12 +371,12 @@ async function buildSSG(options: BuildSSGOptions = {}): Promise<void> {
328371

329372
try {
330373
// Render the component with route params as props
331-
const html = await renderDSD(tagName, ComponentClass, params, {
374+
const html = await renderDSDFn(tagName, ComponentClass, params, {
332375
route: resolvedPath,
333376
source: route.filePath,
334377
});
335378

336-
const fullHtml = wrapInDocument(html, {
379+
const fullHtml = wrapInDocumentFn(html, {
337380
title: options.html?.title || 'LessJS',
338381
lang: options.html?.lang || 'en',
339382
headExtras: options.headExtras || '',

0 commit comments

Comments
 (0)