Skip to content

Commit

Permalink
perf(nuxt): only update changed templates (#26250)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Mar 14, 2024
1 parent 5c6dc4c commit bd0e759
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/nuxt/src/core/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand All @@ -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 */
Expand Down
1 change: 1 addition & 0 deletions packages/schema/src/types/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'> {
Expand Down

0 comments on commit bd0e759

Please sign in to comment.