From 9275b2a7be021583dc2c49a59b752f68b6e0da94 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 2 Dec 2024 14:14:08 +0000 Subject: [PATCH] fix(kit,nuxt): provide `buildDir` to `normalizeTemplate` (#30115) --- packages/kit/src/template.ts | 15 ++++++--------- packages/nuxt/src/core/app.ts | 2 +- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/packages/kit/src/template.ts b/packages/kit/src/template.ts index dcccc878e8..1b3f97360e 100644 --- a/packages/kit/src/template.ts +++ b/packages/kit/src/template.ts @@ -23,8 +23,7 @@ export function addTemplate (_template: NuxtTemplate | string) { const template = normalizeTemplate(_template) // Remove any existing template with the same destination path - nuxt.options.build.templates = nuxt.options.build.templates - .filter(p => normalizeTemplate(p).dst !== template.dst) + nuxt.options.build.templates = nuxt.options.build.templates.filter(p => normalizeTemplate(p).dst !== template.dst) // Add to templates array nuxt.options.build.templates.push(template) @@ -68,7 +67,7 @@ export function addTypeTemplate (_template: NuxtTypeTemplate) { /** * Normalize a nuxt template object */ -export function normalizeTemplate (template: NuxtTemplate | string): ResolvedNuxtTemplate { +export function normalizeTemplate (template: NuxtTemplate | string, buildDir?: string): ResolvedNuxtTemplate { if (!template) { throw new Error('Invalid template: ' + JSON.stringify(template)) } @@ -87,17 +86,16 @@ export function normalizeTemplate (template: NuxtTemplate | string): Resol } if (!template.filename) { const srcPath = parse(template.src) - template.filename = (template as any).fileName || - `${basename(srcPath.dir)}.${srcPath.name}.${hash(template.src)}${srcPath.ext}` + template.filename = (template as any).fileName || `${basename(srcPath.dir)}.${srcPath.name}.${hash(template.src)}${srcPath.ext}` } } if (!template.src && !template.getContents) { - throw new Error('Invalid template. Either getContents or src options should be provided: ' + JSON.stringify(template)) + throw new Error('Invalid template. Either `getContents` or `src` should be provided: ' + JSON.stringify(template)) } if (!template.filename) { - throw new Error('Invalid template. Either filename should be provided: ' + JSON.stringify(template)) + throw new Error('Invalid template. `filename` must be provided: ' + JSON.stringify(template)) } // Always write declaration files @@ -107,8 +105,7 @@ export function normalizeTemplate (template: NuxtTemplate | string): Resol // Resolve dst if (!template.dst) { - const nuxt = useNuxt() - template.dst = resolve(nuxt.options.buildDir, template.filename) + template.dst = resolve(buildDir ?? useNuxt().options.buildDir, template.filename) } return template as ResolvedNuxtTemplate diff --git a/packages/nuxt/src/core/app.ts b/packages/nuxt/src/core/app.ts index 4aebfd2902..bc1ff0cb66 100644 --- a/packages/nuxt/src/core/app.ts +++ b/packages/nuxt/src/core/app.ts @@ -37,7 +37,7 @@ export async function generateApp (nuxt: Nuxt, app: NuxtApp, options: { filter?: await nuxt.callHook('app:templates', app) // Normalize templates - app.templates = app.templates.map(tmpl => normalizeTemplate(tmpl)) + app.templates = app.templates.map(tmpl => normalizeTemplate(tmpl, nuxt.options.buildDir)) // compile plugins first as they are needed within the nuxt.vfs // in order to annotate templated plugins