diff --git a/packages/nuxt/src/core/app.ts b/packages/nuxt/src/core/app.ts index 67cc4d54c6..86b3e10b3c 100644 --- a/packages/nuxt/src/core/app.ts +++ b/packages/nuxt/src/core/app.ts @@ -44,19 +44,23 @@ 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 }) - nuxt.vfs[fullPath] = contents + template.modified = oldContents !== contents + if (template.modified) { + nuxt.vfs[fullPath] = contents - const aliasPath = '#build/' + template.filename!.replace(/\.\w+$/, '') - nuxt.vfs[aliasPath] = contents + const aliasPath = '#build/' + template.filename!.replace(/\.\w+$/, '') + nuxt.vfs[aliasPath] = contents - // In case a non-normalized absolute path is called for on Windows - if (process.platform === 'win32') { - nuxt.vfs[fullPath.replace(/\//g, '\\')] = contents + // In case a non-normalized absolute path is called for on Windows + if (process.platform === 'win32') { + nuxt.vfs[fullPath.replace(/\//g, '\\')] = contents + } } 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`) } - 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 */ diff --git a/packages/schema/src/types/nuxt.ts b/packages/schema/src/types/nuxt.ts index 144be97179..b37c37d900 100644 --- a/packages/schema/src/types/nuxt.ts +++ b/packages/schema/src/types/nuxt.ts @@ -45,6 +45,7 @@ export interface NuxtTemplate { export interface ResolvedNuxtTemplate extends NuxtTemplate { filename: string dst: string + modified?: boolean } export interface NuxtTypeTemplate extends Omit, 'write' | 'filename'> {