mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
fix(nuxt): compile plugin templates last (#27009)
This commit is contained in:
parent
54a9914ee6
commit
933c0aa71f
@ -20,6 +20,12 @@ export function createApp (nuxt: Nuxt, options: Partial<NuxtApp> = {}): NuxtApp
|
||||
} as unknown as NuxtApp) as NuxtApp
|
||||
}
|
||||
|
||||
const postTemplates = [
|
||||
defaultTemplates.clientPluginTemplate.filename,
|
||||
defaultTemplates.serverPluginTemplate.filename,
|
||||
defaultTemplates.pluginsDeclaration.filename,
|
||||
]
|
||||
|
||||
export async function generateApp (nuxt: Nuxt, app: NuxtApp, options: { filter?: (template: ResolvedNuxtTemplate<any>) => boolean } = {}) {
|
||||
// Resolve app
|
||||
await resolveApp(nuxt, app)
|
||||
@ -33,17 +39,28 @@ export async function generateApp (nuxt: Nuxt, app: NuxtApp, options: { filter?:
|
||||
// Normalize templates
|
||||
app.templates = app.templates.map(tmpl => normalizeTemplate(tmpl))
|
||||
|
||||
// compile plugins first as they are needed within the nuxt.vfs
|
||||
// in order to annotate templated plugins
|
||||
const filteredTemplates: Record<'pre' | 'post', Array<ResolvedNuxtTemplate<any>>> = {
|
||||
pre: [],
|
||||
post: [],
|
||||
}
|
||||
|
||||
for (const template of app.templates as Array<ResolvedNuxtTemplate<any>>) {
|
||||
if (options.filter && !options.filter(template)) { continue }
|
||||
const key = template.filename && postTemplates.includes(template.filename) ? 'post' : 'pre'
|
||||
filteredTemplates[key].push(template)
|
||||
}
|
||||
|
||||
// Compile templates into vfs
|
||||
// TODO: remove utils in v4
|
||||
const templateContext = { utils: templateUtils, nuxt, app }
|
||||
const filteredTemplates = (app.templates as Array<ResolvedNuxtTemplate<any>>)
|
||||
.filter(template => !options.filter || options.filter(template))
|
||||
|
||||
const compileTemplate = nuxt.options.experimental.compileTemplate ? _compileTemplate : futureCompileTemplate
|
||||
|
||||
const writes: Array<() => void> = []
|
||||
await Promise.allSettled(filteredTemplates
|
||||
.map(async (template) => {
|
||||
const changedTemplates: Array<ResolvedNuxtTemplate<any>> = []
|
||||
|
||||
async function processTemplate (template: ResolvedNuxtTemplate) {
|
||||
const fullPath = template.dst || resolve(nuxt.options.buildDir, template.filename!)
|
||||
const mark = performance.mark(fullPath)
|
||||
const oldContents = nuxt.vfs[fullPath]
|
||||
@ -64,6 +81,8 @@ export async function generateApp (nuxt: Nuxt, app: NuxtApp, options: { filter?:
|
||||
if (process.platform === 'win32') {
|
||||
nuxt.vfs[fullPath.replace(/\//g, '\\')] = contents
|
||||
}
|
||||
|
||||
changedTemplates.push(template)
|
||||
}
|
||||
|
||||
const perf = performance.measure(fullPath, mark?.name) // TODO: remove when Node 14 reaches EOL
|
||||
@ -79,14 +98,15 @@ export async function generateApp (nuxt: Nuxt, app: NuxtApp, options: { filter?:
|
||||
writeFileSync(fullPath, contents, 'utf8')
|
||||
})
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
await Promise.allSettled(filteredTemplates.pre.map(processTemplate))
|
||||
await Promise.allSettled(filteredTemplates.post.map(processTemplate))
|
||||
|
||||
// Write template files in single synchronous step to avoid (possible) additional
|
||||
// runtime overhead of cascading HMRs from vite/webpack
|
||||
for (const write of writes) { write() }
|
||||
|
||||
const changedTemplates = filteredTemplates.filter(t => t.modified)
|
||||
|
||||
if (changedTemplates.length) {
|
||||
await nuxt.callHook('app:templatesGenerated', app, changedTemplates, options)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user