perf(nuxt): reduce number of mkdirSync calls (#30651)

This commit is contained in:
Daniel Roe 2025-01-17 23:53:35 +00:00
parent 47c40f310a
commit c315eb9805
No known key found for this signature in database
GPG Key ID: 3714AB03996F442B

View File

@ -59,6 +59,7 @@ export async function generateApp (nuxt: Nuxt, app: NuxtApp, options: { filter?:
const compileTemplate = nuxt.options.experimental.compileTemplate ? _compileTemplate : futureCompileTemplate const compileTemplate = nuxt.options.experimental.compileTemplate ? _compileTemplate : futureCompileTemplate
const writes: Array<() => void> = [] const writes: Array<() => void> = []
const dirs = new Set<string>()
const changedTemplates: Array<ResolvedNuxtTemplate<any>> = [] const changedTemplates: Array<ResolvedNuxtTemplate<any>> = []
const FORWARD_SLASH_RE = /\//g const FORWARD_SLASH_RE = /\//g
async function processTemplate (template: ResolvedNuxtTemplate) { async function processTemplate (template: ResolvedNuxtTemplate) {
@ -94,10 +95,8 @@ export async function generateApp (nuxt: Nuxt, app: NuxtApp, options: { filter?:
} }
if (template.modified && template.write) { if (template.modified && template.write) {
writes.push(() => { dirs.add(dirname(fullPath))
mkdirSync(dirname(fullPath), { recursive: true }) writes.push(() => writeFileSync(fullPath, contents, 'utf8'))
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 // Write template files in single synchronous step to avoid (possible) additional
// runtime overhead of cascading HMRs from vite/webpack // 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) { if (changedTemplates.length) {
await nuxt.callHook('app:templatesGenerated', app, changedTemplates, options) await nuxt.callHook('app:templatesGenerated', app, changedTemplates, options)