perf(nuxt): only update changed templates (#26250)

This commit is contained in:
Anthony Fu 2024-03-14 19:56:17 +01:00 committed by GitHub
parent 5c6dc4c14e
commit bd0e759b22
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 8 deletions

View File

@ -44,11 +44,14 @@ export async function generateApp (nuxt: Nuxt, app: NuxtApp, options: { filter?:
.map(async (template) => {
const fullPath = template.dst || resolve(nuxt.options.buildDir, template.filename!)
const mark = performance.mark(fullPath)
const oldContents = nuxt.vfs[fullPath]
const contents = await compileTemplate(template, templateContext).catch((e) => {
logger.error(`Could not compile template \`${template.filename}\`.`)
throw e
})
template.modified = oldContents !== contents
if (template.modified) {
nuxt.vfs[fullPath] = contents
const aliasPath = '#build/' + template.filename!.replace(/\.\w+$/, '')
@ -58,6 +61,7 @@ export async function generateApp (nuxt: Nuxt, app: NuxtApp, options: { filter?:
if (process.platform === 'win32') {
nuxt.vfs[fullPath.replace(/\//g, '\\')] = contents
}
}
const perf = performance.measure(fullPath, mark?.name) // TODO: remove when Node 14 reaches EOL
const setupTime = perf ? Math.round((perf.duration * 100)) / 100 : 0 // TODO: remove when Node 14 reaches EOL
@ -66,7 +70,7 @@ export async function generateApp (nuxt: Nuxt, app: NuxtApp, options: { filter?:
logger.info(`Compiled \`${template.filename}\` in ${setupTime}ms`)
}
if (template.write) {
if (template.modified && template.write) {
writes.push(() => {
mkdirSync(dirname(fullPath), { recursive: true })
writeFileSync(fullPath, contents, 'utf8')
@ -78,7 +82,11 @@ export async function generateApp (nuxt: Nuxt, app: NuxtApp, options: { filter?:
// runtime overhead of cascading HMRs from vite/webpack
for (const write of writes) { write() }
await nuxt.callHook('app:templatesGenerated', app, filteredTemplates, options)
const changedTemplates = filteredTemplates.filter(t => t.modified)
if (changedTemplates.length) {
await nuxt.callHook('app:templatesGenerated', app, changedTemplates, options)
}
}
/** @internal */

View File

@ -45,6 +45,7 @@ export interface NuxtTemplate<Options = TemplateDefaultOptions> {
export interface ResolvedNuxtTemplate<Options = TemplateDefaultOptions> extends NuxtTemplate<Options> {
filename: string
dst: string
modified?: boolean
}
export interface NuxtTypeTemplate<Options = TemplateDefaultOptions> extends Omit<NuxtTemplate<Options>, 'write' | 'filename'> {