fix(nuxt): error on build when required module is missing (#29287)

This commit is contained in:
Daniel Roe 2024-10-08 15:29:15 +02:00 committed by GitHub
parent b9f2ce0f42
commit 0cc2890561
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,7 +2,7 @@ import { createUnplugin } from 'unplugin'
import { genDynamicImport, genImport } from 'knitwork' import { genDynamicImport, genImport } from 'knitwork'
import MagicString from 'magic-string' import MagicString from 'magic-string'
import { pascalCase } from 'scule' import { pascalCase } from 'scule'
import { resolve } from 'pathe' import { relative, resolve } from 'pathe'
import type { Component, ComponentsOptions } from 'nuxt/schema' import type { Component, ComponentsOptions } from 'nuxt/schema'
import { logger, tryUseNuxt } from '@nuxt/kit' import { logger, tryUseNuxt } from '@nuxt/kit'
@ -21,6 +21,7 @@ export const LoaderPlugin = (options: LoaderOptions) => createUnplugin(() => {
const exclude = options.transform?.exclude || [] const exclude = options.transform?.exclude || []
const include = options.transform?.include || [] const include = options.transform?.include || []
const serverComponentRuntime = resolve(distDir, 'components/runtime/server-component') const serverComponentRuntime = resolve(distDir, 'components/runtime/server-component')
const nuxt = tryUseNuxt()
return { return {
name: 'nuxt:components-loader', name: 'nuxt:components-loader',
@ -34,7 +35,7 @@ export const LoaderPlugin = (options: LoaderOptions) => createUnplugin(() => {
} }
return isVue(id, { type: ['template', 'script'] }) || !!id.match(/\.[tj]sx$/) return isVue(id, { type: ['template', 'script'] }) || !!id.match(/\.[tj]sx$/)
}, },
transform (code) { transform (code, id) {
const components = options.getComponents() const components = options.getComponents()
let num = 0 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) => { s.replace(/(?<=[ (])_?resolveComponent\(\s*["'](lazy-|Lazy(?=[A-Z]))?([^'"]*)["'][^)]*\)/g, (full: string, lazy: string, name: string) => {
const component = findComponent(components, name, options.mode) const component = findComponent(components, name, options.mode)
if (component) { if (component) {
// @ts-expect-error TODO: refactor to nuxi // TODO: refactor to nuxi
if (component._internal_install && tryUseNuxt()?.options.test === false) { const internalInstall = ((component as any)._internal_install) as string
// @ts-expect-error TODO: refactor to nuxi if (internalInstall && nuxt?.options.test === false) {
import('../../core/features').then(({ installNuxtModule }) => installNuxtModule(component._internal_install)) 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++}` let identifier = map.get(component) || `__nuxt_component_${num++}`
map.set(component, identifier) map.set(component, identifier)