mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-22 11:22:43 +00:00
perf(nuxt): reduce unnecessary template updating (#30684)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
492b1ec65b
commit
893ac0f8bc
@ -1,6 +1,6 @@
|
|||||||
import { existsSync, statSync, writeFileSync } from 'node:fs'
|
import { existsSync, statSync, writeFileSync } from 'node:fs'
|
||||||
import { isAbsolute, join, normalize, relative, resolve } from 'pathe'
|
import { isAbsolute, join, normalize, relative, resolve } from 'pathe'
|
||||||
import { addBuildPlugin, addPluginTemplate, addTemplate, addTypeTemplate, addVitePlugin, defineNuxtModule, findPath, resolveAlias, resolvePath, updateTemplates } from '@nuxt/kit'
|
import { addBuildPlugin, addPluginTemplate, addTemplate, addTypeTemplate, addVitePlugin, defineNuxtModule, findPath, resolveAlias, resolvePath } from '@nuxt/kit'
|
||||||
import type { Component, ComponentsDir, ComponentsOptions } from 'nuxt/schema'
|
import type { Component, ComponentsDir, ComponentsOptions } from 'nuxt/schema'
|
||||||
|
|
||||||
import { distDir } from '../dirs'
|
import { distDir } from '../dirs'
|
||||||
@ -198,24 +198,6 @@ export default defineNuxtModule<ComponentsOptions>({
|
|||||||
tsConfig.compilerOptions!.paths['#components'] = [resolve(nuxt.options.buildDir, 'components')]
|
tsConfig.compilerOptions!.paths['#components'] = [resolve(nuxt.options.buildDir, 'components')]
|
||||||
})
|
})
|
||||||
|
|
||||||
// Watch for changes
|
|
||||||
nuxt.hook('builder:watch', async (event, relativePath) => {
|
|
||||||
if (!['add', 'unlink'].includes(event)) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
const path = resolve(nuxt.options.srcDir, relativePath)
|
|
||||||
if (componentDirs.some(dir => path.startsWith(dir.path + '/'))) {
|
|
||||||
await updateTemplates({
|
|
||||||
filter: template => [
|
|
||||||
'components.plugin.mjs',
|
|
||||||
'components.d.ts',
|
|
||||||
'components.server.mjs',
|
|
||||||
'components.client.mjs',
|
|
||||||
].includes(template.filename),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if (nuxt.options.experimental.treeshakeClientOnly) {
|
if (nuxt.options.experimental.treeshakeClientOnly) {
|
||||||
addBuildPlugin(TreeShakeTemplatePlugin({ sourcemap: !!nuxt.options.sourcemap.server, getComponents }), { client: false })
|
addBuildPlugin(TreeShakeTemplatePlugin({ sourcemap: !!nuxt.options.sourcemap.server, getComponents }), { client: false })
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { existsSync, readdirSync } from 'node:fs'
|
import { existsSync, readdirSync } from 'node:fs'
|
||||||
import { mkdir, readFile } from 'node:fs/promises'
|
import { mkdir, readFile } from 'node:fs/promises'
|
||||||
import { addBuildPlugin, addComponent, addPlugin, addTemplate, addTypeTemplate, defineNuxtModule, findPath, resolvePath, updateTemplates, useNitro } from '@nuxt/kit'
|
import { addBuildPlugin, addComponent, addPlugin, addTemplate, addTypeTemplate, defineNuxtModule, findPath, resolvePath, useNitro } from '@nuxt/kit'
|
||||||
import { dirname, join, relative, resolve } from 'pathe'
|
import { dirname, join, relative, resolve } from 'pathe'
|
||||||
import { genImport, genObjectFromRawEntries, genString } from 'knitwork'
|
import { genImport, genObjectFromRawEntries, genString } from 'knitwork'
|
||||||
import { joinURL } from 'ufo'
|
import { joinURL } from 'ufo'
|
||||||
@ -93,10 +93,8 @@ export default defineNuxtModule({
|
|||||||
addPlugin(resolve(runtimeDir, 'plugins/check-if-page-unused'))
|
addPlugin(resolve(runtimeDir, 'plugins/check-if-page-unused'))
|
||||||
}
|
}
|
||||||
|
|
||||||
nuxt.hook('app:templates', async (app) => {
|
nuxt.hook('app:templates', (app) => {
|
||||||
app.pages = await resolvePagesRoutes(nuxt)
|
if (!nuxt.options.ssr && app.pages?.some(p => p.mode === 'server')) {
|
||||||
|
|
||||||
if (!nuxt.options.ssr && app.pages.some(p => p.mode === 'server')) {
|
|
||||||
logger.warn('Using server pages with `ssr: false` is not supported with auto-detected component islands. Set `experimental.componentIslands` to `true`.')
|
logger.warn('Using server pages with `ssr: false` is not supported with auto-detected component islands. Set `experimental.componentIslands` to `true`.')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -264,6 +262,13 @@ export default defineNuxtModule({
|
|||||||
if (!pages) { return false }
|
if (!pages) { return false }
|
||||||
return pages.some(page => page.file === file) || pages.some(page => page.children && isPage(file, page.children))
|
return pages.some(page => page.file === file) || pages.some(page => page.children && isPage(file, page.children))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nuxt.hooks.hookOnce('app:templates', async (app) => {
|
||||||
|
if (!app.pages) {
|
||||||
|
app.pages = await resolvePagesRoutes(nuxt)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
nuxt.hook('builder:watch', async (event, relativePath) => {
|
nuxt.hook('builder:watch', async (event, relativePath) => {
|
||||||
const path = resolve(nuxt.options.srcDir, relativePath)
|
const path = resolve(nuxt.options.srcDir, relativePath)
|
||||||
const shouldAlwaysRegenerate = nuxt.options.experimental.scanPageMeta && isPage(path)
|
const shouldAlwaysRegenerate = nuxt.options.experimental.scanPageMeta && isPage(path)
|
||||||
@ -271,9 +276,7 @@ export default defineNuxtModule({
|
|||||||
if (event === 'change' && !shouldAlwaysRegenerate) { return }
|
if (event === 'change' && !shouldAlwaysRegenerate) { return }
|
||||||
|
|
||||||
if (shouldAlwaysRegenerate || updateTemplatePaths.some(dir => path.startsWith(dir))) {
|
if (shouldAlwaysRegenerate || updateTemplatePaths.some(dir => path.startsWith(dir))) {
|
||||||
await updateTemplates({
|
nuxt.apps.default!.pages = await resolvePagesRoutes(nuxt)
|
||||||
filter: template => template.filename === 'routes.mjs',
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user