From 90e413e428d8a33fb0113961fa5fa08cbb37ad2d Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 8 Oct 2024 15:29:15 +0200 Subject: [PATCH] fix(nuxt): error on build when required module is missing (#29287) --- packages/nuxt/src/components/plugins/loader.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/nuxt/src/components/plugins/loader.ts b/packages/nuxt/src/components/plugins/loader.ts index 2eecc471b1..e8d6f3cd4b 100644 --- a/packages/nuxt/src/components/plugins/loader.ts +++ b/packages/nuxt/src/components/plugins/loader.ts @@ -2,7 +2,7 @@ import { createUnplugin } from 'unplugin' import { genDynamicImport, genImport } from 'knitwork' import MagicString from 'magic-string' import { pascalCase } from 'scule' -import { resolve } from 'pathe' +import { relative, resolve } from 'pathe' import type { Component, ComponentsOptions } from 'nuxt/schema' import { logger, tryUseNuxt } from '@nuxt/kit' @@ -21,6 +21,7 @@ export const LoaderPlugin = (options: LoaderOptions) => createUnplugin(() => { const exclude = options.transform?.exclude || [] const include = options.transform?.include || [] const serverComponentRuntime = resolve(distDir, 'components/runtime/server-component') + const nuxt = tryUseNuxt() return { name: 'nuxt:components-loader', @@ -34,7 +35,7 @@ export const LoaderPlugin = (options: LoaderOptions) => createUnplugin(() => { } return isVue(id, { type: ['template', 'script'] }) || !!id.match(/\.[tj]sx$/) }, - transform (code) { + transform (code, id) { const components = options.getComponents() let num = 0 @@ -46,10 +47,14 @@ export const LoaderPlugin = (options: LoaderOptions) => createUnplugin(() => { s.replace(/(?<=[ (])_?resolveComponent\(\s*["'](lazy-|Lazy(?=[A-Z]))?([^'"]*)["'][^)]*\)/g, (full: string, lazy: string, name: string) => { const component = findComponent(components, name, options.mode) if (component) { - // @ts-expect-error TODO: refactor to nuxi - if (component._internal_install && tryUseNuxt()?.options.test === false) { - // @ts-expect-error TODO: refactor to nuxi - import('../../core/features').then(({ installNuxtModule }) => installNuxtModule(component._internal_install)) + // TODO: refactor to nuxi + const internalInstall = ((component as any)._internal_install) as string + if (internalInstall && nuxt?.options.test === false) { + if (!nuxt.options.dev) { + const relativePath = relative(nuxt.options.rootDir, id) + throw new Error(`[nuxt] \`~/${relativePath}\` is using \`${component.pascalName}\` which requires \`${internalInstall}\``) + } + import('../../core/features').then(({ installNuxtModule }) => installNuxtModule(internalInstall)) } let identifier = map.get(component) || `__nuxt_component_${num++}` map.set(component, identifier)