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