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,19 +44,23 @@ export async function generateApp (nuxt: Nuxt, app: NuxtApp, options: { filter?:
.map(async (template) => { .map(async (template) => {
const fullPath = template.dst || resolve(nuxt.options.buildDir, template.filename!) const fullPath = template.dst || resolve(nuxt.options.buildDir, template.filename!)
const mark = performance.mark(fullPath) const mark = performance.mark(fullPath)
const oldContents = nuxt.vfs[fullPath]
const contents = await compileTemplate(template, templateContext).catch((e) => { const contents = await compileTemplate(template, templateContext).catch((e) => {
logger.error(`Could not compile template \`${template.filename}\`.`) logger.error(`Could not compile template \`${template.filename}\`.`)
throw e throw e
}) })
nuxt.vfs[fullPath] = contents template.modified = oldContents !== contents
if (template.modified) {
nuxt.vfs[fullPath] = contents
const aliasPath = '#build/' + template.filename!.replace(/\.\w+$/, '') const aliasPath = '#build/' + template.filename!.replace(/\.\w+$/, '')
nuxt.vfs[aliasPath] = contents nuxt.vfs[aliasPath] = contents
// In case a non-normalized absolute path is called for on Windows // In case a non-normalized absolute path is called for on Windows
if (process.platform === 'win32') { if (process.platform === 'win32') {
nuxt.vfs[fullPath.replace(/\//g, '\\')] = contents nuxt.vfs[fullPath.replace(/\//g, '\\')] = contents
}
} }
const perf = performance.measure(fullPath, mark?.name) // TODO: remove when Node 14 reaches EOL const perf = performance.measure(fullPath, mark?.name) // 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`) logger.info(`Compiled \`${template.filename}\` in ${setupTime}ms`)
} }
if (template.write) { if (template.modified && template.write) {
writes.push(() => { writes.push(() => {
mkdirSync(dirname(fullPath), { recursive: true }) mkdirSync(dirname(fullPath), { recursive: true })
writeFileSync(fullPath, contents, 'utf8') 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 // runtime overhead of cascading HMRs from vite/webpack
for (const write of writes) { write() } 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 */ /** @internal */

View File

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