From 9be0e7b5196fb3ddd7a06b8bcd2c598d0e4aa360 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 17 Jan 2025 23:53:35 +0000 Subject: [PATCH] perf(nuxt): reduce number of `mkdirSync` calls (#30651) --- packages/nuxt/src/core/app.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/nuxt/src/core/app.ts b/packages/nuxt/src/core/app.ts index 887604d5e3..75248d9bfb 100644 --- a/packages/nuxt/src/core/app.ts +++ b/packages/nuxt/src/core/app.ts @@ -57,6 +57,7 @@ export async function generateApp (nuxt: Nuxt, app: NuxtApp, options: { filter?: const templateContext = { nuxt, app } const writes: Array<() => void> = [] + const dirs = new Set() const changedTemplates: Array> = [] const FORWARD_SLASH_RE = /\//g async function processTemplate (template: ResolvedNuxtTemplate) { @@ -92,10 +93,8 @@ export async function generateApp (nuxt: Nuxt, app: NuxtApp, options: { filter?: } if (template.modified && template.write) { - writes.push(() => { - mkdirSync(dirname(fullPath), { recursive: true }) - writeFileSync(fullPath, contents, 'utf8') - }) + dirs.add(dirname(fullPath)) + writes.push(() => writeFileSync(fullPath, contents, 'utf8')) } } @@ -104,7 +103,12 @@ export async function generateApp (nuxt: Nuxt, app: NuxtApp, options: { filter?: // Write template files in single synchronous step to avoid (possible) additional // runtime overhead of cascading HMRs from vite/webpack - for (const write of writes) { write() } + for (const dir of dirs) { + mkdirSync(dir, { recursive: true }) + } + for (const write of writes) { + write() + } if (changedTemplates.length) { await nuxt.callHook('app:templatesGenerated', app, changedTemplates, options)