From c315eb980506f22621d9185908032649fc9807bd 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 e538f72426..2f5e682374 100644 --- a/packages/nuxt/src/core/app.ts +++ b/packages/nuxt/src/core/app.ts @@ -59,6 +59,7 @@ export async function generateApp (nuxt: Nuxt, app: NuxtApp, options: { filter?: const compileTemplate = nuxt.options.experimental.compileTemplate ? _compileTemplate : futureCompileTemplate const writes: Array<() => void> = [] + const dirs = new Set() const changedTemplates: Array> = [] const FORWARD_SLASH_RE = /\//g async function processTemplate (template: ResolvedNuxtTemplate) { @@ -94,10 +95,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')) } } @@ -106,7 +105,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)