mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
perf(nuxt): only update changed templates (#26250)
This commit is contained in:
parent
5c6dc4c14e
commit
bd0e759b22
@ -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 */
|
||||
|
@ -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'> {
|
||||
|
Loading…
Reference in New Issue
Block a user