Skip to content

Commit 0404f24

Browse files
committed
chore: shave ~1-2s off pnpm test:types
`pnpm test:types` runs `nuxt prepare` before `vue-tsc`. In this repo, `nuxt prepare` does not just generate `.nuxt` files, it loads `nuxt.config.ts`, installs modules, and runs each module's `setup()`. The `blog` module's `setup()` calls `loadBlogPosts()`, which calls `fetchBlueskyAvatars()`, which hits the Bluesky API to fetch user avatars and writes those to disk. None of that is needed for type gen or type checking. The blog already gracefully handles missing avatars. We already had a pattern to conditionally skip expensive work during prepare. This uses the same pattern here. This does not break the actual blog build, because nuxt runs modules' `setup()` during build.
1 parent eb2e6bb commit 0404f24

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

modules/blog.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,14 @@ function resolveAuthors(authors: Author[], avatarMap: Map<string, string>): Reso
8787
* Returns all posts (including drafts) sorted by date descending.
8888
* Resolves Bluesky avatars at build time.
8989
*/
90-
async function loadBlogPosts(blogDir: string, imagesDir: string): Promise<BlogPostFrontmatter[]> {
90+
async function loadBlogPosts(
91+
blogDir: string,
92+
options: {
93+
imagesDir: string
94+
resolveAvatars: boolean
95+
},
96+
): Promise<BlogPostFrontmatter[]> {
97+
const { imagesDir, resolveAvatars } = options
9198
const files = await Array.fromAsync(glob(join(blogDir, '**/*.md').replace(/\\/g, '/')))
9299

93100
// First pass: extract raw frontmatter and collect all Bluesky handles
@@ -120,8 +127,10 @@ async function loadBlogPosts(blogDir: string, imagesDir: string): Promise<BlogPo
120127
rawPosts.push({ frontmatter })
121128
}
122129

123-
// Batch-fetch all Bluesky avatars in a single request
124-
const avatarMap = await fetchBlueskyAvatars(imagesDir, [...allHandles])
130+
// Batch-fetch all Bluesky avatars in a single request when avatar resolution is enabled.
131+
const avatarMap = resolveAvatars
132+
? await fetchBlueskyAvatars(imagesDir, [...allHandles])
133+
: new Map<string, string>()
125134

126135
// Second pass: validate with raw schema, then enrich authors with avatars
127136
const posts: BlogPostFrontmatter[] = []
@@ -150,13 +159,14 @@ export default defineNuxtModule({
150159
const resolver = createResolver(import.meta.url)
151160
const blogDir = resolver.resolve('../app/pages/blog')
152161
const blogImagesDir = resolver.resolve('../public/blog/avatar')
162+
const resolveAvatars = !nuxt.options._prepare
153163

154164
nuxt.options.extensions.push('.md')
155165
nuxt.options.vite.vue = defu(nuxt.options.vite.vue, {
156166
include: [/\.vue($|\?)/, /\.(md|markdown)($|\?)/],
157167
})
158168

159-
if (!existsSync(blogImagesDir)) {
169+
if (resolveAvatars && !existsSync(blogImagesDir)) {
160170
await mkdir(blogImagesDir, { recursive: true })
161171
}
162172

@@ -180,7 +190,10 @@ export default defineNuxtModule({
180190
)
181191

182192
// Load posts once with resolved Bluesky avatars (shared across template + route rules)
183-
const allPosts = await loadBlogPosts(blogDir, blogImagesDir)
193+
const allPosts = await loadBlogPosts(blogDir, {
194+
imagesDir: blogImagesDir,
195+
resolveAvatars,
196+
})
184197

185198
// Expose frontmatter for the `/blog` listing page.
186199
const showDrafts = nuxt.options.dev || !isProduction

0 commit comments

Comments
 (0)