From 3fe271f400325d6e792304726cfd984c4bd2176d Mon Sep 17 00:00:00 2001 From: Berke Güzel Date: Mon, 9 Feb 2026 00:17:47 +0300 Subject: better optimized builds? --- src/lib/utils/posts.ts | 63 +++++++++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 37 deletions(-) (limited to 'src/lib/utils/posts.ts') diff --git a/src/lib/utils/posts.ts b/src/lib/utils/posts.ts index cc05c83..d0eca68 100644 --- a/src/lib/utils/posts.ts +++ b/src/lib/utils/posts.ts @@ -1,51 +1,40 @@ import type { Component } from 'svelte'; export interface PostMetadata { - title: string; - date: string; - description: string; - tags?: string[]; + title: string; + date: string; + description: string; + tags?: string[]; } export interface Post extends PostMetadata { - slug: string; + slug: string; } export interface PostModule { - default: Component; - metadata: PostMetadata; + default: Component; + metadata: PostMetadata; } -// Use Vite's glob import to get all .svx files from posts directory -const postFiles = import.meta.glob('/src/posts/*.svx', { eager: true }); - -/** - * Get all posts, sorted by date (newest first) - */ -export function getPosts(): Post[] { - const posts: Post[] = []; - - for (const path in postFiles) { - const file = postFiles[path]; - // Extract slug from path: /src/posts/hello-world.svx -> hello-world - const slug = path.split('/').pop()?.replace('.svx', '') ?? ''; - - if (file.metadata) { - posts.push({ - ...file.metadata, - slug - }); - } - } - - // Sort by date, newest first - return posts.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()); +const postLoaders = import.meta.glob('/src/posts/*.svx'); + +function getSlugFromPath(path: string): string { + return path.split('/').pop()?.replace('.svx', '') ?? ''; +} + +export function getPostSlugs(): string[] { + return Object.keys(postLoaders) + .map((path) => getSlugFromPath(path)) + .sort(); } -/** - * Get a single post by slug - */ -export function getPost(slug: string): PostModule | undefined { - const path = `/src/posts/${slug}.svx`; - return postFiles[path]; +export async function getPost(slug: string): Promise { + const path = `/src/posts/${slug}.svx`; + const loader = postLoaders[path]; + + if (!loader) { + return undefined; + } + + return loader(); } -- cgit v1.2.3