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)